Solved

SQL Select using VBA

Posted on 2014-09-25
3
563 Views
Last Modified: 2014-09-26
Private Sub Command0_Click()

'  Want to create a SQL string in VBA that will select records in tbl based on memory variables.
Dim mVar1 As Long
Dim mVar2 As String
Dim mVar3 As String
Dim strSQL As String
Dim db As DAO.Database
Dim rs As DAO.Recordset

mVar1 = 4451
mVar2 = "Blue"
mVar3 = "(Color# 9486)"

'  This is the code that I get compile error:
strSQL = "SELECT Field1, Field2, Field3 FROM tbl WHERE Field1 = '" & mVar1 & 'AND Field2 = ' & mVar2 & 'AND Field3 = ' & mVar3 &"'"
Set rs = db.OpenRecordset(strSQL, , dbFailOnError)

End Sub
0
Comment
Question by:clock1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 250 total points
ID: 40344618
It looks like you are missing the double quotes around your strings and you are also missing some whitespace.

strSQL = "SELECT Field1, Field2, Field3 FROM tbl WHERE Field1 = '" & mVar1 & "' AND Field2 = '" & mVar2 & "' AND Field3 = '" & mVar3 &"'"
0
 
LVL 66

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 40344627
>WHERE Field1 = '" & mVar1 & 'AND
Since Field1 is a Long Integer, you don't need to delineate the value with single quote marks, so instead it's
WHERE Field1 = " & mVar1 & " AND 

Open in new window


Also you forgot
Set Db = CurrentDb() 

Open in new window

'or whatever database we're talking about.

Also, when troubleshooting queries in VBA code, right after the strSQL= line add this
Debug.Print strSQL

Open in new window

Then run, then copy the resulting SQL from your Immediate window, open a new query, paste it into the query, execute, and fiddle with it until it works.  Then you'll know what to change back in VBA code.
1
 

Author Closing Comment

by:clock1
ID: 40347056
Thanks
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question