Solved

SQL Select using VBA

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

734 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