Solved

VBA script in MS Access calling stored procedure - Invalid Syntax

Posted on 2013-05-28
6
454 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 VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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 50

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
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…
Familiarize people with the process of utilizing SQL Server functions 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 Microsoft Ac…

820 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