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.
# 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
# 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?