Solved

SQL Select using VBA

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now