Access VBA: 无法初始化数据提供程序
Access VBA : Data provider could not be initialized
从 "find" 表单转换为 "main form" 时出现此错误。
步骤是
- 打开主窗体
- 单击 mani 表单上的 "Find" 按钮,这会打开另一个名为 "Find"
的表单
- 单击 "Find" 表单上的 "Find" 按钮
- 在主窗体加载记录
代码:
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
在主窗体上加载记录后,用户可以 select 来自组合框的选项 "auto-search",然后该选项将执行驻留在 sql 服务器,并将其输出用作其形式 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
记录已加载,到目前为止一切似乎都运行良好,如果有多个记录,用户可以在该结果集上来回导航。
当用户在使用组合框选项/功能后尝试使用 "Find" 表单时会出现问题。
步骤:
- 单击 mani 表单上的 "Find" 按钮,这会打开另一个名为 "Find"
的表单
- 单击 "Find" 表单上的 "Find" 按钮
然后 运行-时间错误“31”出现。
"Data provider could not be initialized."
当我 运行 调试器时,我看到错误发生在 "Find" 表单代码的这一行:
Forms("Production").RecordSource = SQLString
我可以看到之前调用存储过程仍然是作为记录源值。
所以我尝试了这个:
Forms("Production").RecordSource = ""
Forms("Production").RecordSource = SQLString
即使 "clears" {call stored procedure ?} 的值(类似的东西),仍然 returns 同样的错误。
我很迷茫,我不知道如何纠正这个错误。任何建议都会很棒。可能是我想多了,简单的解决了。
提前致谢。
尝试将数据从 ADO 连接导入到本地临时文件 table,然后将记录源 属性 设置为该数据。如本文所述 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
...
从 "find" 表单转换为 "main form" 时出现此错误。
步骤是
- 打开主窗体
- 单击 mani 表单上的 "Find" 按钮,这会打开另一个名为 "Find" 的表单
- 单击 "Find" 表单上的 "Find" 按钮
- 在主窗体加载记录
代码:
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
在主窗体上加载记录后,用户可以 select 来自组合框的选项 "auto-search",然后该选项将执行驻留在 sql 服务器,并将其输出用作其形式 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
记录已加载,到目前为止一切似乎都运行良好,如果有多个记录,用户可以在该结果集上来回导航。
当用户在使用组合框选项/功能后尝试使用 "Find" 表单时会出现问题。
步骤:
- 单击 mani 表单上的 "Find" 按钮,这会打开另一个名为 "Find" 的表单
- 单击 "Find" 表单上的 "Find" 按钮
然后 运行-时间错误“31”出现。
"Data provider could not be initialized."
当我 运行 调试器时,我看到错误发生在 "Find" 表单代码的这一行:
Forms("Production").RecordSource = SQLString
我可以看到之前调用存储过程仍然是作为记录源值。
所以我尝试了这个:
Forms("Production").RecordSource = ""
Forms("Production").RecordSource = SQLString
即使 "clears" {call stored procedure ?} 的值(类似的东西),仍然 returns 同样的错误。
我很迷茫,我不知道如何纠正这个错误。任何建议都会很棒。可能是我想多了,简单的解决了。
提前致谢。
尝试将数据从 ADO 连接导入到本地临时文件 table,然后将记录源 属性 设置为该数据。如本文所述 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
...