Would someone with more knowledge than I have a look at this code and see if they can see why it is not working.
I have set up a form to search with 5 fields and the results of this are supposed to populate a list box on the same form. When I press the search button, nothing happens.
Fields explained:
ItemName = text and number document names
SerialFrom = possible character and number
Regimental = 4 or 5 diget number
ArchiveFolio = Numeral and/or letter
DestructionYear = 4 digit numeral (i.e. year)
Fields are drawn, through a query (called qryPullData) from tables tblItem (ItemName) and AccountableItem (remainder of fields).
Much appreciated in advance...
Private Sub Search_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 tblItem.ItemName, tblAccountableItem.SerialFrom, tblAccountableItem.Regimental, tblAccountableItem.ArchiveFolio, tblAccountableItem.DestructionYear " & _
"FROM tblItem "
strWhere = "WHERE"
strOrder = "ORDER BY tblItem.ItemName;"
'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.lookup1) Then '<--If the textbox lookup1 contains no data THEN do nothing
strWhere = strWhere & " (tblItem.ItemName) Like '*" & Me.lookup1 & "*' AND" '<--otherwise, apply the LIKE statement to the QueryDef
End If
If Not IsNull(Me.lookup2) Then
strWhere = strWhere & " (tblAccountableItem.SerialFrom) Like '*" & Me.lookup2 & "*' AND"
End If
If Not IsNull(Me.lookup3) Then
strWhere = strWhere & " (tblAccountableItem.Regimental) Like '*" & Me.lookup3 & "*' AND"
End If
If Not IsNull(Me.lookup4) Then
strWhere = strWhere & " (tblAccountableItem.ArchiveFolio) Like '*" & Me.lookup4 & "*' AND"
End If
If Not IsNull(Me.lookup5) Then
strWhere = strWhere & " (tblAccountableItem.DestructionYear) Like '*" & Me.lookup5 & "*' AND"
End If
'Remove the last AND from the SQL statement
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox Me.ListSearchResults.RowSource = strSQL & " " & strWhere & "" & strOrder
End Sub
I have set up a form to search with 5 fields and the results of this are supposed to populate a list box on the same form. When I press the search button, nothing happens.
Fields explained:
ItemName = text and number document names
SerialFrom = possible character and number
Regimental = 4 or 5 diget number
ArchiveFolio = Numeral and/or letter
DestructionYear = 4 digit numeral (i.e. year)
Fields are drawn, through a query (called qryPullData) from tables tblItem (ItemName) and AccountableItem (remainder of fields).
Much appreciated in advance...
Private Sub Search_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 tblItem.ItemName, tblAccountableItem.SerialFrom, tblAccountableItem.Regimental, tblAccountableItem.ArchiveFolio, tblAccountableItem.DestructionYear " & _
"FROM tblItem "
strWhere = "WHERE"
strOrder = "ORDER BY tblItem.ItemName;"
'Set the WHERE clause for the Listbox RowSource if information has been entered into a field on the form
If Not IsNull(Me.lookup1) Then '<--If the textbox lookup1 contains no data THEN do nothing
strWhere = strWhere & " (tblItem.ItemName) Like '*" & Me.lookup1 & "*' AND" '<--otherwise, apply the LIKE statement to the QueryDef
End If
If Not IsNull(Me.lookup2) Then
strWhere = strWhere & " (tblAccountableItem.SerialFrom) Like '*" & Me.lookup2 & "*' AND"
End If
If Not IsNull(Me.lookup3) Then
strWhere = strWhere & " (tblAccountableItem.Regimental) Like '*" & Me.lookup3 & "*' AND"
End If
If Not IsNull(Me.lookup4) Then
strWhere = strWhere & " (tblAccountableItem.ArchiveFolio) Like '*" & Me.lookup4 & "*' AND"
End If
If Not IsNull(Me.lookup5) Then
strWhere = strWhere & " (tblAccountableItem.DestructionYear) Like '*" & Me.lookup5 & "*' AND"
End If
'Remove the last AND from the SQL statement
strWhere = Mid(strWhere, 1, Len(strWhere) - 5)
'Pass the SQL to the RowSource of the listbox Me.ListSearchResults.RowSource = strSQL & " " & strWhere & "" & strOrder
End Sub