wiklendt
i recommend chocolate
- Local time
- Today, 13:28
- Joined
- Mar 10, 2008
- Messages
- 1,746
hi.
i was playing with some VBA code to pass start and end dates to a report, and was using what seemed like the stock standard code everyone seems to use, however, i wanted to add one more criteria to my SQL to pass to the report: person who made the order(s).
i noticed the stock code did not allow for two null dates, so i have added this part to the code, for anyone interested. the query, if not restricted by a filter, returns all records; and if restricted only by person it does not error.
(edit: and you could theoretically continue to add more criteria to this too...)
if there is an easier way to do this, then by all means jump in and correct me.
i was playing with some VBA code to pass start and end dates to a report, and was using what seemed like the stock standard code everyone seems to use, however, i wanted to add one more criteria to my SQL to pass to the report: person who made the order(s).
i noticed the stock code did not allow for two null dates, so i have added this part to the code, for anyone interested. the query, if not restricted by a filter, returns all records; and if restricted only by person it does not error.
(edit: and you could theoretically continue to add more criteria to this too...)
if there is an easier way to do this, then by all means jump in and correct me.
Code:
Private Sub cmdRunOrderReport_Click()
Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.
Const conDateFormat = "\#dd\/mm\/yyyy\#"
strReport = "rptOrders"
strField = "[OrderDate]"
' ================================================================================
' CHECK FOR DATE RANGE
' ================================================================================
If IsNull(Me.cmbDateStart) Then
If Not IsNull(Me.cmbDateEnd) Then 'End date, but no start.
strWhere = strField & " <= " & Format(Me.cmbDateEnd, conDateFormat)
[COLOR=Red] Else 'neither start nor end dates
strWhere = ""[/COLOR]
End If
Else
If IsNull(Me.cmbDateEnd) Then 'Start date, but no End.
strWhere = strField & " >= " & Format(Me.cmbDateStart, conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.cmbDateStart, conDateFormat) _
& " And " & Format(Me.cmbDateEnd, conDateFormat)
End If
End If
' ================================================================================
' CHECK FOR PERSON FILTER AND ADD TO SQL STATEMENT
' ================================================================================
If IsNull(Me.cmbPerson.Column(0)) Then
'No person to filter by, so make no changes to strWhere
Else
If strWhere = "" Then 'no date filter, just person filter
strWhere = "[PersonID] = " & Me.cmbPerson
Else 'date and person filter
strWhere = strWhere & "AND [PersonID] = " & Me.cmbPerson
End If
End If
' ================================================================================
' RUN THE REPORT
' ================================================================================
DoCmd.OpenReport strReport, acViewPreview, , strWhere, acDialog
End Sub
Attachments
Last edited: