Re: Access Report - VB code for OnOpen - date range?



Got it:

code:
I finally got the code working. It is all in the OnClick of the form
button, to open the appropriate report with the appropriate criteria.

I will post it here, in the hopes that it may help others:


Code:

Private Sub cmdOpenReport_Click()
On Error GoTo ErrorHandler
Dim Msg, Style, Title
Dim gstrReportName As String
Dim gstrWhere As String

Select Case Forms![frmReports]![GrpReportType]
Case 1
gstrReportName = "rptExclusions"

Case 2
gstrReportName = "rptObjections"

Case 3
gstrReportName = "rptNoForwardingAddress"

Case 4
gstrReportName = "rptUpdatedAddress"

Case 5
gstrReportName = "rptCommentsQuestions"

End Select

Select Case Forms![frmReports]![GrpReportDate]

Case 1 'today
gstrWhere = "ActivityDate = #" & Date & "#"

Case 2 'a particular date
gstrWhere = "ActivityDate =#" & Me.txtFromChoose & "#"

Case 3 ' date range
gstrWhere = "ActivityDate between #" & Me.txtFromChoose & "# and
#" & Me.txtTo & "#"

Case 4 'all dates
gstrWhere = ""

End Select


If GrpReportDate = 2 And Me.txtFromChoose.Value < 0 Then
Msg = "You must enter a date in the Choose Date field!"
Style = vbOKOnly
Title = "Date Must be Entered!"
ElseIf GrpReportDate = 3 And (Me.txtFromChoose.Value < 0 Or
Me.txtTo.Value < 0) Then
Msg = "You must enter a start AND end date!"
Style = vbOKOnly
Title = "Dates Must be Entered!"
Else

DoCmd.OpenReport gstrReportName, acViewPreview, , gstrWhere

End If
ExitHandler:
Exit Sub

ErrorHandler:
If Err = 2501 Then
Resume ExitHandler

Else
MsgBox Err.Description
Resume ExitHandler

End If
' msgbox Err.Description
' Resume Exit_cmdPreviewRpt_Click
End Sub

.