Avatar of RichFrot
RichFrotFlag for United States of America asked on

Variable in SQL Statement

Using VB 6, trying to pass a variable to a SQL Statement.  Recieve the following error message.  " Data type mismatch in criteria expression".  The problem is in the Where Statement using the variable txtClientNo.

Current Code:
Dim txtClientNo As String
txtClientNo = Mid(sText, 34, 4)

SQLQuery2 = "Select tblSysCompanyAvail.CompanyCode, tblSysCompanyAvail.CompanyName, tblSysCompanyAvail.EmployerIDNum, tblSysCompanyAvail.Address1, tblSysCompanyAvail.City, tblSysCompanyAvail.State, tblSysCompanyAvail.ZipCode " _
& "FROM tblSysCompanyAvail " _
& "WHERE tblSysCompanyAvail.CompanyCode = " & txtClientNo & "; "
rst2.Open SQLQuery2, cnn2, adOpenKeyset, adLockOptimistic

When I change " & txtClientNo & " to '1345' an actual value the code works.  The CompanyCode database field is a text field.
Visual Basic ClassicSQL

Avatar of undefined
Last Comment
Jim Horn

8/22/2022 - Mon
Jim Horn

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jim Horn

{some cleanup}

Dim sSQL as String

sSQL = "Select CompanyCode, CompanyName, EmployerIDNum, Address1, City, State, ZipCode "
sSQL = sSQL & "FROM tblSysCompanyAvail " 
sSQL = sSQL & "WHERE CompanyCode = '" & txtClientNo & "'; " 

rst2.Open sSQL, cnn2, adOpenKeyset, adLockOptimistic

Thank you for your prompt reply
Jim Horn

Thanks for the grade.  Good luck with your project.  -Jim
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck