Can't Insert new records via VBA

D

Duck

I am a VBA newbie so bear with me. I am trying to appent new records
to a table. Just to try to get the format of the statement right I am
trying the following test code:


Public Sub Update()

Dim strSql As String
Dim strVendor As String
Dim strModel As String

strVendor = "Bosh"
strModel = "M111"

strSql = "INSERT INTO tblInventory (Vendor, ModelNo)"
strSql = strSql & " SELECT '" & strVendor & "', '" & strModel &
"'"

DoCmd.RunSQL strSql

End Sub

When I type "Update()" in the immediate window expecting the code to
add a record to my tblInventory table I keep getting: Compile error:
Expected: =
What am I not understanding???
 
J

Jason Lepack

I am a VBA newbie so bear with me. I am trying to appent new records
to a table. Just to try to get the format of the statement right I am
trying the following test code:

Public Sub Update()

Dim strSql As String
Dim strVendor As String
Dim strModel As String

strVendor = "Bosh"
strModel = "M111"

strSql = "INSERT INTO tblInventory (Vendor, ModelNo)"
strSql = strSql & " SELECT '" & strVendor & "', '" & strModel &
"'"

DoCmd.RunSQL strSql

End Sub

When I type "Update()" in the immediate window expecting the code to
add a record to my tblInventory table I keep getting: Compile error:
Expected: =
What am I not understanding???

Jsut type ?Update
 
D

Douglas J. Steele

When you use the SELECT syntax with INSERT INTO, you need to provide a table
name.

Instead, use the VALUES syntax:

strSql = "INSERT INTO tblInventory (Vendor, ModelNo)"
strSql = strSql & " VALUES ('" & strVendor & "', '" & strModel & "')"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top