I try to develop a simple filter for a form bound to a table using _DblClick event for form's controls.
Except I don't know how to determine the field data type, everything is fine... :) until I try to handle the date data type.
Again and again the same error in line 52:
Run-time error in date in query expression.
The (string) value for MyFilter is ([RecData] = #11.17.2011#) (when the error raise)
What is wrong here ?!?!?
Except I don't know how to determine the field data type, everything is fine... :) until I try to handle the date data type.
Again and again the same error in line 52:
Run-time error in date in query expression.
The (string) value for MyFilter is ([RecData] = #11.17.2011#) (when the error raise)
What is wrong here ?!?!?
Code:
Option Compare Database
Option Explicit
Dim MyFilter As String
Private Sub ID_Nod_DblClick(Cancel As Integer)
Call UpdateFilter("ID_Nod", Me.ID_Nod, "Number")
End Sub
Private Sub Nod_DblClick(Cancel As Integer)
Call UpdateFilter("Nod", Me.Nod, "Text")
End Sub
Private Sub AreContract_DblClick(Cancel As Integer)
Me.AreContract = Not (Me.AreContract) 'Restore value after first click
Call UpdateFilter("AreContract", Me.AreContract, "Yes/No")
End Sub
Private Sub RecData_DblClick(Cancel As Integer)
Call UpdateFilter("RecData", Me.RecData, "Date")
End Sub
Private Sub UpdateFilter(MyField As String, WithThisValue, FieldType As String)
Dim dtData As Date
Dim FixFilter As String
Select Case FieldType
Case "Number"
FixFilter = "([" & MyField & "] = " & WithThisValue & ")"
Case "Text"
FixFilter = "([" & MyField & "] = """ & WithThisValue & """)"
Case "Yes/No"
' Same as case "Number"
FixFilter = "([" & MyField & "] = " & WithThisValue & ")"
Case "Date"
FixFilter = "([" & MyField & "] = " & WithThisValue & ")"
FixFilter = "([" & MyField & "] = #" & WithThisValue & "#)"
FixFilter = "([" & MyField & "] = " & "#" & Format(CDate(WithThisValue), "mm/dd/yyyy") & "#)"
Case Else
Stop
End Select
If MyFilter = "" Then
MyFilter = FixFilter
Else
MyFilter = MyFilter & " AND " & FixFilter
End If
Me.Filter = MyFilter
Me.FilterOn = True
End Sub
Private Sub cmdResetFilter_Click()
MyFilter = ""
Me.FilterOn = False
End Sub
Comment