Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Filtering a database between two dates using MonthCalendars

Hi.

I'm using VB2005Express, and I need my application to filter the contents of its database between two specific dates, as selected on 'From' and 'To' MonthCalendars, but my newbieness won't let me.

I have the data, the calendars and the button, but I just can't work out what the code on the button should be to make this filter happen.

Thanks in advance.
[420 byte] By [AngusHuey] at [2007-11-11 10:17:31]
# 1 Re: Filtering a database between two dates using MonthCalendars
Assuming we're talking about the same app we talked about last week, with a DataGridView bound to a BindingSource, you can do this:

Private Sub FilterButton_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles FilterButton.Click

GaiaBindingSource.Filter = _
String.Format("DateField >= '{0}' AND DateField <= '{1}'", _
DateFrom.SelectionRange.Start.ToShortDateString(), _
DateTo.SelectionRange.Start.ToShortDateString())
End Sub

Replace "DateField" with the name of the database field on which you want to filter records. "DateFrom" and "DateTo" are the MonthCalendar controls.
Phil Weber at 2007-11-11 20:48:14 >
# 2 Re: Filtering a database between two dates using MonthCalendars
Phil, thanks for this.

I now have the following, but the results each time are empty. I've tried switching the date formats on the database both ways (dd/mm/yyyy and mm/dd/yyyy) but no luck. I've renamed the calendars 'DateFrom' and 'DateTo', but once again I'm stuck.

Private Sub CalButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalButton.Click
GaiaDataBindingSource.Filter = _
String.Format("LastDateRecieved >= '{0}' AND LastDateRecieved <= '{1}'", _
DateFrom.SelectionRange.Start.ToShortDateString(), _
DateTo.SelectionRange.Start.ToShortDateString())

End Sub
AngusHuey at 2007-11-11 20:49:14 >
# 3 Re: Filtering a database between two dates using MonthCalendars
What is the data type of the LastDateRecieved column? Is it date/time or text?
Phil Weber at 2007-11-11 20:50:15 >
# 4 Re: Filtering a database between two dates using MonthCalendars
It's just a date - dd/mm/yyyy.
AngusHuey at 2007-11-11 20:51:21 >
# 5 Re: Filtering a database between two dates using MonthCalendars
Each field in a database can hold a specific data type. Is LastDateRecieved configured to hold text data, or something else? What does the field definition look like when you view the database table in design view?
Phil Weber at 2007-11-11 20:52:25 >