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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to execute stored procedure from Access?

Status
Not open for further replies.

Mayo123

Technical User
Apr 23, 2005
58
US
I have created a stored procedure with parameter in SQL server 2000. How can I execute it from Access.
Thanks for your help!
 
If the parameter is an 'in' only, then the quick way is to use a pass-thru query. If you are returning a value from the parameter, then you'll need to look into ADO, using the command object.
--Jim
 
Tell us what you want to do. Bring the results back to a recordset in vba code, bind the results to a Form, display the results in a Report???
 
Based on user's input(ssn),the related record would be shown on a form. How can I pass this ssn to my stored procedure?
Thanks for your help!
 
Not elegant, but it has the virtue of working.
Attach this code to a button on your form.
Code:
Private Sub MakePassThru()
Dim MyQuery    As QueryDef
Dim ConnectStr As String
Dim SqlCmd     As String
Dim SSNParm    As String

    On Error Resume Next
    DoCmd.DeleteObject acQuery, "YourQueryNameHere"
    SSNParm = Me!YourFormFieldWithTheSSN
'
    On Error GoTo MakePassThru_Err
'
    Set MyQuery = CurrentDb.CreateQueryDef("YourQueryNameHere")
    ConnectStr = "ODBC;DSN=YourDSN;UID=YourUserID;PWD='YourPassword';LANGUAGE=us_english;DATABASE=YourDatabase"
    SqlCmd = "EXECUTE YourProcedureName '" & SSNParm & "'"
    
    With MyQuery
        .Connect = ConnectStr
        .SQL = SqlCmd 
        .ODBCTimeout = 0 
        .ReturnsRecords = True 
    End With
    MyQuery.Close
'
MakePassThru_Exit:
    Exit Sub
MakePassThru_Err:
    MsgBox Err.Description
    Resume MakePassThru_Exit
This will create a pass-thru query with the required parameter.
You can then bind the query as the datasource for a form or report.
 
Set oRst = CurrentProject.Connection.Execute("StoredProcName(" & me.ssn & ")")

Do whatever you like with the resulting recordset.

The line of code assumes the current user has privileges to execute the procedure and that ssn is a number.

HTH


[pipe]
Daniel Vlas
Systems Consultant

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top