Harry Paraskeva
Registered User.
- Local time
- Today, 18:29
- Joined
- Sep 8, 2013
- Messages
- 78
Hello to all,
I have a form in an MS Access 64-bit frontend with some text fields, some integer, and some combo boxes (the backend is in MySQL). The combo boxes have queries in their row source that are bound to the form based on ID, but contain text data in the second column of the row source query. I provide an example row source query of one of the combo boxes below:
It should be noted that the first/bound column of the combo boxes is hidden (0-width column), so as to show the second column with the text.
I also have an unbound text box and a button. The latter two are meant to allow the end user to type a string in the text box and when the user clicks the button VBA code should be doing the following:
- Looking if the string is contained in a selection of the text and integer fields.
- Looking if the string is contained in the second column of the row source query in combo boxes.
- Changing the form's row source and repopulating it only with fields matching the search criteria.
The decision to go with row source change, instead of filtering has to do with the fact that the form is quite complex (about 150 fields and 7 nested subforms).
I have produced this code, but it doesn't work and I'm not sure why.
I have a form in an MS Access 64-bit frontend with some text fields, some integer, and some combo boxes (the backend is in MySQL). The combo boxes have queries in their row source that are bound to the form based on ID, but contain text data in the second column of the row source query. I provide an example row source query of one of the combo boxes below:
SQL:
SELECT dating.ID, dating.Period_Name AS Period, dating.General, dating.Order FROM dating AS dating WHERE (((dating.General)=Yes)) ORDER BY dating.ID, dating.Order;
I also have an unbound text box and a button. The latter two are meant to allow the end user to type a string in the text box and when the user clicks the button VBA code should be doing the following:
- Looking if the string is contained in a selection of the text and integer fields.
- Looking if the string is contained in the second column of the row source query in combo boxes.
- Changing the form's row source and repopulating it only with fields matching the search criteria.
The decision to go with row source change, instead of filtering has to do with the fact that the form is quite complex (about 150 fields and 7 nested subforms).
I have produced this code, but it doesn't work and I'm not sure why.
Code:
Private Sub cmdSearch_Click()
Dim strSearch As String
Dim rstClone As Recordset
Dim strFilter As String
If Me.Search_Descriptions = "" Or IsNull(Me.Search_Descriptions) Then
MsgBox "Please add a search term in the search box.", vbOKOnly, "No search term"
Exit Sub
End If
strSearch = Me.Search_Descriptions.Value
' Clone the recordset of the form "Inventory_List"
Set rstClone = Me.RecordsetClone
' Build the base filter string
strFilter = "[Inventory_Provenance] Like '*" & strSearch & "*' OR [Context] Like '*" & strSearch & "*'"
strFilter = strFilter & " OR [Exact_Dating] Like '*" & strSearch & "*' OR [Artefact_Type] Like '*" & strSearch & "*'"
strFilter = strFilter & " OR [Materials] Like '*" & strSearch & "*' OR [Description] Like '*" & strSearch & "*'"
strFilter = strFilter & " OR [Artefact_Caption] Like '*" & strSearch & "*'"
' Handle Specific_Dating_List combo box
If Me.Specific_Dating_List.Column(1) Like "*" & strSearch & "*" Then
strFilter = strFilter & " OR [Specific_Dating_List] = " & Me.Specific_Dating_List.Value
End If
' Handle General_Dating_List combo box
If Me.General_Dating_List.Column(1) Like "*" & strSearch & "*" Then
strFilter = strFilter & " OR [General_Dating_List] = " & Me.General_Dating_List.Value
End If
' Handle General_Category combo box
If Me.General_Category.Column(1) Like "*" & strSearch & "*" Then
strFilter = strFilter & " OR [General_Category] = " & Me.General_Category.Value
End If
' Apply the filter to the cloned recordset
rstClone.Filter = strFilter
' Populate the form's record source with the filtered records
If rstClone.RecordCount > 0 Then
Set Me.Recordset = rstClone
Me.Requery
Else
MsgBox "No records found.", vbOKOnly, "Search Results"
' Clear the form's record source if no records are found
Me.RecordSource = ""
End If
' Clear the search box
Me.Search_Descriptions.Value = ""
' Cleanup
Set rstClone = Nothing
End Sub