Good evening all.
I have a problem with my query filtering code. I can't figure out where the problem is and why it doesn't work.
Here is the code:
I also add the database
I have a problem with my query filtering code. I can't figure out where the problem is and why it doesn't work.
Here is the code:
Code:
Private Sub cmdReport_Click()
Dim reportName As String
Dim strQuery As String
Dim strDateField As String
Dim strWhere As String
Const strcJetDate = "\#mm\/dd\/yyyy\#" 'Do NOT change it to match your local settings.
reportName = "report"
strQuery = "Query1"
strDateField = "[DateTask]" 'Put your field name in the square brackets in these quotes.
lngView = acViewReport 'Use acViewNormal to print instead of preview.
'Build the filter string.
If IsDate(Me.txt_DateFrom) Then
strWhere = "(" & strDateField & " >= " & Format(Me.txt_DateFrom, strcJetDate) & ")"
Else
MsgBox "Please, inser date!", vbInformation, "Attention!"
Exit Sub
End If
If IsDate(Me.txt_DateTo) Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txt_DateTo + 1, strcJetDate) & ")"
Else
MsgBox "Please, inser date!", vbInformation, "Attention!"
Exit Sub
End If
If Trim(Me.cbo_Worker & "") <> "" Then
If strWhere <> vbNullString Then
strWhere = strWhere & " AND "
End If
strWhere = strWhere & "[Worker] = '" & Me.cbo_Worker & "'"
Else
MsgBox "Please, inser 'Worker'!", vbInformation, "Attention!"
Exit Sub
End If
' check if the strWhere has some value
If Trim(strWhere) = "" Then strWhere = "(1=1)"
'Close the report if already open: otherwise it won't filter properly.
If CurrentProject.AllReports(strReport).IsLoaded Then
DoCmd.Close acReport, strReport
End If
DoCmd.OpenReport reportName, acViewReport
End Sub
I also add the database