我来自"发现"形成一个"主要形式"。
步骤
代码:
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
在主窗体上加载记录后,用户可以从组合框中选择一个选项&#34; auto-search&#34;,然后该选项将执行驻留在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
记录已加载,到目前为止所有内容似乎都正常工作,如果有多个记录,用户可以在该结果集上来回导航。
当用户尝试使用&#34;查找&#34;使用组合框选项/功能后的表单。
步骤:
然后运行时错误&#39; 31&#39;出现了。
&#34;无法初始化数据提供者。&#34;
当我运行调试器时,我发现错误发生在&#34; Find&#34;表格代码:
Forms("Production").RecordSource = SQLString
我可以看到之前对存储过程的调用仍然是记录源值。
所以我尝试了这个:
Forms("Production").RecordSource = ""
Forms("Production").RecordSource = SQLString
即使那样&#34;清除&#34; {call stored procedure?}(类似的东西)的值仍然返回相同的错误。
我迷路了,我不知道如何纠正这个错误。任何建议都会很棒。也许我在思考太多了,而这很简单。
提前谢谢。
答案 0 :(得分: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
...