Solved

Need correct syntax for SQL statement in VBA

Posted on 2013-10-24
3
599 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Track name AutoCorrect info 14 48
Parse last 2 characters of text value if they are letters 7 17
Access 2010 Query Syntax 5 23
VBA code won't run Delete Query 5 26
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
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…
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…
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…

867 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

15 Experts available now in Live!

Get 1:1 Help Now