访问VBA:无法初始化数据提供者

问题描述 投票:1回答:1

从“查找”表单到“主表单”时,我收到此错误。

步骤是

  1. 打开主窗体
  2. 单击mani表单上的“查找”按钮,打开另一个名为“查找”的表单
  3. 点击“查找”表单上的“查找”按钮
  4. 在主窗体上加载记录

码:

Private Sub cmdFind_Click()

    Dim SQLString As String
    Dim TitleString As String
    Dim con         As ADODB.Connection
    Dim cmd         As ADODB.Command

    If production_batch_find.Form.RecordsetClone.RecordCount > 0 Then        

        SQLString = "sku IN (SELECT sku FROM production_batch WHERE [user] = '" & getUsr & "')"    

    Else        

        If IsNull(txtSKU.Value) = False Then            

            If Len(txtSKU.Value) < 9 Then                

                If IsNumeric(txtSKU.Value) Then                    
                    txtSKU.Value = "MN" & String(7 - Len(txtSKU.Value), "0") & txtSKU.Value                    
                Else                    
                    MsgBox "The SKU you entered isn't a valid product number.", vbCritical, "Invalid SKU"                    
                    Exit Sub                
                End If            

            End If

            SQLString = "sku = '" & txtSKU.Value & "'"

        Else

            If IsNull(txtTitle.Value) = False Then                

                TitleString = txtTitle.Value                

                Do
                    If InStr(TitleString, "  ") > 0 Then
                        TitleString = Replace(TitleString, "  ", " ")
                    Else
                        Exit Do
                    End If
                Loop

                If Left(TitleString, 6) = "like '" Or Left(TitleString, 6) = "like " & Chr(34) Then                    

                TitleString = Mid(TitleString, 7)                    

                    If Right(TitleString, 1) = Chr(34) Or Right(TitleString, 1) = Chr(39) Then
                        TitleString = Left(TitleString, Len(TitleString) - 1)
                    End If

                    SQLString = "title LIKE '" & InsertChr39(TitleString) & "' OR ID IN (SELECT ID FROM Variants WHERE Variant LIKE '" & InsertChr39(TitleString) & "')"

                Else

                    SQLString = "title = '" & InsertChr39(TitleString) & "' OR ID IN (SELECT ID FROM TitleVariants WHERE VariantTitle = '" & InsertChr39(TitleString) & "')"

                End If

            Else

                If IsNull(txtSongID.Value) = False Then
                    SQLString = "(ID = " & txtSongID.Value & ")"
                End If

            End If

            If IsNull(cmbName.Value) = False Then
                SQLString = SQLString & IIf(SQLString > "", " AND ", "") & "(songid IN (SELECT ID FROM Names WHERE Nameno  = " & cmbName.Value & "))"
            Else
                If IsNull(cmbPrimary.Value) = False Then
                    SQLString = SQLString & IIf(SQLString > "", " AND ", "") & "Primary = " & cmbPrimary.Value
                End If
            End If

            If IsNull(cmbStatusID.Value) = False Then
                SQLString = SQLString & IIf(SQLString > "", " AND ", "") & "StatusID = " & cmbStatusID.Value
            End If

            If IsNull(cmbPublisher.Value) = False Then
                SQLString = SQLString & IIf(SQLString > "", " AND ", "") & "PublisherID = " & cmbPublisher.Value
            End If

        End If

    End If

    If SQLString = "" Then        
        MsgBox "You haven't entered any search criteria", vbInformation, "No Search"
        Exit Sub    
    End If

    SQLString = "SELECT * FROM Production WHERE " & SQLString

    Forms("Production").RecordSource = SQLString

    DoCmd.Close acForm, "ProductionFind"

End Sub

一旦在主窗体上加载了记录,用户就可以从组合框“自动搜索”中选择一个选项,然后该选项将执行驻留在sql server上的存储过程,并将其输出用作其形式RecordSource 。

码:

Private Sub cmbAutoSearch_Click()

    Dim ADOCon As ADODB.Connection
    Dim ADOQD As ADODB.Command
    Dim ADORS As ADODB.Recordset
    Dim SearchSQL As String

    SearchSQL = ""

    Set ADOCon = New ADODB.Connection
    With ADOCon
        .ConnectionString = GetConnectionString("MNCatalog")
        .Open
    End With

    If cmbAutoSearch.Value = 101 Then    

        Set ADOQD = New ADODB.Command
        With ADOQD
            .ActiveConnection = ADOCon
            .ActiveConnection.CursorLocation = adUseClient
            .CommandType = adCmdStoredProc
            .CommandText = "[dbo].[mn_Production_Derived_Products_Hierarchy]"
            .Parameters.Append .CreateParameter("@sku", adVarChar, adParamInput, 10, SKU.Value)
        End With

        Set ADORS = New ADODB.Recordset
        With ADORS
            .CursorType = adOpenKeyset
            .CursorLocation = adUseClient
            Set ADORS = ADOQD.Execute()
            Set Me.Recordset = ADORS.Clone
        End With    

    End If

    Set ADOQD = Nothing
    Set ADORS = Nothing    
    ADOCon.Close: Set ADOCon = Nothing

    Exit Sub

End Sub

存储过程的输出在此行中分配:

Set Me.Recordset = ADORS.Clone

记录加载,到目前为止所有内容似乎都正常工作,如果有多个记录,用户可以在该结果集上来回导航。

当用户在使用组合框选项/功能后尝试使用“查找”表单时,会出现此问题。

脚步:

  1. 单击mani表单上的“查找”按钮,打开另一个名为“查找”的表单
  2. 点击“查找”表单上的“查找”按钮

然后出现运行时错误“31”。

“数据提供程序无法初始化。”

当我运行调试器时,我发现错误发生在“查找”表单代码中的这一行:

Forms("Production").RecordSource = SQLString

我可以看到之前对存储过程的调用仍然是记录源值。

所以我尝试了这个:

Forms("Production").RecordSource = ""
Forms("Production").RecordSource = SQLString

即使“清除”{call stored procedure?}(类似的东西)的值,仍然会返回相同的错误。

我迷路了,我不知道如何纠正这个错误。任何建议都会很棒。也许我的想法太多了,而这很简单。

先感谢您。

vba forms access-vba dataprovider
1个回答
1
投票

尝试将数据从ADO连接导入到本地临时表中,然后将recordsource属性设置为该数据。正如本older blog post所述

这样的事情可能会有所帮助:

dim t as tabledef, i as integer
for each t in currentdb.tabledefs
select case t.name
    case "myTempTable"
        currentdb.tabledefs.delete "mytemptable"
end select
next t

currentdb.execute "CREATE TABLE ..."

ADORS.MoveFirst
for i = 0 to ADORS.RecordCount -1
    currentdb.execute "INSERT INTO myTempTable " & _
        ADORS(i).Fields("FieldName1").Value & " as [FieldName1], " & _
        etc.
next i
...
© www.soinside.com 2019 - 2024. All rights reserved.