Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Select using VBA

Posted on 2014-09-25
3
Medium Priority
?
568 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 1000 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 1000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

730 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