Bind Access form to the results from a Stored Procedure

后端 未结 3 1860
失恋的感觉
失恋的感觉 2021-01-05 15:32

I am trying to return the results of a stored procedure to a form. I have managed to iterate thru the results using an ADO recordset, but cannot bind the results to the form

相关标签:
3条回答
  • 2021-01-05 15:47

    Okay, I have tested this example. It includes changes to suit my set-up which I have left in, rather than guessing at your set-up. Most of this is taken from http://support.microsoft.com/kb/281998/EN-US/

    Dim cn As New ADODB.Connection
    Dim cmd As New ADODB.Command
    Dim param1  As New ADODB.Parameter
    
        With cn
            .Provider = "Microsoft.Access.OLEDB.10.0"
            .Properties("Data Provider").Value = "SQLOLEDB"
            .Properties("Data Source").Value = "Server"
            .Properties("Integrated Security").Value = "SSPI"
            .Properties("Initial Catalog").Value = "Test"
            .Open
        End With
    
        txtSiteID_Search = 1
    
        If Nz(txtSiteID_Search, vbNullString) <> vbNullString Then
            Set param1 = cmd.CreateParameter("@SiteID", adBigInt, adParamInput)
            param1.Value = txtSiteID_Search
            cmd.Parameters.Append param1
        End If
    
        With cmd
            .ActiveConnection = cn
            .CommandText = "spSiteInformation_Retrieve"
            .CommandType = adCmdStoredProc
            Set Me.Recordset = .Execute
        End With
    
    0 讨论(0)
  • 2021-01-05 15:49

    You need to use Set whenever you assign an object reference in VBA.

    Change Me.Recordset = .Execute to Set Me.Recordset = .Execute.

    Also, you probably need to open it with a supported cursor type. I don't think there's a way to change the cursor type if you use the Execute method on the Command object. You'll have to create the Recordset separately.

    Set rs = New ADODB.Recordset
    rs.Open cmd, , adOpenKeyset
    Set Me.Recordset = rs
    
    0 讨论(0)
  • 2021-01-05 15:52

    Forget ADO. Create a passthru query in Access, with property ReturnsRecords = True.
    Bind your form to that passthru query.
    Using VBA, change the .SQL property of that QueryDef object, then open the form. You're done.

    Set qry = CurrentDb.QueryDefs("myQryDef")
    qry.SQL = "exec spMyStoredProc " & "'argument1'"
    
    0 讨论(0)
提交回复
热议问题