Hello all,
I have a filter correctly working, based on two date fields on a main form, which displays data on a subform. My problem now is that I'd like to include an "If" statement for when the date fields are blank. Here's my code:
Private Sub ApplyDates_Click()
Dim FromFilter
Dim ToFilter
FromDate = Me.FromDate
ToDate = Me.ToDate
If FromDate = Null Then
FromFilter = "#1/1/1900#"
Else
FromFilter = "#" & FromDate & "#"
End If
If ToDate = Null Then
ToFilter = "#12/31/3000#"
Else
ToFilter = "#" & ToDate & "#"
End If
If (Len(FromFilter) & Len(ToFilter)) Then
Me.Subform.Form.Filter = "ReleaseDate>=" & _ FromFilter & "AND ReleaseDate<=" & ToFilter
Me.Subform.Form.FilterOn = True
End If
End Sub
This is the error message I get every time run the filter when leaving the date fields blank:
Syntax error in date in query expression 'ReleaseDate>=## AND ReleaseDate<=##'.
The strange thing is, if I run this without the "If" statement and use either [FromFilter = "#1/1/1900#"] or [FromFilter = "#" & FromDate & "#"] (and the same for ToFilter), the code will run okay for that particular scenario. I only get the error message when having the "If".
Also, if I go thru Debug when the date fields are blank, both FromFilter and ToFilter are equal to "##". I don't understand why they are not being assigned the date values I specified after the "Then" statement.
Hope this makes sense to everyone.
Thanks for any help!
I have a filter correctly working, based on two date fields on a main form, which displays data on a subform. My problem now is that I'd like to include an "If" statement for when the date fields are blank. Here's my code:
Private Sub ApplyDates_Click()
Dim FromFilter
Dim ToFilter
FromDate = Me.FromDate
ToDate = Me.ToDate
If FromDate = Null Then
FromFilter = "#1/1/1900#"
Else
FromFilter = "#" & FromDate & "#"
End If
If ToDate = Null Then
ToFilter = "#12/31/3000#"
Else
ToFilter = "#" & ToDate & "#"
End If
If (Len(FromFilter) & Len(ToFilter)) Then
Me.Subform.Form.Filter = "ReleaseDate>=" & _ FromFilter & "AND ReleaseDate<=" & ToFilter
Me.Subform.Form.FilterOn = True
End If
End Sub
This is the error message I get every time run the filter when leaving the date fields blank:
Syntax error in date in query expression 'ReleaseDate>=## AND ReleaseDate<=##'.
The strange thing is, if I run this without the "If" statement and use either [FromFilter = "#1/1/1900#"] or [FromFilter = "#" & FromDate & "#"] (and the same for ToFilter), the code will run okay for that particular scenario. I only get the error message when having the "If".
Also, if I go thru Debug when the date fields are blank, both FromFilter and ToFilter are equal to "##". I don't understand why they are not being assigned the date values I specified after the "Then" statement.
Hope this makes sense to everyone.
Thanks for any help!