首页 > 解决方案 > Microsoft Access - 使用多个组合框设置报告

问题描述

好的,我缺乏 VBA 方面的专业知识。我想使用 5 个组合框和 2 个文本框作为参数来查找并根据这些选择生成报告。它提取所有适用数据库条目的信息并将它们放入我单击命令按钮时生成的报告中。我有 2 个文本框设置为我从另一个站点拉出的从和到日期框。基本上我希望能够使用这个现有代码并为所有其他组合框添加信息。请让我知道这是否可能。我附上了附件以试图澄清这一点。

页面设置和简要说明:

Private Sub Submit_Click()

On Error GoTo Err_Handler      'Remove the single quote from start of this line once you have it working.
'Purpose:       Filter a report to a date range.
'Documentation: http://allenbrowne.com/casu-08.html
'Note:          Filter uses "less than the next day" in case the field has a time component.
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Do NOT change it to match your local settings.

'DO set the values in the next 3 lines.
strReport = "Search"            'Put your report name in these quotes.
strDateField = "[Date]"         'Put your field name in the square brackets in these quotes.
lngView = acViewReport          'Use acViewNormal to print instead of preview.

'Build the filter string.
If IsDate(Me.startdate) Then
    strWhere = "(" & strDateField & " >= " & Format(Me.startdate, strcJetDate) & ")"
End If
If IsDate(Me.enddate) Then
    If strWhere <> vbNullString Then
        strWhere = strWhere & " AND "
    End If
    strWhere = strWhere & "(" & strDateField & " < " & Format(Me.enddate + 1, strcJetDate) & ")"
End If

'Close the report if already open: otherwise it won't filter properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
    DoCmd.Close acReport, strReport
End If
'Open the report.
Debug.Print strWhere        'Remove the single quote from the start of this line for debugging purposes.
DoCmd.OpenReport strReport, lngView, , strWhere

Exit_Handler:
Exit Sub

Err_Handler:
If Err.Number <> 2501 Then
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open report"
End If
Resume Exit_Handler

End Sub

标签: ms-access

解决方案


推荐阅读