Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Syntax Error in VBA SQL

Posted on 2016-08-30
5
Medium Priority
?
50 Views
Last Modified: 2016-09-20
I am trying to run an SQL statement in MS Access VBA.  The values in the immediate window for strSQl is:

INSERT INTO tblMAPUniqueID_Junction_SessionID(UniqueJournalID,EventID) VALUES(A011473893_1_614,287984)

but I am getting an error when I try to execute: "too few pararmeters: Expected 1.  Is my statement written incorrectly?

Function MultiSelectMAP()

Dim ItemIndex As Variant
Dim strSessionID As String
Dim strMAPTransactions As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String

Set db = CurrentDb()

strSessionID = Me.lstEligibleSessions

For Each ItemIndex In Me.lstDeptDetails.ItemsSelected
    strMAPTransactions = Me.lstDeptDetails.ItemData(ItemIndex)
    MsgBox Me.lstDeptDetails.ItemData(ItemIndex)

        strSQL = "Select * from tblMAPUniqueID_Junction_SessionID"
       
       
        Set rs = db.OpenRecordset(strSQL)
       
            strSQL = "INSERT INTO tblMAPUniqueID_Junction_SessionID(UniqueJournalID,EventID) VALUES(" & strMAPTransactions & "," & strSessionID & ")"


            db.Execute (strSQL)
           
Next

End Function
0
Comment
Question by:marku24
  • 4
5 Comments
 
LVL 85

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 2000 total points (awarded by participants)
ID: 41777145
Looks like that first value is a Text value, so:

   strSQL = "INSERT INTO tblMAPUniqueID_Junction_SessionID(UniqueJournalID,EventID) VALUES('" & strMAPTransactions & "'," & strSessionID & ")"

Note I inserted single quotes ( ' ), which I highlighted in Bold.
0
 
LVL 85
ID: 41777149
Also, what's the point of opening the Recordset in the loop? I realize you may have pared down the code to post it here, but as it stands, there doesn't seem to be any reason to open that recordset.
0
 

Author Comment

by:marku24
ID: 41778519
Thank you so much.  The quotes always confuse me.  I thought I had to open the recordset to read it.  Tnanks
0
 
LVL 85
ID: 41779604
The quotes always confuse me.  

They can be confusing when you first are learning SQL. Essentially, any Text data must be enclosed in single or double quotes. I use single quotes since I do as much work in SQL Server as I do in Access, and it was just easier to do the same in all.

I thought I had to open the recordset to read it.

A Recordset does have to be open to "read" it, but in the code above you're not reading it. To read a value from a recordset, you'd use this syntac:

msgbox rst("FieldName")

Of course, a Recordset often contains more than one record, so that would just give you the value from the current record.

In your case, you're not inserting into a recordset, but rather into a Table.
0
 
LVL 85
ID: 41806193
Very common mistake - not enclosing Text values in single or double quotes.
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
Windows Explorer lets you open cabinet (cab) files like any other folder. In VBA you can easily handle normal files and folders, but opening and indeed creating cabinet files takes a lot more - and that's you'll find here.
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 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 …
Suggested Courses

971 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