Dorian
I would like to select the year from the first cbo, then the month from
the second combo then the day from the third cbo and the continuous form
will sort automatically as each cbo is selected. Can this be done and
is this the best way to filter by date / date ranges?
Let's take this in layers. A basic example of filtering on a date if you
were entering a full date in a TextBox would be...
Me.Filter = "DateField = #" & Me.DateTextBox & "#"
Me.FilterOn = True
Now, you need to do that but by constructing the date out of three
ComboBox selections...
Dim FilterDate as Date
FilterDate = DateSerial(Me.YearCombo, Me.MonthCombo, Me.DayCombo)
Me.Filter = "DateField = #" & _
Format(FilterDate,"yyyy-mm-dd") & "#"
Me.FilterOn = True
Further, you need logic that uses code similar to the above but where a
valid FilterDate is still produced even when a selection has not been
made for the month and day. By the way, are you making it a requirement
that a Year must always be chosen so we count on that always being
supplied? For now I am assuming that to be the case.
Not only do you need to deal with a month and day not being selected, but
now your date filter will have to be for a range rather than a single
value. Actually, if the Dates in your table contain a non-midnight time
component then you would have needed a range for the above examples as
well. To that end we need a FilterDateStart and a FilterDateEnd rather
than simply a single FilterDate.
Dim FilterDateStart as Date
Dim FilterDateEnd as Date
If IsNull(Me.MonthCombo) Then
FilterDateStart = DateSerial(Me.YearCombo, 1, 1)
FilterDateEnd = DateSerial(Me.YearCombo + 1, 1, 1)
Else
FilterDateStart = DateSerial(Me.YearCombo, _
Me.MonthCombo, _
Nz(Me.DayCombo, 1))
If IsNull(Me.DayCombo) Then
FilterDateEnd = DateSerial(Me.YearCombo, _
Me.MonthCombo + 1, 1)
Else
FilterDateEnd = DateSerial(Me.YearCombo, _
Me.MonthCombo, _
Me.DayCombo + 1)
End If
End If
Me.Filter = "DateField >= #" & _
Format(FilterDateStart,"yyyy-mm-dd") & "# " & _
"AND DateField < #" & _
Format(FilterDateEnd,"yyyy-mm-dd") & "#"
Me.FilterOn = True
I am making an assumption above that the user is not allowed to leave the
month blank and still select a date. If that were done then the code
above for end date would produce unexpected results. What I have there
allows for selecting a month without a date, but not a date without a
month.
Notice that I also go one day past the nominal end date but I use < for
that test rather that <=. This way the code works even if your dates in
the table have a time-component.
You would have to put this code in a sub-routine of your form and call it
in the AfterUpdate event of all three of your ComboBoxes. I would also
recommend that you disable the month and date Combos by default. The
month Combo would be enabled after they select a year and the date Combo
would be enabled when they select a month.
All of the above is untested air-code intended to give you the basic
idea. I could very well have a few problems in there, but it's a place
to start.