Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error running a stored procedure from Access

Status
Not open for further replies.

PRMiller

Technical User
Mar 18, 2004
95
US
I am attempting, for the first time (well, more like 63rd time today...) to call an SQL Server stored procedure from Microsoft Access.

I started by copying code from Here's what I pulled in:

Code:
Private Sub cmdGo_Click()

Dim cnn As New ADODB.Connection
Dim cmd As New ADODB.Command
Dim rst As New ADODB.Recordset
Dim param1 As Parameter, param2 As Parameter, param3 As Parameter, param4 As Parameter
Dim begdate As Date
Dim enddate As Date
begdate = #10/1/2001#
enddate = #10/31/2001#

' Connect
Set cnn = CurrentProject.Connection

Set cmd.ActiveConnection = cnn
''Set param1 = cmd.CreateParameter("Input", adDBDate, adParamInput)
Set param1 = cmd.CreateParameter(, adDBDate, adParamInput)
cmd.Parameters.Append param1
param1.Value = begdate
Set param2 = cmd.CreateParameter("Input", adDBDate, adParamInput)
cmd.Parameters.Append param2
param2.Value = enddate
Set param3 = cmd.CreateParameter("Input", adSmallInt, adParamInput)
cmd.Parameters.Append param3
param3.Value = 3
Set param4 = cmd.CreateParameter("Input", adSmallInt, adParamInput)
cmd.Parameters.Append param4
param4.Value = 3

' Set up a command object for the stored procedure.
cmd.CommandText = "dbo.sp_PM_Test"
cmd.CommandType = adCmdStoredProc

' Execute command to run stored procedure

Set rst = cmd.Execute

End Sub

I am receiving the error "The Microsoft Jet database engine cannot find the input table or query 'dbo'."

I am assuming that I need to find a way to tell Access that it should be looking for that stored procedure on the SQL box (which is located at STLVS001\DA).

Question: how do I do that? I must be missing something obvious, as the code from the post above resolved that user's problem.

By the way, does anyone else find it particulary annoying that this Microsoft calls this "Run-time error '-2147217865'"? What's up with the negative numbering?

Thanks,
Paul
 
You must construct a proper connection string telling ADO how to get to your SQL server. All you have done is tell it to use your current projects connection string which is not enough information for ADO.

 
Yeah, I suppose I deserve a slap for that. Ok, here's my code, a bit cleaned up:

Code:
Private Sub cmdGo_Click()

    Dim cnPSR As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim rst As New ADODB.Recordset
    Dim param1 As Parameter
    Dim datDate As Date
    
    Set cnPSR = New ADODB.Connection
    
    cnPSR = "Provider=MSDASQL.1;Persist Security Info=False;Data Source=PSR;Initial Catalog=DA"
    cnPSR.Open
    
    Set cmd.ActiveConnection = cnPSR
    Set param1 = cmd.CreateParameter(, adDBDate, adParamInput)
    
    cmd.Parameters.Append param1
    param1.Value = datDate
    
    cmd.CommandText = "dbo.sp_PM_Test"
    cmd.CommandType = adCmdStoredProc
    
    Set rst = cmd.Execute

End Sub

Now I'm seeing [Microsoft][ODBC SQL Server Driver]Optional feature not implemented.

Er, what? Thoughts on this one?
 
Never mind, found the problem:
When the sample code is run, it gives this error:
Run-time error '2147217887 (80040e21)':
[Microsoft][ODBC SQL Server Driver] Optional feature not Implemented.
This is because SQL Server does not support the adDBDate datatype. To correct this problem, change the datatype of the @theDate parameter to adDBTimeStamp.

In order to determine the number, names, types, and sizes of parameters needed in a stored procedure, use the Parameters.Refresh method of the command object. You can call this method during development of your application to determine the correct requirements for the stored procedure, then remove the expensive call to Parameters.Refresh after you have gathered the necessary data.

I made the change, and it runs perfectly! Thanks for your earlier reply. Sometimes I just need someone to point out the obvious, and it gets me going again.

I think I need more coffee...
 
A good trick to use is to set up your stored procedure just as you have only do not bother with parameters. For a one time only TEST run, do something like the following in a test function. It helps clear up the details of what parameter types are expected by the stopred procedure. Saves lots of time if you are writing stubs for many SP's

Code:
      cmd.Parameters.Refresh
      For Each oParam In cmd.Parameters
        Debug.Print oParam.Name, oParam.Size, oParam.Value, ADODataName(oParam.Type) & "(" & CStr(oParam.Type) & ")"
      Next

Select Case DType
    Case 20 'Indicates an eight-byte signed integer (DBTYPE_I8).
        ADODataName = "adBigInt"
    Case 128 'Indicates a binary value (DBTYPE_BYTES).
        ADODataName = "adBinary"
    Case 11 'Indicates a boolean value (DBTYPE_BOOL).
        ADODataName = "adBoolean"
    Case 8 'Indicates a null-terminated character string (Unicode) (DBTYPE_BSTR).
        ADODataName = "adBSTR"
    Case 136 'Indicates a four-byte chapter value that identifies rows in a child rowset (DBTYPE_HCHAPTER).
        ADODataName = "adChapter"
    Case 129 'Indicates a string value (DBTYPE_STR).
        ADODataName = "adChar"
    Case 6 'Indicates a currency value (DBTYPE_CY). Currency is a fixed-point number with four digits to the right of the decimal point. It is stored in an eight-byte signed integer scaled by 10,000.
        ADODataName = "adCurrency"
    Case 7 'Indicates a date value (DBTYPE_DATE). A date is stored as a double, the whole part of which is the number of days since December 30, 1899, and the fractional part of which is the fraction of a day.
        ADODataName = "adDate"
    Case 133 'Indicates a date value (yyyymmdd) (DBTYPE_DBDATE).
        ADODataName = "adDBDate"
    Case 134 'Indicates a time value (hhmmss) (DBTYPE_DBTIME).
        ADODataName = "adDBTime"
    Case 135 'Indicates a date/time stamp (yyyymmddhhmmss plus a fraction in billionths) (DBTYPE_DBTIMESTAMP).
        ADODataName = "adDBTimeStamp"
    Case 14 'Indicates an exact numeric value with a fixed precision and scale (DBTYPE_DECIMAL).
        ADODataName = "adDecimal"
    Case 5 'Indicates a double-precision floating-point value (DBTYPE_R8).
        ADODataName = "adDouble"
    Case 0 'Specifies no value (DBTYPE_EMPTY).
        ADODataName = "adEmpty"
    Case 10 'Indicates a 32-bit error code (DBTYPE_ERROR).
        ADODataName = "adError"
    Case 64 'Indicates a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (DBTYPE_FILETIME).
        ADODataName = "adFileTime"
    Case 72 'Indicates a globally unique identifier (GUID) (DBTYPE_GUID).
        ADODataName = "adGUID"
    Case 9 'Indicates a pointer to an IDispatch interface on a COM object (DBTYPE_IDISPATCH).Note___This data type is currently not supported by ADO. Usage may cause unpredictable results.
        ADODataName = "adIDispatch"
    Case 3 'Indicates a four-byte signed integer (DBTYPE_I4).
        ADODataName = "adInteger"
    Case 13 'Indicates a pointer to an IUnknown interface on a COM object (DBTYPE_IUNKNOWN).Note___This data type is currently not supported by ADO. Usage may cause unpredictable results.
        ADODataName = "adIUnknown"
    Case 205 'Indicates a long binary value (Parameter object only).
        ADODataName = "adLongVarBinary"
    Case 201 'Indicates a long string value (Parameter object only).
        ADODataName = "adLongVarChar"
    Case 203 'Indicates a long null-terminated Unicode string value (Parameter object only).
        ADODataName = "adLongVarWChar"
    Case 131 'Indicates an exact numeric value with a fixed precision and scale (DBTYPE_NUMERIC).
        ADODataName = "adNumeric"
    Case 138 'Indicates an Automation PROPVARIANT (DBTYPE_PROP_VARIANT).
        ADODataName = "adPropVariant"
    Case 4 'Indicates a single-precision floating-point value (DBTYPE_R4).
        ADODataName = "adSingle"
    Case 2 'Indicates a two-byte signed integer (DBTYPE_I2).
        ADODataName = "adSmallInt"
    Case 16 'Indicates a one-byte signed integer (DBTYPE_I1).
        ADODataName = "adTinyInt"
    Case 21 'Indicates an eight-byte unsigned integer (DBTYPE_UI8).
        ADODataName = "adUnsignedBigInt"
    Case 19 'Indicates a four-byte unsigned integer (DBTYPE_UI4).
        ADODataName = "adUnsignedInt"
    Case 18 'Indicates a two-byte unsigned integer (DBTYPE_UI2).
        ADODataName = "adUnsignedSmallInt"
    Case 17 'Indicates a one-byte unsigned integer (DBTYPE_UI1).
        ADODataName = "adUnsignedTinyInt"
    Case 132 'Indicates a user-defined variable (DBTYPE_UDT).
        ADODataName = "adUserDefined"
    Case 204 'Indicates a binary value (Parameter object only).
        ADODataName = "adVarBinary"
    Case 200 'Indicates a string value (Parameter object only).
        ADODataName = "adVarChar"
    Case 12 'Indicates an Automation Variant (DBTYPE_VARIANT).Note___This data type is currently not supported by ADO. Usage may cause unpredictable results.
        ADODataName = "adVariant"
    Case 139 'Indicates a numeric value (Parameter object only).
        ADODataName = "adVarNumeric"
    Case 202 'Indicates a null-terminated Unicode character string (Parameter object only).
        ADODataName = "adVarWChar"
    Case 130 'Indicates a null-terminated Unicode character string (DBTYPE_WSTR).
        ADODataName = "adWChar"
    Case Else
        ADODataName = "Unknown"
End Select
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top