Solved

SQL Select using VBA

Posted on 2014-09-25
3
545 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
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 65

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

773 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