Solved

Syntax Error in VBA SQL

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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.
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…

831 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