Solved

Syntax Error in VBA SQL

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 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

19 Experts available now in Live!

Get 1:1 Help Now