Question : Help with calling SQL Stored Procedure from Access

Hi all, this is my first attempt at calling a SQL stored procedure from Access to probably doing something really stupid.  Been trying to read up on how to call stored procedures and getting conflicting information so this is the best I could come up with:

        Dim db As Database
        Dim sqlExpr As String
        Dim rs As Recordset
       
        Set db = CurrentDb()
        sqlExpr = "PROCEDURE getWastePrice '" & Forms![frmOutgoing]![ShipDate].Value & "', " & HazWasteID
        MsgBox (sqlExpr)
        Set rs = db.OpenRecordset(sqlExpr, dbOpenSnapshot, dbSQLPassThrough)

The error message I'm getting is "Syntax error in PARAMETER clause"

Now I've inserted the MsgBox in there so I can see what the sqlExpr value is and it comes back as

PROCEDURE getWastePrice '02/02/2010', 118

Which is what I would expect.  So what's wrong with my syntax?

Open to other, better ways of calling stored procedures with parameters that return values.

Thanks.

Answer : Help with calling SQL Stored Procedure from Access

Well, if you paste the code below into a new module and then you are free to call it at will.
For example using:

   Dim qdf As DAO.QueryDef
   Dim rst As DAO.Recordset
   Dim strSQL As String

   strSQL = "EXEC getWastePrice '" & Forms![frmOutgoing]![ShipDate].Value & "', " & HazWasteID
   Set qdf = fCreatePassThrough("", strSQL, "YourDBName", "YourServerName", True)
   Set rst = qdf.OpenRecordset
   
   Debug.Print rst(0)     'Do whatever you need to here

   rst.Close
   Set rst = Nothing
   Set qdf = Nothing

See if that makes sense to you.

Cheers.

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
Function fCreatePassThrough(strName As String, strSQL As String, strDBname As String, _
                        strServer As String, Optional blnIntegratedSecurity As Boolean, _
                        Optional strUserName As String, Optional strPassword As String, _
                        Optional blnReturnsRecords As Boolean = True, Optional strDriver As String = "{SQL Server}") As DAO.QueryDef
    
    Dim db As Database
    Dim qdf As QueryDef
    Dim strConnect As String
    
    strConnect = "ODBC;Driver=" & strDriver & ";Server=" & strServer & ";Database=" & strDBname
    If blnIntegratedSecurity Then
        strConnect = strConnect & ";Trusted_Connection=Yes"
    Else
        If Len(strUserName) > 0 And Len(strPassword) > 0 Then
            strConnect = strConnect & ";UID=" & strUserName & ";PWD=" & strPassword
        End If
    End If
    
    Set db = CurrentDb
    Set qdf = db.CreateQueryDef(strName)

    With qdf
        .ReturnsRecords = blnReturnsRecords
        .Connect = strConnect
        .sql = strSQL
        .ODBCTimeout = 60
    End With
    Set fCreatePassThrough = qdf
    
    Set qdf = Nothing
    Set db = Nothing
    
End Function
Random Solutions  
 
programming4us programming4us