Passing values from VBA variable to SQL

N

Nexus

Hi,

How do I pass values from a VBA variable into a SQL
variable and vice versa? Below are the codes that I've
used and have encountered the following error:

"Run-time error '170'
Line 1: Incorrect syntax near 'tblVendors'"

Codes:

Dim strVendorID As String

DoCmd.RunSQL "DECLARE @strVendorID2 nvarchar (6)" _
& "SET @strVendorID2 = strVendorID" _
& "UPDATE tblVendors SET VendorID = '9898' WHERE VendorID
= @strVendorID2"

My take is that sql doesn't recognise strVendorID in line
2 of my codes as strVendorID belongs to VBA. Is this
really the problem? When i replace strVendorID with a
sample number, 9999(see codes below), the statement is
able to run.

Dim strVendorID As String

DoCmd.RunSQL "DECLARE @strVendorID2 nvarchar (6)" _
& "SET @strVendorID2 = '9999'" _
& "UPDATE tblVendors SET VendorID = '9898' WHERE VendorID
= @strVendorID2"
 
M

Mary Chipman

That's because you're passing your variable names as literals. SQL
Server is looking for a VendorID equal to "@strVendorID2" in the WHERE
clause, not the actual value in the variable.

--Mary
 
N

Norman Yuan

DoCmd.RunSQL "DECLARE @strVendorID2 nvarchar (6)" _
& "SET @strVendorID2 = " & strVendorID & _
& "UPDATE tblVendors SET VendorID = '9898' WHERE VendorID
= @strVendorID2"
 
N

Nexua

Yup, it does work! Thanks!
-----Original Message-----
DoCmd.RunSQL "DECLARE @strVendorID2 nvarchar (6)" _
& "SET @strVendorID2 = " & strVendorID & _
& "UPDATE tblVendors SET VendorID = '9898' WHERE VendorID
= @strVendorID2"




.
 

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