问题
I have created a pass through query and trying to call a stored procedure from it.
I am able to execute the queries on sql server database sucessfully but when it comes to stored procedures, i am getting an error as :
"ODBC call Failed"
The problem is with stored procedures only. The queries are executing fine .
Here , is my code :
Dim qdf As DAO.QueryDef, rst As ADODB.Recordset
Dim DatabaseName As String
Dim Server As String
ServerName = "XXXX"
DatabaseName = "XXX"
Set qdf = CurrentDb.CreateQueryDef("")
strConnectionString = "ODBC;DRIVER={sql server};" & _
"DATABASE=" & DatabaseName & ";" & _
"SERVER=" & ServerName & ";" & _
"Trusted_Connection=YES;"
qdf.Connect = strConnectionString
qdf.SQL = " EXEC [dbo].[SAMPLE_TEST]"
qdf.ReturnsRecords = True
Set rst = qdf.OpenRecordset
Debug.Print rst!RecordCount
rst.Close
Set rst = Nothing
Please let me know if i am missing any thing ?
回答1:
To get more information about the cause of an "ODBC--call failed." error we can loop through the DBEngine.Errors
collection and see if there are other messages that might be a bit more descriptive. For example, with the code
qdf.Connect = strConnectionString
qdf.SQL = " EXEC [dbo].[SAMPLE_TEST]"
qdf.ReturnsRecords = True
On Error GoTo oops
Set rst = qdf.OpenRecordset
Debug.Print rst!RecordCount
rst.Close
Set rst = Nothing
Exit Sub
oops:
Dim dbeError As Error
For Each dbeError In DBEngine.Errors
Debug.Print "(" & dbeError.Number & "): " & dbeError.Description
Next
End Sub
we might see the following in the VBA Immediate window:
(229): [Microsoft][ODBC SQL Server Driver][SQL Server]The EXECUTE permission was denied on the object 'SAMPLE_TEST', database 'myDb', schema 'dbo'.
(3146): ODBC--call failed.
Certainly
The EXECUTE permission was denied on the object 'SAMPLE_TEST', database 'myDb', schema 'dbo'.
is considerably more helpful than just
ODBC--call failed.
回答2:
Just looked over your code again. You are not running the qry
Wouldnt you need to do qry.execute
?
来源:https://stackoverflow.com/questions/29774178/odbc-call-failed-with-stored-procedure-pass-through-query