1

我已经查看了下面详细介绍的您之前的示例。但是我有一个问题:

这是我的代码,它在名为 Reports 的表单上运行一个按钮:

Dim dbs As Database

    Dim qdf As QueryDef
    Dim varitem As Variant

    Set dbs = CurrentDb()

    Set qdf = dbs.QueryDefs("Qry_rpt_cr")
    qdf.Parameters(0) = Forms!frm_reports.rmselectfilter.Column(1, varitem)
    qdf.Parameters(4) = Forms!frm_reports.rmselectperiod.Column(0, 0)
    qdf.Parameters(3) = Forms!frm_reports.rmselectperiod.Column(0, 1)
    qdf.Parameters(2) = Forms!frm_reports.rmselectperiod.Column(0, 2)
    qdf.Parameters(1) = Forms!frm_reports.rmselectperiod.Column(0, 3)

    Set grst = CurrentDb.OpenRecordset("Select * from Qry_rpt_cr")

    DoCmd.OpenReport "rpt_cr_test", acPreview

    grst.Close

    Set grst = Nothing
End Sub

问题是查询需要五个参数传递给它,然后使用定义的参数打开记录集,然后打开报表。但是代码打不开。它在这条线上说一个错误 Set grst = CurrentDb.OpenRecordset("Select * from Qry_rpt_cr") 要求 5 个参数,但我已经在代码的前面传递了它们。任何建议都会受到欢迎。很高兴为正确答案捐款。ED

来自您的档案的示例 在 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
4

1 回答 1

1

I've not used Access for a while , but i think you are setting the params for a query, and then running a different query. Access is asking for params to be provided for the query you are asking ("select * from ..."), not the (named) query that your query references, if that makes sense.

This should be easily fixed, just run OpenRecordset from the QueryDef:

Set grst = qdf.OpenRecordSet

and then Access will include the query's params correctly.

Edit: Thx Remou

于 2013-01-14T11:55:33.487 回答