I am trying to execute a stored procedure in Access 2013 using VBA. On 64-bit Windows 10 with 32-bit Access 2013 (corporate standard, can't change that).
Connecting to a SQL Server 2008.
Not sure why this is so hard, but I just can't make it work!
I have a generic function named SQLExec:
Public Function SQLExec(SQL as String) As DAO.Recordset
Dim Rs As DAO.Recordset
Dim MyDB As DAO.DATABASE
Debug.Print SQL
On Error GoTo SQLExecErr
Set MyDB = CurrentDb
Set Rs = MyDB.OpenRecordset(SQL, dbOpenSnapshot, dbSQLPassThrough)
Set SQLExec = Rs
CleanUp:
Set Rs = Nothing
Set MyDB = Nothing
Exit Function
SQLExecErr:
Debug.Print Err.Number, Err.Description
MsgBox "Error " & Err.Number & " in SQLExec: '" & Err.Description & ".'", vbOKOnly + vbCritical, "Error in SQLExec"
If IsDeveloper() Then
Stop
Resume
Else
Resume CleanUp
End If
End Function
I try to pass in a procedure name with parameters, but it always fails.
sReplaceDevice 'AA000000', 1000, 'AA000000', 'AA000001', 19"
Error:
Error 3129 in SQLExec: 'Invalid SQL statement; expected 'DELETE', 'INSERT', 'PROCEDURE', 'SELECT', or 'UPDATE'..'
I tried prepending EXECUTE
and PROCEDURE
- same result. I tried adding the parameter IDs to the query (@param1 ='test', for example.) - still same result.
Does anyone have any suggestions? I'm dumbfounded that this should be so hard.
EDIT: This question has been marked as a duplicate of another question. Firstly, it would be nice if a link to that other question were provided. Secondly, I have searched and searched on this question and have come up with several solutions, some from StackOverflow, NONE OF WHICH WORK. Any input would be appreciated.