如果您想使用 MS Access 的报告生成器创建报告,您必须使用查询对象(可能有一种方法可以诱使 MS Access 从您的记录集中运行它,但它可能不值得你努力)。
您可以在“数据库”窗口中创建查询对象。单击对象列表中的查询按钮,然后单击新建。在生成的编辑器中,您可以以图形方式创建查询,或者如果您更喜欢使用 SQL。保存查询并给它一个有意义的全名。
同样可以在“数据库”窗口中创建报告。单击报告按钮,然后单击新建。在生成的向导中,您会将报告链接到您刚刚创建的查询。
更新: 作为 D.W. Fenton 说,您可以将查询直接嵌入到报表对象中,而无需创建单独的查询对象。无论如何,我的偏好是创建一个。
这种方法的问题是您必须为每个表创建单独的查询和报告。
如果您只想将结果转储到文本文件(以便稍后读取/打印),那么您可以使用记录集来完成,就像在 VBA 代码中一样。它看起来像这样
'...
dim strFoo as string
dim strBar as string
'...
if not rs.bof then
rd.MoveFirst
end if
While Not rs.EOF
strFoo = rs("foo") 'copy the value in the field
'named "foo" into strFoo.
strBar = rs("bar")
'... etc. for all fields you want
'
'write out the values to a text file
'(I'll leave this an exercise for the reader)
'
rs.MoveNext
Wend
'...
解析所有表可以像这样在循环中完成:
dim strTableName as string
dim db As Database
'...
Set db = CurrentDb
db.TableDefs.Refresh
For Each myTable In db.TableDefs
If Len(myTable.Connect) > 0 Then
strTableName = myTable.Name
'...
'Do something with the table
'...
End If
Next
set db = nothing
========================更新==================== ===
可以从记录集中运行 MS-Access 报告。重复我对 tksy's question 所说的话
从Access Web,您可以使用记录集的“名称”属性。您生成的代码将如下所示:
在报告中
Private Sub Report_Open(Cancel As Integer)
Me.RecordSource = gMyRecordSet.Name
End Sub
在调用对象(模块、表单等)中
Public gMyRecordSet As Recordset
'...
Public Sub callMyReport()
'...
Set gMyRecordSet = CurrentDb.OpenRecordset("Select * " & _
"from foo " & _
"where bar='yaddah'")
DoCmd.OpenReport "myReport", acViewPreview
'...
gMyRecordSet.Close
Set gMyRecordSet = Nothing
'...
End Sub
Q.E.D.