Greetings, I am working on my second Database and need help with this query. The query is attached to a Filter Form with a Hidden_Box, which allows the user to select a single or multiple states from a List Box, then choose a beginning and ending date range from a popup calendar.
The filter, however, is not providing the correct results. It will locate the States, but include all records from those states without regard to the date range.
I am new to Access and VBA, (as you can tell from the code) but I am trying. Any help and guidance would be appreciated, thanks.
Here is the code---------------->
Private Sub Filter_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim IFNo As Byte
stDocName = "RptInformation" 'Name of Report
On Error GoTo PrintSelectedRecords_Err
If IsNull(Me.txtFirstDate) Then
Me.txtFirstDate.SetFocus
MsgBox "Please enter a beginning date", , "Need beginning date"
Exit Sub
End If
If IsNull(Me.txtSecondDate) Then
Me.txtSecondDate.SetFocus
MsgBox "Please enter an ending date", , "Need ending date"
Exit Sub
End If
'Tried using the following with poor results
'stLinkCriteria = "(" & Me![Hidden_Box] _
'& "
AND (cDate[Arrival_Date]" _
'& " Between #" & Me![txtFirstDate] _
'& "# And #" & Me![txtSecondDate] & "#)"
'Moved to the below code upon suggestion - still poor results.
If Len(Me.Hidden_Box) > 0 Then
stLinkCriteria = Me!Hidden_Box & " AND "
Else
stLinkCriteria = ""
End If
stLinkCriteria = stLinkCriteria & "cDate[Arrival_Date] >= #" & Me![txtFirstDate] & _
"# AND cDate[Arrival_Date] <= #" & Me![txtSecondDate] & "#"
Debug.Print stLinkCriteria ' used to check my query
MsgBox stLinkCriteria
DoCmd.OpenReport "RptInformation", acPreview, , stLinkCriteria
DoCmd.Close acForm, "FrmState_Filter"
PrintSelectedRecords_Exit:
Exit Sub
PrintSelectedRecords_Err:
Dim Answer As Byte
Answer = MsgBox("It appears your " & _
"Query Request was not processed. Would you like to re-try " & _
"your filter request?", vbInformation + vbYesNo + _
vbDefaultButton1, "Query Not Completed"
If Answer = vbYes Then
Dim DocName As String
Dim LinkCriteria As String
DocName = "FrmVehicle_Information"
DoCmd.OpenForm DocName, , , LinkCriteria
DoCmd.Restore
Else
If Answer = vbNo Then
DoCmd.Close acForm, "FrmState_Filter"
Dim DName As String
Dim LCriteria As String
DName = "FrmVehicle_Information"
DoCmd.OpenForm DName, , , LCriteria
DoCmd.Maximize
End If
DoCmd.Close acForm, "FrmState_Filter"
Resume PrintSelectedRecords_Exit
End If
End Sub
The filter, however, is not providing the correct results. It will locate the States, but include all records from those states without regard to the date range.
I am new to Access and VBA, (as you can tell from the code) but I am trying. Any help and guidance would be appreciated, thanks.
Here is the code---------------->
Private Sub Filter_Click()
Dim stDocName As String
Dim stLinkCriteria As String
Dim IFNo As Byte
stDocName = "RptInformation" 'Name of Report
On Error GoTo PrintSelectedRecords_Err
If IsNull(Me.txtFirstDate) Then
Me.txtFirstDate.SetFocus
MsgBox "Please enter a beginning date", , "Need beginning date"
Exit Sub
End If
If IsNull(Me.txtSecondDate) Then
Me.txtSecondDate.SetFocus
MsgBox "Please enter an ending date", , "Need ending date"
Exit Sub
End If
'Tried using the following with poor results
'stLinkCriteria = "(" & Me![Hidden_Box] _
'& "
'& " Between #" & Me![txtFirstDate] _
'& "# And #" & Me![txtSecondDate] & "#)"
'Moved to the below code upon suggestion - still poor results.
If Len(Me.Hidden_Box) > 0 Then
stLinkCriteria = Me!Hidden_Box & " AND "
Else
stLinkCriteria = ""
End If
stLinkCriteria = stLinkCriteria & "cDate[Arrival_Date] >= #" & Me![txtFirstDate] & _
"# AND cDate[Arrival_Date] <= #" & Me![txtSecondDate] & "#"
Debug.Print stLinkCriteria ' used to check my query
MsgBox stLinkCriteria
DoCmd.OpenReport "RptInformation", acPreview, , stLinkCriteria
DoCmd.Close acForm, "FrmState_Filter"
PrintSelectedRecords_Exit:
Exit Sub
PrintSelectedRecords_Err:
Dim Answer As Byte
Answer = MsgBox("It appears your " & _
"Query Request was not processed. Would you like to re-try " & _
"your filter request?", vbInformation + vbYesNo + _
vbDefaultButton1, "Query Not Completed"
If Answer = vbYes Then
Dim DocName As String
Dim LinkCriteria As String
DocName = "FrmVehicle_Information"
DoCmd.OpenForm DocName, , , LinkCriteria
DoCmd.Restore
Else
If Answer = vbNo Then
DoCmd.Close acForm, "FrmState_Filter"
Dim DName As String
Dim LCriteria As String
DName = "FrmVehicle_Information"
DoCmd.OpenForm DName, , , LCriteria
DoCmd.Maximize
End If
DoCmd.Close acForm, "FrmState_Filter"
Resume PrintSelectedRecords_Exit
End If
End Sub