我正在使用NEXTRECORDSET
方法查看从SQL存储过程生成的记录。 SP获取用户输入并在SQL数据库中搜索3个表,并将包含值的记录返回给Access。我能够在Access的调试(debug.print
)窗口中成功查看结果,但不能在Access表单的列表框lstResults1
或所有3个列表框中查看结果。调用函数和函数本身的表单过程如下。通过用函数中的lstResults1
语句替换每个Recordset的循环,我能够成功地将结果集传递给表单中的列表框(lstResults2
,rstCompound
等)(请参阅第三个代码示例)但它不是那么干净,每当我搜索的值中的一个在第二个或第三个表中时,我得到一个“对象变量或块变量未设置”:
程序
Private Sub cmdRun_Click()
'On Error Resume Next
Dim strSQL As String
'Stored procedure + parameters called from form
strSQL = "Exec spSQL_SearchDatabase " & "'" & Me.txtTables & "'" & _
", " & "'%" & Me.txtSearchTerm & "%'"
OpenMyRecordset rstCompound, strSQL
Set Me.lstResults1.Recordset = rstCompound
'debug - view procedure
Me.lblQuery.Caption = strSQL
Me.Repaint
End Sub
功能
Public Function OpenMyRecordset(rstCompound As ADODB.Recordset, strSQL As String, _
Optional rrCursor As rrCursorType, _
Optional rrLock As rrLockType, Optional bolClientSide As Boolean) As ADODB.Recordset
If con.STATE = adStateClosed Then
con.ConnectionString = "ODBC;Driver={SQL Server};Server=vnysql;DSN=RecordsMgmt_SQLDB;UID=DMP;Trusted_Connection=Yes;DATABASE=RecordsManagementDB;"
con.Open
End If
Set rstCompound = New ADODB.Recordset
With rstCompound
.ActiveConnection = con
.CursorLocation = adUseClient
.CursorType = IIf((rrCursor = 0), adOpenDynamic, rrCursor)
.LockType = IIf((rrLock = 0), adLockOptimistic, rrLock)
.Open strSQL
End With
' Display results from each recordset
intCount = 1
Do Until rstCompound Is Nothing
Debug.Print "Contents of recordset #" & intCount
Do Until rstCompound.EOF
Debug.Print rstCompound.Fields(0), rstCompound.Fields(1)
rstCompound.MoveNext
Loop
Set rstCompound = rstCompound.NextRecordset
intCount = intCount + 1
Loop
End Function
功能中的替代声明
Set rs1 = New ADODB.Recordset
With rs1
.ActiveConnection = con
.CursorLocation = adUseClient
.CursorType = IIf((rrCursor = 0), adOpenDynamic, rrCursor)
.LockType = IIf((rrLock = 0), adLockOptimistic, rrLock)
.Open strSQL
End With
Do Until rs1.EOF
Debug.Print rs1.Fields(0), rs1.Fields(1)
rs1.MoveNext
Loop
Set rs2 = rs1.NextRecordset
Do Until rs1.EOF
Debug.Print rs2.Fields(0), rs2.Fields(1)
rs2.MoveNext
Loop
Set rs3 = rs2.NextRecordset
Do Until rs3.EOF
Debug.Print rs3.Fields(0), rs3.Fields(1)
rs3.MoveNext
Loop
答案 0 :(得分:1)
函数OpenMyRecordset从不设置要返回的内容。它需要像
这样的东西Set OpenMyRecordset = rstCompound
此外,对于所有调试语句,rstCompound可能处于EOF且无法显示。