Solved

VBA script in MS Access calling stored procedure - Invalid Syntax

Posted on 2013-05-28
6
450 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
Comment Utility
Don't you need to put "EXEC" before the procedure name?
0
 
LVL 10

Expert Comment

by:JEaston
Comment Utility
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
Comment Utility
I get an invalid syntax if I write it like this:

sSQL = "EXEC spAddAssetInventory '" & strAssetName & "' & '" & strAssetDate & "' & '" & strAssetFlag & "' & '" & strAssetID & "'"
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 10

Accepted Solution

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

Open in new window

0
 
LVL 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
Comment Utility
awesome!!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

762 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

6 Experts available now in Live!

Get 1:1 Help Now