Solved

Need correct syntax for SQL statement in VBA

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

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
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 stored procedures 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 Micr…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

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

17 Experts available now in Live!

Get 1:1 Help Now