Solved

Need correct syntax for SQL statement in VBA

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Can not open the Access Help ? 3 53
Access 2003 query lost it's only join 7 26
How to get the closest date in a query in Access 2010 8 22
Reference Controls on subforms 7 28
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

776 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