使用VBA在Access中生成查询报告

时间:2017-01-20 17:01:54

标签: vba ms-access

我正在使用Microsoft Access。我有一个SQL查询。 Select * from people

我有当前代码连接到表单按钮。我如何修复我的代码以正确生成报告?

DoCmd.OpenQuery "PeopleSearch", acViewPreview 'open query
DoCmd.SelectObject acQuery, "PeopleSearch" 
RunCommand acGenerateReport

1 个答案:

答案 0 :(得分:2)

你真的很亲密。这是一个如何完成的例子。此外,我在报告生成结束时关闭了查询。

Private Sub Command24_Click()
On Error GoTo Err_Command24_Click

    DoCmd.OpenQuery "PeopleSearch", acViewPreview
    DoCmd.SelectObject acQuery, "PeopleSearch"
    RunCommand acCmdNewObjectAutoReport
    DoCmd.Close acQuery, "PeopleSearch"

Exit_Command24_Click:
    Exit Sub

Err_Command24_Click:
    MsgBox Err.Description
    Resume Exit_Command24_Click

End Sub