Solved

Need correct syntax for SQL statement in VBA

Posted on 2013-10-24
3
630 Views
Last Modified: 2013-10-24
I am having trouble executing the below code in Access VBA.  I assume my SQL statement syntax is off.  Possibly with Date or Currency?

Dim strInvoiceID As String
Dim curInvoiceAmt As Currency
Dim datInvoiceDate As Date
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Dim strUser As String


strInvoiceID = Form_frmDashboard.txtLstBoxInvID.Value
curInvoiceAmt = Form_frmDashboard.txtInvRcvdAmt.Value
datInvoiceDate = Format(Form_frmDashboard.txtInvRcvdDate, "yyyy\/mm\/d")



Set db = CurrentDb()

strSQL = "Select * from [tblReceivableManagament]"

Set rs = db.OpenRecordset(strSQL)

    If rs.RecordCount > 0 Then
            'clear existing log in that may not have cleared out
           
        Else
    End If

strUser = "'" & fncUserID & "'"
strInvoiceID = "'" & strInvoiceID & "'"




            strSQL = "INSERT INTO tblReceivableManagement (Invoice#, InvoicePaidDate, InvoicePaidAmount, UserID)VALUES(" & strInvoiceID & ", #" & datInvoiceDate & "#,curInvoiceAmt, " & strUser & ")"
           

            db.Execute (strSQL)
0
Comment
Question by:marku24
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 18

Assisted Solution

by:lludden
lludden earned 50 total points
ID: 39598853
Put brackets around field names with special characters:

strSQL = "INSERT INTO tblReceivableManagement ([Invoice#], InvoicePaidDate, InvoicePaidAmount, UserID)VALUES(" & strInvoiceID & ", #" & datInvoiceDate & "#,curInvoiceAmt, " & strUser & ")"
            

Open in new window

0
 
LVL 61

Accepted Solution

by:
mbizup earned 200 total points
ID: 39598882
In addition to the brackets, you need to separate all of the variables from the sql string, and treat the commas between values as part of the string like this:


            strSQL = "INSERT INTO tblReceivableManagement ([Invoice#], InvoicePaidDate, InvoicePaidAmount, UserID) VALUES(" & strInvoiceID & ", #" & datInvoiceDate & "#," & curInvoiceAmt & ", " & strUser & ")"

Open in new window


This assumes Invoice# and userID are numeric.


if InvoiceNumber and UserID are TEXT, you'll need quotes for delimiters around those values like this:

            strSQL = "INSERT INTO tblReceivableManagement ([Invoice#], InvoicePaidDate, InvoicePaidAmount, UserID) VALUES('" & strInvoiceID & "', #" & datInvoiceDate & "#," & curInvoiceAmt & ", '" & strUser & "')"

Open in new window

0
 

Author Closing Comment

by:marku24
ID: 39598967
nice job, thank you.  That special character screwed me up.
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

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

752 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