1

I am trying to do an SQL query in VBA to retun a specific case number. Whenever I execute the query, it returns an error of "Data Type Mismatch in Criteria Expression". I am passing the query an integer to use to query an autonumber primary key.

    Dim c As ADODB.Connection
    Dim r As ADODB.Recordset
    Dim strSQL As String, strManager As String
    Set c = New ADODB.Connection
    c.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Commit Tracker.accdb; Persist Security Info=False;"
    strSQL = "SELECT * FROM CommitTrk WHERE CASE_ID_NBR = '" & CInt(frmCommitViewer.lstCases.Value) & "'"
    Set r = c.Execute(strSQL)

Of course the debug hilights the execute command. Any help would be appreciated. Am I passing the wrong datatype to match the autonumber? If so, what datatype should I be using? Thanks!

0

1 Answer 1

5

if CASE_ID_NBR has numeric type, you should use it without quotes:

strSQL = "SELECT * FROM CommitTrk WHERE CASE_ID_NBR = " & CInt(frmCommitViewer.lstCases.Value)

you may also want to read this: Global Variables in SQL statement

Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, that worked. I am still trying to wrap my head around this SQL thing. The formatting is throwing me off. I will accept the answer when it allows me to. I will read that in a bit. Thanks!
in general it's common mistake when to use "SELECT ... WHERE Field1 = 1" (numeric) "SELECT ... WHERE Field1 = #01/01/2012#" (dates) and "SELECT ... WHERE Field1 = 'sometext'" (text)
Thanks, I had the same problem and the solution was that easy... my search criteria used 'Jan-24' and the data in the table was date not a string... Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.