简体   繁体   中英

SQL Statement to grab data from entry form to Data table in Microsoft Access 2010 VBA

So I'm setting up a database in MS Access 2010 (first time doing this). I can't seem to get the syntax of the SQL statement right. I want to take the data I have entered into a data entry form and insert a new row into a table, with that data. All of this using SQL in the VBA editor (only code I have available, all others are blocked)

Private Sub Add_Click()
    Dim sSQL As String

    sSQL = "INSERT INTO Data (Type, Name, Quantity, Amount, Class, Series, Client, Date1, Date2) VALUES " & _
            "('" & Me!txtType & "','" & Me!txtName & "'," & Me!txtQuantity & "," & Me!txtAmount & ",'" & Me!txtClass & "','" & Me!txtSeries & "','" & Me!txtClient & "'," & Me!date1 & "," & Me!date2 & ")"
    Debug.Print sSQL

    CurrentDb.Execute sSQL
End Sub

So i Get the following error when i run the code, from the VBA editor :

"Run-time error '3134':

Syntax error in INSERT INTO statement."

And the printout of the SQL statement looks correct :

INSERT INTO Data (Type, Name, Quantity, Amount, Class, Series, Client, Date1, Date2)
    VALUES ('Purhcase', 'TV', 500, 100, '', '', ' Bob', 05.08.2019, 05.08.2019)

Found the answer :

turns out one of the names of my data fields was a reserved word, therefore i simply put them all into [] and it solved the issue.

Thanks anyways for the help guys!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM