Share via

Open report between dates OR specific date

Anonymous
2012-01-11T13:33:55+00:00

Hello

I have a query that provides records for a report.

On the report is a date

I have a form that has a button to open the report

On the form are 3 unbound text boxes ( txtStartDate txtEndDate txtSpecificDate )

The OnChange event of txtStartDate and txtEndDate is txtSpecificDate=Null

The OnChange event of txtSpecificDate  is txtStartDate=Null and txtEndDate=Null

This way I can "either" put a start date and end date OR a specific date

What would be the VBA to open the report with EITHER a start and end  OR a specific date

I have tried lots of combinations and I am getting nowhere.

Thank you

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

HansV 462.6K Reputation points
2012-01-11T13:42:47+00:00

Try this:

Private Sub cmdOpenReport_Click()

    Dim strWhere As String

    If Not IsNull(Me.txtStartDate) And Not IsNull(Me.txtEndDate) Then

        strWhere = "[DateField] Between #" & Format(Me.txtStartDate, _

            "mm/dd/yyyy") & "# And #" & Format(Me.txtEndDate, _

            "mm/dd/yyyy") & "#"

    ElseIf Not IsNull(Me.txtSpecificDate) Then

        strWhere = "[DateField] = #" & Format(Me.txtSpecificDate, _

            "mm/dd/yyyy") & "#"

    Else

        MsgBox "Please fill in either start and end date or specific date"

        Exit Sub

    End If

    DoCmd.OpenReport ReportName:="rptMyReport", View:=acViewPreview, _

        WhereCondition:=strWhere

End Sub

Substitute the correct names for the command button (cmdOpenReport), the date/time field (DateField) and the report (rptMyReport).

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Anonymous
    2012-01-11T13:57:39+00:00

    Thank you

    Was this answer helpful?

    0 comments No comments