Ms access Access:如何执行查询并将其结果保存在报告中

Ms access Access:如何执行查询并将其结果保存在报告中,ms-access,Ms Access,嗨 我正在尝试用vba编写一个查询,并将其结果保存在报告中。 我是初学者。这就是我尝试过的 有人能纠正我吗 Dim cn As New ADODB.Connection, rs As New ADODB.Recordset Dim sql As String Set cn = CurrentProject.Connection sql = "Select * from table1 where empno is 0" rs.Open sql, cn While Not rs.EOF '

我正在尝试用vba编写一个查询,并将其结果保存在报告中。 我是初学者。这就是我尝试过的 有人能纠正我吗

Dim cn As New ADODB.Connection, rs As New ADODB.Recordset
Dim sql As String

Set cn = CurrentProject.Connection
sql = "Select * from table1 where empno is 0"

rs.Open sql, cn


While Not rs.EOF

' here i think i should save the result in a report but i am not sure how

  rs.MoveNext
Wend
rs.Close
cn.Close
Set rs = Nothing
Set cn = Nothing

另外,如何更改此查询以在数据库中的所有表上运行此查询通常您将基于数据源设计报告。然后,在完成报告并正常工作后,使用VBA显示或保存报告。

要对数据库中的每个表运行此操作,我建议编写一个循环通过CurrentData.AllTablesi的函数,然后在每次迭代中调用上面的函数

希望这对您有所帮助

如果您想使用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报告。重复我对他说的话 从中,可以使用记录集的name属性。生成的代码如下所示:

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
在报告中

在调用对象模块、窗体等中


Q.E.D.

如果只想查看结果,可以创建查询。例如,以下是一些粗略的、大部分未经测试的VBA:

Sub ViewMySQL
Dim strSQL as String
Dim strName As String

'Note that this is not sensible in that you
'will end up with as many queries open as there are tables

    For Each tdf In CurrentDB.TableDefs
       If Left(tdf.Name,4)<>"Msys" Then
          strName = "tmp" & tdf.Name

          strSQL = "Select * from [" & tdf.Name & "] where empno = 0"
          UpdateQuery strName, strSQL
          DoCmd.OpenQuery strName, acViewNormal
       End If
    Next
End Sub

Function UpdateQuery(QueryName, SQL)

    If IsNull(DLookup("Name", "MsysObjects", "Name='" & QueryName & "'")) Then
        CurrentDb.CreateQueryDef QueryName, SQL
    Else
        CurrentDb.QueryDefs(QueryName).SQL = SQL
    End If

    UpdateQuery = True

End Function
您可能还对MDB Doc感兴趣,它是Microsoft Access 97-2003的一个外接程序,允许您将Access数据库中的对象/属性记录到HTML文件中


-我不完全清楚你想做什么。如果您想查看SQL语句的结果,您需要创建一个表单,并将其recordsource设置为从表1中选择*,其中empno为0。然后您可以一次查看一条记录的结果

如果这不是你想要的,那么我恐怕没有足够的信息来回答你的问题

从您到目前为止所说的,我看不出您需要VBA或报表的任何原因,因为您只想查看数据。报告用于打印,表单用于查看和编辑。报表是面向页面的,不容易导航,而表单是面向记录的,允许您编辑数据(如果需要)


更多关于你想要完成什么的信息将帮助我们给你更好的答案。

有同样的问题。只需使用剪贴板

通过单击/拖动显示的所有字段名称来选择查询结果 按ctrl-c复制到windows剪贴板 在word中打开一个空白文档,然后在其中单击 按ctrl-v从剪贴板粘贴。
你说的报告是什么意思?您是指HTML还是word文档?运行报表不需要保存查询。报表定义具有与报表一起存储的Recordsource属性。在打开报表之前,无需创建记录集。只需将OpenRecordset中使用的SQL字符串指定为Recordsource属性。我认为您编写的代码无法工作,因为您正在将代码中打开的记录集的名称指定给Recordsource属性。
Sub ViewMySQL
Dim strSQL as String
Dim strName As String

'Note that this is not sensible in that you
'will end up with as many queries open as there are tables

    For Each tdf In CurrentDB.TableDefs
       If Left(tdf.Name,4)<>"Msys" Then
          strName = "tmp" & tdf.Name

          strSQL = "Select * from [" & tdf.Name & "] where empno = 0"
          UpdateQuery strName, strSQL
          DoCmd.OpenQuery strName, acViewNormal
       End If
    Next
End Sub

Function UpdateQuery(QueryName, SQL)

    If IsNull(DLookup("Name", "MsysObjects", "Name='" & QueryName & "'")) Then
        CurrentDb.CreateQueryDef QueryName, SQL
    Else
        CurrentDb.QueryDefs(QueryName).SQL = SQL
    End If

    UpdateQuery = True

End Function