Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Executing Stored Procedure from Access

Posted on 2008-06-11
4
Medium Priority
?
1,099 Views
Last Modified: 2012-06-27
I'm trying to run a stored procedure with parameters from Access.

I get an "object required" error message on the first parameter line.
If I remove the parameters, I get "The Microsoft Jet database engine cannot find the input table or query 'usp_printerorder_pending'. Make sure it exists and that its name is spelled corretly."

How do I set the parameters correctly and does it have something to do with not being able to find the stored procedure? And if so, how do I make it see the stored procedure?
Dim cmd As ADODB.Command
    Dim rs As ADODB.Recordset
    
    Set cmd = New ADODB.Command
    Set cmd.ActiveConnection = CurrentProject.Connection
    
    cmd.CommandText = "usp_printerorder_pending"
    cmd.CommandType = adCmdStoredProc
    
    'Set the parameters
    cmd.Parameters.Append (cmd.CreateParameter("@FromDate", adDate, adParamInput, 10, Me.dteFrom.Value))
    cmd.Parameters.Append (cmd.CreateParameter("@ToDate", adDate, adParamInput, 10, Me.dteTo.Value))
 
    Set rs = New ADODB.Recordset
    Set rs = cmd.Execute
    Me.frmPrinterOrdersub.Form.Recordset = rs
 
    rs.Close
    Set rs = Nothing
    Set cmd = Nothing

Open in new window

0
Comment
Question by:slamhound
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 77

Expert Comment

by:peter57r
ID: 21766767
Trying adding the the owner prefix to the sp name. (dbo.usp_printerorder_pending or whatever it is)
0
 
LVL 1

Expert Comment

by:treanor99
ID: 21766937
Is your database set up as project, i.e. the name ends .adp?

If so this code should work
Function ExecSP(strSQL As String, Optional intTimeout As Integer = 360, Optional blnReturnError As Boolean = False) As String
' runs sql command
' creates log entry in command_log
' If there is an error than return it otherwise return ""
    Dim s_sql_log As String
    Dim objCmd As New ADODB.Command
 
    s_sql_log = _
        "INSERT INTO command_log " & _
        "(command, datetime_sent, user_sent) " & _
        "VALUES ('" & Replace(strSQL, "'", "''") & "', GETDATE(), '" & Environ("user") & "')"
 
    ' create log entry
    DoCmd.RunSQL s_sql_log
 
    ' run sql command with 3 minute timeout
    With objCmd
        .ActiveConnection = CurrentProject.Connection
        .CommandType = adCmdText
        .CommandText = strSQL
        .CommandTimeout = intTimeout
        If blnReturnError = True Then
            On Error Resume Next
            .Execute
            If Err.Number <> 0 Then
                ExecSP = Err.Description
            Else
                ExecSP = ""
            End If
            On Error GoTo 0
        Else
            .Execute
        End If
    End With
    Set objCmd = Nothing
End Function

Open in new window

0
 
LVL 10

Author Comment

by:slamhound
ID: 21774527
Referencing the owner and server name etc has no effect. I'm not setup using a project.
0
 
LVL 10

Accepted Solution

by:
slamhound earned 0 total points
ID: 21808181
I solved it myself.
    Dim cnn As ADODB.Connection
    Dim cmd As ADODB.Command
    Dim rs As ADODB.Recordset
 
    Set cnn = New ADODB.Connection
    cnn.Open "Driver={SQL Server};Server=SOY-SQL;Database=SOYSourceSQL;UID=xxxx;Password=xxxxxx"
    cnn.CursorLocation = adUseClient
 
    Set cmd = New ADODB.Command 'New Command
 
    With cmd
        .ActiveConnection = cnn
        ' .ActiveConnection = Application.CurrentProject.Connection
        .CommandText = "usp_printerorder_pending"
        .CommandType = adCmdStoredProc
        .Prepared = True
        '.Parameters.Refresh
        .Parameters("@FromDate").Value = Me.dteFrom.Value
        .Parameters("@ToDate").Value = Me.dteTo.Value
        Set rs = .Execute
    End With
    
    Set Me.frmPrinterOrdersub.Form.Recordset = rs
 
    rs.Close
    Set rs = Nothing
    Set cmd = Nothing

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

618 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