我使用MS Access 2016获得此MS Access VBA代码。
Private Sub cmdPreview_Click()
On Error GoTo Err_Handler
Dim strReport As String
Dim strDateField As String
Dim strWhere As String
Dim lngView As Long
Const strcJetDate = "\#mm\/dd\/yyyy\#"
strReport = "Sales Report V2"
strDateField = "[OrderDate]"
lngView = acViewReport
If IsDate(Me.txtStartDate) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
End If
If IsDate(Me.txtEndDate) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
End If
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
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
用于使日期范围表单工作的代码。表单允许您选择开始日期和结束日期。然后单击调用上述代码的按钮,并在新选项卡中生成报告。我想要实现的目标,但到目前为止还没有弄清楚。是如何使报表显示在包含日期范围控件的窗体的子窗体/子报表中,然后从中有一个按钮单击以在新选项卡或打印中打开生成的报表,或者如果用户满意则显示随着选择。
的教程答案 0 :(得分:0)
您可以设置随附报告的Filter
属性:
Me!NameOfYourSubreportControl.Report.Filter = strWhere
Me!NameOfYourSubreportControl.Report.FilterOn = True
要以正常方式打开报告,请使用现有代码。