Tuesday, March 20, 2012

open recordset error

I get a 'data type mismatch in criteria expression' when trying to open a recordset from a query that uses a variable in the match. It works fine if I use the query without the varible. The error occurs at the rs1.open line, not the sqlstmt line.

ticket=1002
db_file = App.Path & "\goldpos.mdb"

Set cn = New ADODB.Connection
cn.CursorLocation = adUseClient
cn.ConnectionString = _
"Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & db_file & ";" & _
"Persist Security Info=False"
cn.Open

'sqlstmt = "SELECT * FROM orders where [invoice] = 1002" THIS WORKS
sqlstmt = "SELECT * FROM orders where [invoice] = '" & ticket & "'" 'THIS DOESN'T WORK

Set rs1 = New ADODB.Recordset

rs1.Open sqlstmt, cn, adOpenStatic, adLockOptimistic, adCmdText

Set DataGrid1.DataSource = rs1Originally posted by JerryB
'sqlstmt = "SELECT * FROM orders where [invoice] = 1002" THIS WORKS
sqlstmt = "SELECT * FROM orders where [invoice] = '" & ticket & "'" 'THIS DOESN'T WORK

Why are you adding single quotes around ticket if it just wants a number? Why not:

sqlstmt = "SELECT * FROM orders where [invoice] = " & ticket

No comments:

Post a Comment