I am using some code in a form to filter a report. (I got the code from a website and have tried to amend it) I don't actually much of it. (I am not a developer)
It needs to filter by date range.
Code: Select all
Option Compare Database
Option Explicit
Private Sub cmdApplyFilter_Click()
Dim strFilter As String
Dim blnFilterOn As Boolean
Dim strCriteria As String
strCriteria = "Criteria: "
If Nz(Me.txtStartDate, vbNullString) <> vbNullString And Nz(Me.txtEndDate, vbNullString) <> vbNullString Then
If Me.txtStartDate > Me.txtEndDate Then
MsgBox "Start Date Cannot Be Greater Than End Date", vbInformation
Exit Sub
End If
End If
If Nz(Me.txtStartDate, vbNullString) <> vbNullString Then
strFilter = strFilter & " AND [Course Date] >= #" & Me.txtStartDate & "#"
strCriteria = strCriteria & vbCrLf & "Begin Date: " & Me.txtStartDate
End If
If Nz(Me.txtEndDate, vbNullString) <> vbNullString Then
strFilter = strFilter & " AND [Course Date] <= #" & Me.txtEndDate & "#"
strCriteria = strCriteria & vbCrLf & "End Date: " & Me.txtEndDate
End If
If strFilter <> vbNullString Then
strFilter = Mid(strFilter, 6)
Me.txtCriteria = strCriteria
Reports("RPT_TrainingRecordTime").Filter = strFilter
Reports("RPT_TrainingRecordTime").FilterOn = True
Else
MsgBox "Enter some criteria or print the report", vbInformation
End If
End Sub
Private Sub cmdClose_Click()
DoCmd.Close acReport, "RPT_TrainingRecordTime"
DoCmd.Close acForm, "FRM_TrainingTimeSelectionDate"
End Sub
Private Sub cmdRemoveFilter_Click()
Me.txtCriteria = "Criteria: None"
Reports("RPT_TrainingRecordTime").Filter = vbNullString
Reports("RPT_TrainingRecordTime").FilterOn = False
End Sub
Private Sub Form_Open(Cancel As Integer)
DoCmd.OpenReport "RPT_TrainingRecordTime", acPreview
End Sub
I can upload the access file if you want a look at it.