Data type mismatch is criteria expression Error Message (1 Viewer)

psimpson

Registered User.
Local time
Today, 11:07
Joined
Aug 1, 2007
Messages
40
The code below is placed in a button control in ms access 2007 (using vba), however the above error message is returned at the set rst = db.openrecordset(strSQL, OpenSynaset) line. I am struggling with this for some time, but no solution yet. Any help is appreciated.

Thanks.

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rst1 As DAO.Recordset
Dim strSQL As String
Dim strSQL1 As String

Set db = CurrentDb()
strSQL = "SELECT * FROM Table1 WHERE Table1.Project_Number=" & Me.PROJECT_NUMBER
Set rst = db.OpenRecordset(strSQL, OpenDynaSet)
If rst.RecordCount > 0 Then
strSQL1 = "SELECT * FROM Table2 WHERE Professional_REG_Number = " & rst(PROFESSIONAL_REG_NUMBER)
With rst1
If Not (.BOF And .EOF) Then
.Edit
.Delete
.Update
End If
.Close
End With
Set rst1 = Nothing
Else
'Do Nothing
End If
With rst
If Not (.BOF And .EOF) Then
.Edit
.Delete
.Update
End If
.Close
End With

Set rst = Nothing
'====================================ERROR HANDLING===========================================================

Exit_Delete_Record_s__Click:
Exit Sub

Err_Delete_Record_s__Click:
MsgBox Err.Description
Resume Exit_Delete_Record_s__Click
End Sub
 

PNGBill

Win10 Office Pro 2016
Local time
Tomorrow, 06:07
Joined
Jul 15, 2008
Messages
2,271
Declare your project number variable first and use that in your strSQL.
Code:
Dim ProjectNumber As Long
ProjectNumber = Me.PROJECT_NUMBER
Code:
strSQL = "SELECT * FROM Table1 WHERE Table1.Project_Number=" & ProjectNumber
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:07
Joined
Aug 30, 2003
Messages
36,133
Try

strSQL = "SELECT * FROM Table1 WHERE Table1.Project_Number='" & Me.PROJECT_NUMBER & "'"
 

psimpson

Registered User.
Local time
Today, 11:07
Joined
Aug 1, 2007
Messages
40
That worked. Thanks so much to all who responded.
 

PNGBill

Win10 Office Pro 2016
Local time
Tomorrow, 06:07
Joined
Jul 15, 2008
Messages
2,271
Good news but what worked ? My guess or Pauls experienced advice :)
 

psimpson

Registered User.
Local time
Today, 11:07
Joined
Aug 1, 2007
Messages
40
Actually Paul's solution worked best for my needs and all responses appreciated.

Thanks.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:07
Joined
Aug 30, 2003
Messages
36,133
Happy to help. PNGBill, given the error message, your method was doomed from the start. ;)
 

PNGBill

Win10 Office Pro 2016
Local time
Tomorrow, 06:07
Joined
Jul 15, 2008
Messages
2,271
Thanks Paul, I assumed datatypemismatch was because either the data was different ie text/number or the code couldn't understand what it was ie hadn't been declared.

Your solution is really just correcting the code string which I guess means the code was "correct" just couldn't be read by vba ??
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 11:07
Joined
Aug 30, 2003
Messages
36,133
Well, the original code treated Project_Number as a numeric data type. The error message is telling you that there's a mismatch, which meant that the data type is text. My code adds the quotes around the value which are required for text values.

Your code to use a variable instead of the form reference directly would not have worked, as it still assumed a numeric data type (it would work fine with a numeric data type). I wouldn't normally bother with declaring/setting/using a variable if I'm only going to use it once like that, but that's personal preference.
 

PNGBill

Win10 Office Pro 2016
Local time
Tomorrow, 06:07
Joined
Jul 15, 2008
Messages
2,271
I understand, Thanks. I jumped to the conclusion that a control named Project_Number held a number.
 

Users who are viewing this thread

Top Bottom