Access Combo - Filter As You Type? - vba

I started a thread here: Access Form Combobox Partial Filter but it was a bit too specific, and now that I moved past that, I am struggling to get the behavior I want.
Here is the layout:
A form with the detail hidden. The header has an unbound combobox for filtering the form. Once there is a filter to apply, that record is set for the filter on the form, and the detail is shown. The users want to be able to type in the combo, for partial matches, instead of only being able to click the drop down and click to choose an option.
This is the rowsource of the combo:
SELECT [vw_Info_Records]![recordName] & " (" & [vw_Info_Records]![recordNo] & ")" AS frecord, vw_Info_Records.INF_RID
FROM vw_Info_Records
WHERE ((([vw_Info_Records]![recordName] & " (" & [vw_Info_Records]![recordNo] & ")") Like "*" & [Forms]![frmrecords]![cboFindrecord] & "*"))
ORDER BY [vw_Info_Records]![recordName] & " (" & [vw_Info_Records]![recordNo] & ")";
It is set to auto-expand Yes. It is not bound to anything, and limit to list is set to yes.
This is the onchange event:
Private Sub cboFindRecord_Change()
Dim MyCriteria As String
If Me.cboFindRecord.SelStart > 0 Then
Me.cboFindRecord.Dropdown
End If
End Sub
This is the after update:
Private Sub cboFindRecord_AfterUpdate()
Dim Criteria As String
Dim myfilter As String
If Me.cboFindRecord.ListIndex = -1 Then
Me.Form.Filter = ""
Me.FilterOn = False
Me.Detail.Visible = False
Else
Criteria = "[INF_RID] = " & Me.cboFindRecord.Column(1)
With Me.Form
.Filter = Criteria
.FilterOn = True
End With
Me.Detail.Visible = True
End If
End Sub
The first character that is typed in, makes the dropdown expand, and filter to any results that contain that character. Any subsequent characters, and the drop down list doesn't update the filter to re-filter based on the added characters. If I add a requery in the change, I get an error about saving the field, before I do a requery. It wants me to set that "filter" value as a value for the combo, which I wouldn't do, because it's really an in string filter. The user still needs to pick a value from whatever is left in the list, before I can filter the form to that record.
Thanks for any advice as to how to fix this!

Related

dynamic search box filters using LIKE

I have a dynamic search box filtering a subform based on user input. I also have a few filter buttons that filter the same subform. I set up the search box to incorporate preexisting filters applied by those buttons.
All that works fine. The problem I have is:
The dynamic filter using the LIKE statement only seems to work correctly when the length of the filter text is >= 3. Go below that and it applies some wonky filtering. Maybe I am using the statement wrong. I thought it would look if the field's value contains the search string somewhere in it. But it seemingly accepts values like "Natronlauge 50% techn. EN 896" for a search string of "Hä", which seems weird to me. It works once I add a third character though. Lines 11 and 13:
Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'"
Would be nice if someone has some ideas how to go about these issues.
Here the full code for my searchbox:
Private Sub SearchBoxStoffe_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
'Apply dynamic filter for current filter category.
If Len(SearchBoxStoffe.Text) > 0 Then
filterText = SearchBoxStoffe.Text
If Forms![HUB]![FilterAlleLink] = "" Then
Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*'"
Else
Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'"
End If
Me.FilterOn = True
'Retain filter text in search box after refreshing.
SearchBoxStoffe.Text = filterText
SearchBoxStoffe.SelStart = Len(SearchBoxStoffe.Text)
Else
'Revert to current main filter category.
If Forms![HUB]![FilterAlleLink] <> "" Then
Call FilterStoffe("[kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'")
Else
If Forms![HUB]![FilterAlleLink] = "" Then
Me.Filter = ""
Me.FilterOn = False
End If
End If
End If
'Set focus back to search box
SearchBoxStoffe.SetFocus
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
End Sub
The dynamic filter using the LIKE statement only seems to work
correctly when the length of the filter text is >= 3. Go below that
and it applies some wonky filtering.
I don't really know why this is happening, but try the below. It makes use of the Change() event and covers 4 scenarios:
Both filters applicable.
Search Box only.
Main category only.
Nothing (clear the filter).
Also, I don't know what the FilterStoffe() method does, but I assume it just applies the main filter only.
Private Sub SearchBoxStoffe_Change()
On Error GoTo Trap
Select Case True
'both filters
Case Len(SearchBoxStoffe.Text) > 0 And Not IsNull(Forms.HUB.FilterAlleLink.Value):
Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*' AND [kategorie] = '" & Forms.HUB.FilterAlleLink.Value & "'"
FilterOn = True
'SearchBox only
Case Len(SearchBoxStoffe.Text) > 0:
Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*'"
FilterOn = True
'FilterAlleLink only
Case Not IsNull(Forms.HUB.FilterAlleLink.Value):
Form.Filter = "[kategorie] = '" & Forms.HUB.FilterAlleLink.Value & "'"
FilterOn = True
'Nothing
Case Else:
FilterOn = False
Filter = vbNullString
End Select
Leave:
On Error Resume Next
SearchBoxStoffe.SetFocus
Exit Sub
Trap:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
Resume Leave
End Sub
Keep in mind, within the Change() event, the Text property gets updated with every keystroke and when the control loses the focus, it gets copied to the Value property.
However, the Value is the default property when you just reference the control.
So this
Me.SearchBoxStoffe
is the same as this:
Me.SearchBoxStoffe.Value
There were 2 issues that prevented the searchbox from running as intended:
Object references in the project were created with 2 different language versions of access. Objects would call other objects using the formulation of one language, which in turn called objects using referencing in another language etc. In cases where fields and/or queries would return empty, this would cause some of the references to no longer function as intended. The result was the program running out of stack space, empty controls on subforms that returned empty queries, objects not being found and more.
The searchbox filter was lagging behind the text in the searchbox by one event. If entering a new search string, the applied filter would always be missing the last character when using SearchBoxStoffe in the filter statement. Entering "Wood" would cause the filter to apply "Woo" etc.
The solutions are the following:
Fix all the references in the file manually to either language version and do not mix them up going forward.
The Value of the search box SearchBoxStoffe is not yet updated on either the KeyUp or the Change event when entering a new character. This can be fixed by substituting the Text value instead, which is updated already. Simply change line 11 to Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*'" and line 13 to Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe.Text & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'". The info originally came from #KostasK in his solution:
Keep in mind, within the Change() event, the Text property gets updated with every keystroke and when the control loses the focus, it gets copied to the Value property.
Which works too btw, just wasn't able to be verified since issue 1 prevented the code from running correctly. Answer by Kostas K.

dynamic search box not accounting for strings containing blank space

I have a dynamic search box filtering a subform based on user input. I also have a few filter buttons that filter the same subform. I set up the search box to incorporate preexisting filters applied by those buttons.
The problem:
Currently, I save the string entered into the search box as a variable I call filterText (so I don't lose the value when the form gets refreshed). After the form gets refreshed, I set the content of the search box to that saved value. Then I set the location of the insertion point to the length of the string currently in the search box (lines 9,17,18). This however, does not account for blank space. If a user types something that includes a blank space, say "Homebrew 50%", the insertion point will immediately update back to the end of the text string only and the input will end up missing the space like so "Homebrew50%". How can I get the length of the current user input including spaces?
Here the full code of my search box, there are other things wrong with it but the current question is only regarding the blank space issue:
Private Sub SearchBoxStoffe_KeyUp(KeyCode As Integer, Shift As Integer)
On Error GoTo errHandler
Dim filterText As String
'Apply dynamic filter for current filter category.
If Len(SearchBoxStoffe.Text) > 0 Then
filterText = SearchBoxStoffe.Text
If Forms![HUB]![FilterAlleLink] = "" Then
Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*'"
Else
Me.Form.Filter = "[bezeichnung] LIKE '*" & SearchBoxStoffe & "*' AND [kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'"
End If
Me.FilterOn = True
'Retain filter text in search box after refreshing.
SearchBoxStoffe.Text = filterText
SearchBoxStoffe.SelStart = Len(SearchBoxStoffe.Text)
Else
'Revert to current main filter category.
If Forms![HUB]![FilterAlleLink] <> "" Then
Call FilterStoffe("[kategorie] = '" & Forms![HUB]![FilterAlleLink] & "'")
Else
If Forms![HUB]![FilterAlleLink] = "" Then
Me.Filter = ""
Me.FilterOn = False
End If
End If
End If
'Set focus back to search box
SearchBoxStoffe.SetFocus
Exit Sub
errHandler:
MsgBox Err.Number & " - " & Err.Description, vbOKOnly, "Error ..."
End Sub
So, I figured out the solution eventually.
When inserting the saved user input into the searchbox, you need to actually refer to the Value, not the Text of the searchbox. This will transfer any blank space in the user input correctly.
Change line 17 to:
SearchBoxStoffe = filterText

Continue Loop in TextBox to search multiple value from Database, MS Access

I've changed the textbox "enter key behavior", so that everytime I hit enter, textbox will go to the next line,
Project's Form View
When user Click the Button, subform show result for the first line, but then how to get result in every line user input.
Private Sub CountButton_Click()
Dim SQL As String
SQL = "SELECT database.Tracking, database.Date, DateDiff(""d"",[Date],Date()) As Aeging FROM database;"
Me.Query1_subform.Form.RecordSource = SQL
Me.Query1_subform.Form.Requery
End Sub
Do I need to use Loop or VbCrLf or something else to function the textbox
Split textbox value to an array and loop array to build comma-separated string or:
strIN = Replace(Me.textbox, vbCrLf, "','")
If InStrRev(strIN, "','") > 0 Then strIN = Left(strIN, Len(strIN)-3)
SQL = "SELECT database.Tracking, database.Date, DateDiff('d', [Date], Date()) As Aging " & _
"FROM database " & _
"WHERE Tracking IN('" & strIN & "');"
Using a textbox relies on users to be consistent with input - not starting input with a CR and no other extraneous characters input accidentally or otherwise.
More reliable alternative is a multi-select listbox of Tracking values. Code loops through selected items and builds comma-separated string.

How to query combo box of only current record/row in Access data entry form?

I have created a data entry form in Access that uses combobox for entering farmer name. The combobox is used for ease and to make sure only farmers from the list are entered. For ease combo box is re-queried as you type in.
The combobox works well for the first entry but previous farmers' names are vanished when queried for the next row. I think, Access is requerying all dropdowns rather than the current drop-down/combo-box.
The VBA for the querying drop down is given below:
Public Sub FilterComboAsYouType(combo As ComboBox, defaultSQL As String,
lookupField As String)
Dim strSQL As String
If Len(combo.Text) > 0 Then
strSQL = defaultSQL & " AND " & lookupField & " LIKE '*" & combo.Text &
"*'"
Else
strSQL = defaultSQL 'This is the default row source of combo box
End If
combo.RowSource = strSQL
combo.Dropdown
End Sub
Private Sub Combo137_Change()
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
End Sub
Private Sub Combo137_GotFocus()
If Form_Name <> "" Then
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & Form_Name & "*'", "farmer.name"
Else
FilterComboAsYouType Me.Combo137, "SELECT farmer.name,farmer.ID FROM farms INNER JOIN farmer ON
farms.ID = farmer.farm_id where farms.ID LIKE" & "'" & "NONE" & "*'", "farmer.name"
End If
End Sub
Yes, all records will show the same filtered list because there is only one combobox and property settings are reflected in all instances. Filtering a combobox RowSource based on value in another field/control is known as "cascading" or "dependent". Also, your RowSource has alias - value saved is not value displayed. When the list is filtered the display alias will not be available for records that have saved value which has been filtered out. This is a well-known issue of cascading combobox. Options for dealing with:
for any form style, only filter the list for new record or when primary value is changed, then reset to full list for existing records
for forms in Continuous or Datasheet view, include lookup table in form RecordSource, bind a textbox to descriptive field from lookup table, position textbox on top of combobox, set textbox as Locked Yes and TabStop No

Selecting results from table based on combo box data

Firstly I apologise for my lack of knowledge on SQL.
I currently have two Comboboxes on a form and a list box which looks like the attached image
TroubleshootPage
The first combobox references a table with the list of manufacturers and then the second "Model" combobox updates with model numbers which match the first combobox' data
The List box below needs to display data from the "Solutions" Table. ONLY THE "SolutionText" COLUMN
The Manufacturer selected in "cboManfact" has to match the "ManufacturerSolution" in table "Solutions" and then return "SolutionText" if they match. Same for "cboModel" and "ModelSolution".
I dont want the listbox to display any manufacturer or model text, just the "SolutionText" field when a button db_search is pressed.
Thanks to the help of #LiamH, i currently have the following SQL Command on the RowSource of my list box
This can happen either as the user selects options in the comboboxes or when clicking the green search icon
The problem I am having is with the SQL Query. I currently have this below happening when the user clicks the search button
SELECT [SolutionText] FROM [Solutions] WHERE solutions.ManufacturerSolution like forms![Troubleshoot]!cboManfact & "*" AND solutions.ModelSolution like forms![Troubleshoot]!cboModel & "*"
It displays the SolutionText value in the listbox, but on clicking the "db_search" button, the listbox becomes empty
Private Sub dbSearch_Click()
me.listbox.requery
end sub
Very close to getting this one now, any advice
I have assumed you are using MS-Access, correct me if I am wrong. You may want to consider using access-vba tag if this is correct.
There are a few things wrong with your code. Firstly, docmd.RunSQL cannot be used on select queries. This (SELECT) is not an action query and this command is reserved for action queries only. e.g. DELETE, UPDATE, ALTER, SELECT...INTO.
When you use multiple where clauses use the word AND instead of the ampersand. You don't need to concatenate SQL strings together. String values that are in a query and related to form values need encapsulating in single quotes, like so:
"...WHERE NAME='" & Me.name & "'"
You can use debug.print strSQL or msgbox strSQL to see how your SQL query reads for any errors.
So here's what I would do:
Change the rowsource of the listbox to a query:
SELECT [SolutionText] FROM [Solutions] WHERE solutions.ManufacturerSolution like forms![FORM_NAME]!cboManfact.Column(1) & "*" AND solutions.ModelSolution like forms![FORM_NAME]!cboModel.Column(1) & "*"
The like ... & "*" means that if the combobox is empty it will show all.
You will need some VBA on the on_click event, which will be:
Private Sub dbSearch_Click()
me.listbox.requery
end sub
The requery will change the listbox items based on the records selected in the combo boxes.
I don't know what your intention is when using [Solution Text]?
Are you sure you require column(1) and not column(0)?
I am not sure what your intention was with strTableName?
Solved the issue using the following code:
Private Sub dbSearch_Click()
Dim ManfactQuery As String
Dim ModelQuery As String
Dim strSQL As String
ManfactQuery = Me.cboManfact.Column(1)
ModelQuery = Me.cboModel.Column(1)
If Nz(ManfactQuery) = "" Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ModelSolution = '" & ModelQuery & "'"
Else
If Nz(ModelQuery) = "" Then
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = '" & ManfactQuery & "'"
Else
strSQL = "SELECT [Solutions].SolutionText FROM [Solutions] WHERE [Solutions].ManufacturerSolution = " & ManfactQuery & " AND [Solutions].ModelSolution = " & ModelQuery & ""
End If
End If
Me.lstSolution.RowSource = strSQL
End Sub