We help IT Professionals succeed at work.

Variable in SQL Statement

353 Views
Last Modified: 2013-12-25
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.
Comment
Watch Question

SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
{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

Author

Commented:
Thank you for your prompt reply
Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
Thanks for the grade.  Good luck with your project.  -Jim
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.