Solved

VBA script in MS Access calling stored procedure - Invalid Syntax

Posted on 2013-05-28
6
452 Views
Last Modified: 2013-05-28
I am trying to call a stored procedure from SQL server from Access.  I know the procedure works fine because I can run in manually, enter the parameters and it writes to the table.  I am getting an invalid syntax and assume I am getting my " ' " mixed up.  Here it is:

Dim strAssetName As String
Dim strAssetDate As String
Dim strAssetFlag As String
Dim strAssetID As String

Dim sSQL As String



strAssetName = Form_frmDashboard.txtAssetName.Value
strAssetDate = Form_frmDashboard.txtAssetDate.Value
strAssetFlag = Form_frmDashboard.txtAssetFlag.Value
strAssetID = Form_frmDashboard.txtAssetID.Value

sSQL = "spAddAssetInventory '" & strAssetName & "' & '" & strAssetDate & "' & '" & strAssetFlag & "' & '" & strAssetID & "'"

DoCmd.RunSQL sSQL

Can anyone help? ~ Thanks
0
Comment
Question by:marku24
  • 3
  • 2
6 Comments
 
LVL 10

Expert Comment

by:JEaston
ID: 39201226
Don't you need to put "EXEC" before the procedure name?
0
 
LVL 10

Expert Comment

by:JEaston
ID: 39201248
Also, I've just relised you do not seperate your parameters.  Normally I would expect the final SQL statement to look something like:
Exec  spAddAssetInventory @Asset='ABCD'

Open in new window

0
 

Author Comment

by:marku24
ID: 39201312
I get an invalid syntax if I write it like this:

sSQL = "EXEC spAddAssetInventory '" & strAssetName & "' & '" & strAssetDate & "' & '" & strAssetFlag & "' & '" & strAssetID & "'"
0
Complete Microsoft Windows PC® & Mac Backup

Backup and recovery solutions to protect all your PCs & Mac– on-premises or in remote locations. Acronis backs up entire PC or Mac with patented reliable disk imaging technology and you will be able to restore workstations to a new, dissimilar hardware in minutes.

 
LVL 10

Accepted Solution

by:
JEaston earned 280 total points
ID: 39201325
Try this:
sSQL = "EXEC spAddAssetInventory '" & strAssetName & "', '" & strAssetDate & "', '" & strAssetFlag & "', '" & strAssetID & "'"

Open in new window

0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39201331
in your string

with the arguments are separated by &

strAssetName = "a"
strAssetDate = "b"
strAssetFlag = "c"
strAssetID = "d"

sSQL = "spAddAssetInventory '" & strAssetName & "' & '" & strAssetDate & "' & '" & strAssetFlag & "' & '" & strAssetID & "'"

Open in new window

this gives

spAddAssetInventory 'a' & 'b' & 'c' & 'd'

Open in new window


maybe

sSQL = "spAddAssetInventory '" & strAssetName & "' , '" & strAssetDate & "' , '" & strAssetFlag & "' , '" & strAssetID & "'"

Open in new window


Regards
0
 

Author Closing Comment

by:marku24
ID: 39201346
awesome!!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

948 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

20 Experts available now in Live!

Get 1:1 Help Now