Searching Databases

tk.unlimited

Active member
Joined
Apr 2, 2005
Messages
27
Programming Experience
1-3
Hey All!!
Having troubles with searching for data within a DB. I have to RESTRICT the HTML report output by using an IF statement and selecting only the records where the student Birth Month matches a month entered by the user (i.e user defined month). For Example, if the user enters 1, the report should only show the students born in January. I am uncertain whether it lies in the SQL Query which currently is
VB.NET:
sSql = "select * from student
" or if it lies within the code itself.. Im sure you have to put it in the Do Loop though :confused:
I have the following code:

VB.NET:
iTotal = 0 
Do While oDataReader.Read() And iTotal <= 10
 
' --- Place here If Statement for Search Report to be completed in :
 
' --- Page 6 - Point 3 - Bullet Point No 8 Restricting HTML to a Month
 
sRecordLine = "<TR>"
 
For iColCounter = 0 To oDataReader.FieldCount - 1
 
' --- Below is an If Statement to format Dates .. Advanced 
 
If dtTempDate.GetType.Equals(oDataReader.Item(iColCounter).GetType) Then
 
sColOutput = CDate(oDataReader.Item(iColCounter)).ToShortDateString
 
Else
 
sColOutput = oDataReader.Item(iColCounter).ToString
 
End If
 
sRecordLine = sRecordLine & "<td>" & sColOutput & "</td>"
 
Next
 
sRecordLine = sRecordLine & "</TR>"
 
 
 
iTotal = iTotal + 1
 
sPageText = sPageText & sRecordLine & vbCrLf
 
'End If ' --- end if of Search Report If Statement
 
Loop
 
How could i forget a MONTH function :(

Actually as John gave you an example i can only add that this func returns a number from 1 to 12 corresponding to the month of the given date. Its syntax is MONTH (date-expression)

Also there are similar time functions like DAY, MDY, MONTHNAME (returns the name of the month from a date), MONTHS (returns the integer number of months between two dates), WEEKDAY, YEAR etc.

Regards ;)
 
IS there something like MONTHNAME AND YEAR?? That way if the user inputs January 2005 the SQL query will search for 01/01/2005 - 31/01/2005
 
Of course. You're just ANDing together two conditions like any other.
VB.NET:
SELECT * FROM student WHERE MONTHNAME(dob) = 'January' AND YEAR(dob) = 2005
Be aware that not all databases support the MONTHNAME function, most notably SQL Server (although Access does). The MONTH function is more widely supported.
 
Then you need to extract the month value and year value seperately from that TextBox before placing them into the SQL statement. I'd actually suggest using two seperate controls. I'd use a ComboBox or DomainUpDown for the month and a NumericUpDown for the year. That will guarantee that the selections are valid and make it easy to get the values. So many people seem to use TextBoxes for everything, but they should generally only be used when you want to allow free-form data entry. If you want to limit what data is entered then use the controls that provide the appropriate validation. It's also easier for the user to select from a list of months than to type a full month name, and they get grumpy if they get error messages because they entered invalid data.
 
Back
Top