Solved

Syntax Error in VBA SQL

Posted on 2016-08-30
5
44 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 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 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 84
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 84
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 84
ID: 41806193
Very common mistake - not enclosing Text values in single or double quotes.
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

821 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