Error: mismatched data types in the criterion expression

meme'1992

Registered User.
Local time
Yesterday, 23:40
Joined
Apr 2, 2018
Messages
14
In the debug of a vba form, i have this error "mismatched data types in the criterion expression"

Code:
Private Sub btn_find_Click()
    Dim tmpRS As DAO.Recordset
    Set tmpRS = CurrentDb.OpenRecordset(" SELECT name FROM Person WHERE [Surname]= '" & Me.txt_Surname & "' AND [Date] = '" & Me.txt_Date & "' ")
    Me.txt_name = tmpRS.Fields(0)
End Sub
I think is wrong the date. The criterios is #. Have I to convert?
 
Last edited:
Code:
Private Sub btn_find_Click()
    Dim tmpRS As DAO.Recordset
    Set tmpRS = CurrentDb.OpenRecordset(" SELECT name FROM Person WHERE [Surname]= '" & Me.txt_Surname & "' AND [Date] = #" & Format(Me.txt_Date,"mm/dd/yyyy") & "#")
    Me.txt_name = tmpRS.Fields(0)
End Sub
 
Two things - your date needs formatting correctly, have a read here - http://allenbrowne.com/ser-36.html

Secondly name is a reserved word - surround it in [name] or better still change it to something else.
 
Yes, you use # for dates.

I tend to use a custom format nowadays

Code:
Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
then something like
Code:
TempVars("StartDate") = Format(Me.txtDateBW, strcJetDate)
then
Code:
strSQL = strSQL & " WHERE (((tblEmployee.EndDate) Is Null) AND ((tblEmployee.Payroll)=True)) OR (((tblEmployee.EndDate) > " & [TempVars]![StartDate] & "))"
HTH
 

Users who are viewing this thread

Back
Top Bottom