我正在尝试生成类似于交叉表的报告。数据来自已过滤的表单(日期和 WorkerID (字符串))。
在下一行:
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
我遇到了错误:
错误3265在此集合中找不到项目
我在做什么错了?
Private Sub Report_Open(Cancel As Integer)
' Create underlying recordset for report using criteria
Dim intX As Integer
Dim qdf As QueryDef
Dim frm As Form
' Don't open report if frmReg form isn't loaded.
If Not (IsLoaded("frmReg")) Then
Cancel = True
MsgBox "To preview or print this report, you must open " _
& "frmReg in Form view.", vbExclamation, _
"Must Open Dialog Box"
Exit Sub
End If
' Set database variable to current database.
Set dbsReport = CurrentDb
Set frm = Forms!frmReg
' Open QueryDef object.
' Set qdf = dbsReport.QueryDefs("ReportReg")
Me.RecordSource = "SELECT * FROM [tReg]"
Set qdf = dbsReport.QueryDefs(Me.RecordSource)
' Open Recordset object.
Set rstReport = qdf.OpenRecordset()
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
End Sub
答案 0 :(得分:1)
问题似乎出在SQL与命令之间的关系问题上,您可能没有查询设置来获取您要查找的信息。
尝试一下:
sSQL = "SELECT * FROM [tReg]"
Me.RecordSource = sSQL
Set qdf = dbsReport.CreateQueryDef("NewQuery", sSQL)
'This will purge the query after your inteactions are complete
dbsReport.QueryDefs.Delete "NewQuery"
注意:这将不包括与QueryDef
的任何交互。
答案 1 :(得分:0)
QueryDefs集合采用已保存的命名查询而不是SQL语句。正如@ Jiggles32演示的那样,您需要创建一个命名查询,然后通过QueryDefs()
调用来引用它。
但是,您可以通过直接使用OpenRecordset()直接打开记录集来绕过查询的使用,这是您需要的最终结果:
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = dbsReport.OpenRecordset(strSQL)
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count
实际上,您可以使用RecordsetClone属性直接提取表单的记录集(如果运行各种操作而不影响表单的实际记录,则优先使用Recordset):
strSQL = "SELECT * FROM [tReg]"
Me.RecordSource = strSQL
Set rstReport = Me.RecordsetClone
' Set a variable to hold number of columns in crosstab query.
intColumnCount = rstReport.Fields.Count