使用查询Access2007将参数传递给记录源

时间:2015-06-18 15:09:05

标签: ms-access access-vba ms-access-2007

我相对较新,我的任务是改善应用程序的导航。

我有一个用于打开报告的表单。 该报告的记录来源如下,

SELECT 
    Projects.Project_Number, 
    Projects.Customer, 
    Projects.End_User, 
    Projects.Engineering_Company, 
    [Merged Ship and Delivery Dates].Sched_Next_Delivery_Date, 
    [Merged Ship and Delivery Dates].Sched_Next_Ship_Date, 
    Projects.QC_Pack_Req, 
    Projects.Target_QC_Pack_Date, 
    Projects.Invoice_QC_Pack 
FROM 
    Projects LEFT JOIN [Merged Ship and Delivery Dates] 
    ON Projects.Project_Number = [Merged Ship and Delivery Dates].Project_Number 
WHERE 
    (((Projects.Project_Number = [Project Number] ))) 
ORDER BY Projects.Project_Number;

我试图让报告在没有提示我的情况下打开。在某些情况下,我需要刷新报表或从其他表单中打开报表。 我试过用

DoCmd.OpenReport "Project Control Sheet", _ 
                acViewReport, , _ 
                "[Project_Number]=" & Me.ProjectNumber, , _
                "[Project_Number]=" & Me.ProjectNumber

仍然无法将参数传递给记录源。反正我是否将参数传递给recordsource(查询)?

我尝试过使用

  

窗体![窗体名称] ProjectNumber

where语句中,但这仅适用于单个表单,我还有其他表单可以打开此报告。

刷新表单的目的是允许用户在更新报表后查看对报表所做的更改。

1 个答案:

答案 0 :(得分:1)

如果从报告的“记录源”查询中删除WHERE子句,则可以稍后使用DoCmd.OpenReport方法的 WhereCondition 选项过滤它返回的行。

Dim strWhereCondition As String
strWhereCondition = "[Project_Number]=" & Me.ProjectNumber.Value
Debug.Print strWhereCondition ' <- view this in Immediate window; Ctrl+g will take you there
'DoCmd.OpenReport "Project Control Sheet", acViewReport, , strWhereCondition
DoCmd.OpenReport ReportName:="Project Control Sheet", _
    View:=acViewReport, WhereCondition:=strWhereCondition

如果您需要不同的方法,可以使用 TempVar ,因为您的Access版本是2007。

TempVars.Add "ProjectNumber", Me.ProjectNumber.Value

并更改Record Source查询以使用 TempVar ...

WHERE Projects.Project_Number = [TempVars]![ProjectNumber]