从表单发送多个参数以报告查询

时间:2015-06-15 21:50:54

标签: vba ms-access ms-access-2010

尝试将三个变量(startDate,endDate,division)从选择器表单发送到报表查询。通过doCommand.OpenReport命令发送三个变量,然后在报告的Load事件中解压缩它们。我不确定在报告中将它们分配给查询以在查询中使用它们。

表格形式:

Private Sub btn_bud_sum_exp_div_Click()
    Dim StrWhereCondition
    Dim a As String
    Dim b As String
    Dim c As String

    a = Me.txtStartDate.Value
    b = Me.txtEndDate.Value
    c = Me.lstDivision.Value

    StrWhereCondition = a & "|" & b & "|" & c

    'StrWhereCondition = "[accounting start date] = " & Me.txtStartDate.Text

    DoCmd.OpenReport "FY15 Budget Line Sum - Expenditures - Div", , ,    StrWhereCondition
End Sub

在报告的加载事件中:

Private Sub Report_Load()
    Dim dtStartDate As Date
    Dim dtEndDate As Date
    Dim strDivision As String

    If Not IsNull(Me.OpenArgs) Then
        dtStartDate = parsetext(OpenArgs, 0)
        dtEndDate = parsetext(OpenArgs, 1)
        strDivision = parsetext(OpenArgs, 2)
    End If
End Sub

在报告查询中:

SELECT 
    * 
FROM 
    budget b
WHERE 
    (b.start_date between dtStartDate AND dtEndDate) AND d.division = strDivision

1 个答案:

答案 0 :(得分:0)

如果您使用过滤字符串打开报告,则可以删除报告中的所有OnLoad内容:

Private Sub btn_bud_sum_exp_div_Click()

    Dim StartDate      As String
    Dim EndDate        As String
    Dim Division       As String
    Dim WhereCondition As String

    StartDate = Format(Me!txtStartDate.Value, "yyyy\/mm\/dd")
    EndDate = Format(Me!txtEndDate.Value, "yyyy\/mm\/dd")
    Division = Me!lstDivision.Value

    WhereCondition = "start_date between #" & StartDate & "# and #" & EndDate & "# and division = " & Division & ""
    ' or:
    ' WhereCondition = "[accounting start date] between #" & StartDate & "# and #" & EndDate & "# and division = " & Division & ""
    ' or, if division is text:
    ' WhereCondition = "start_date between #" & StartDate & "# and #" & EndDate & "# and division = '" & Division & "'"

    DoCmd.OpenReport "FY15 Budget Line Sum - Expenditures - Div", , , WhereCondition

End Sub