I've modified a search/filter form ( to accomodate my ADP project. Basically this code passes the search results to a list box. For the most part everything works well except for searching a date range. Basically, I want my users to be able to enter a begining date and an ending date to search for the needed agreement. My current code for date searching (see below code) will not pass any results.
- My form has two unbounded search boxes (txtStart_Date) and (txtEnd_date)
- The data in my date field is formated "short" (mm/dd/yyyy)
- Iv'e used #" and '%" as wild cards with no luck
Any help or suggestions would be greatly apprecitated.
- My form has two unbounded search boxes (txtStart_Date) and (txtEnd_date)
- The data in my date field is formated "short" (mm/dd/yyyy)
- Iv'e used #" and '%" as wild cards with no luck
Any help or suggestions would be greatly apprecitated.
Code:
Option Compare Database
Private Sub cmdSearch_Click()
'Set the Dimensions of the Module
Dim strSQL As String, strOrder As String, strWhere As String
Dim dbNm As Database
Dim qryDef As QueryDef
Set dbNm = CurrentDb()
'Constant Select statement for the RowSource
strSQL = "SELECT dbo.Location.Recno_Location, dbo.Location.Agreement_Number, dbo.Location.County, dbo.Location.State, dbo.Header.Owner, dbo.Header.Product, dbo.Header.Status, dbo.Header.Approved_Date " & _
"FROM (dbo.Header INNER JOIN dbo.Location ON dbo.Header.Agreement_Number =dbo.Location.Agreement_Number)"
strWhere = "WHERE"
strOrder = "ORDER BY dbo.Location.Recno_Location;"
'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.txtAgrmnt) Then '<--If the textbox txtAgrmnt contains no data THEN do nothing
strWhere = strWhere & " (dbo.Location.Agreement_Number) Like '%" & Me.txtAgrmnt & "%' AND" '<--otherwise, apply the LIKE statment to the QueryDef
End If
If Not IsNull(Me.txtCounty) Then
strWhere = strWhere & " (dbo.Location.County) Like '%" & Me.txtCounty & "%' AND"
End If
If Not IsNull(Me.txtState) Then
strWhere = strWhere & " (dbo.Location.State) Like '%" & Me.txtState & "%' AND"
End If
If Not IsNull(Me.txtOwner) Then
strWhere = strWhere & " (dbo.Header.Owner) Like '%" & Me.txtOwner& "%' AND"
End If
If Not IsNull(Me.txtProduct) Then
strWhere = strWhere & " (dbo.Header.Product) Like '%" & Me.txtProduct & "%' AND"
End If
If Not IsNull(Me.txtStatus) Then
strWhere = strWhere & " (dbo.Header.Status) Like '%" & Me.txtStatus & "%' AND"
End If
If Not IsNull(Me.txtStart_Date) Or Not IsNull(Me.txtEnd_Date) Then
strWhere = strWhere & " (dbo.Header.Approved_Date) Between '%" & Format(Me.txtStart_Date, "mm/dd/yyyy") & "%' AND '%" & Format(Me.txtEnd_Date, "mm/dd/yyyy") & "%'));"
End If
'Remove the last AND from the SQL statment
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox
Me.lstSearchInfo.RowSource = strSQL & " " & strWhere & "" & strOrder
End Sub
Private Sub lstSearchInfo_DblClick(Cancel As Integer)
'Open form seamless based on the Recno_Location from lstSearchInfo listbox
DoCmd.OpenForm "seamless", , , "[Recno_Location] = " & Me.lstSearchInfo, , acDialog
End Sub
Private Sub Close1_Click()
On Error GoTo Close1_Click_Err
DoCmd.OpenForm "Agreements Switchboard", acNormal, "", "", , acNormal
DoCmd.Close acForm, "frmSearchLocation"
close1_Click_Exit:
Exit Sub
Close1_Click_Err:
MsgBox Error$
Resume close1_Click_Exit
End Sub