Solved Search as you type inside a Combo Box (1 Viewer)

Romio_1968

Member
Local time
Tomorrow, 01:53
Joined
Jan 11, 2023
Messages
126
Is there any way to search for matches inside the row source of a combo based 'Like *something*'?

To be more clear:
Let say I have some strings in the combo row source, The default behavior is to search as you type from the begining of the string.
I wonder if I can change this behavior.

For example, if I type Apple in the combo, to shorten (limit) the list to all strings that contain the text apple inside
Apples and other fruits
The storry of mr. Applegate
If nothing is selected, then it will offer the entire row source to choose from.

Thank You
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:53
Joined
Oct 29, 2018
Messages
21,474
Hi. Do a search here for @MajP "FAYT" demo.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:53
Joined
May 21, 2018
Messages
8,529
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:53
Joined
May 7, 2009
Messages
19,245
An example that uses NotInList to filter: https://github.com/josef-poetzl/access-examples/blob/main/usability/ComboboxFilterControl.zip
... This eliminates the need to filter after each character entry.
although sometimes an Error occurs, and when it errors the Class is destroyed?
yet the code is compact.

i think the error comes when you Close the recordset, then assign a "new" recordset (on a closed recordset).
you can leave the recordset or just set it to Nothing:

Code:
   With m_ComboBox

      If .RowSourceType = "Value List" Then
         .RowSource = GetValueListFromRecordset(rst)
      Else
         If .Recordset Is rst Then
            Exit Sub
         ElseIf Not .Recordset Is Nothing Then
            If Not (.Recordset Is m_DataRecordset) Then
               '.Recordset.Close
               'arnelgp
               Set .Recordset = Nothing
            End If
         End If
         Set .Recordset = rst
      End If
     
   End With
 

Attachments

  • ComboboxFilterControl.accdb
    732 KB · Views: 125
Last edited:

Josef P.

Well-known member
Local time
Tomorrow, 00:53
Joined
Feb 2, 2023
Messages
827
Thanks for looking!
I now remove the recordset with Combobox.RowSource = vbNullstring.

Code:
Private Sub SetComboboxDataSource(ByRef rst As Object)

   With m_ComboBox

      If .RowSourceType = "Value List" Then
         .RowSource = GetValueListFromRecordset(rst)
      Else
         If .Recordset Is rst Then
            Exit Sub
         ElseIf Not .Recordset Is Nothing Then
            If Not (.Recordset Is m_DataRecordset) Then
               .Recordset.Close ' To close or not to close, that is the question.
            End If
         End If
         .RowSource = vbNullString 'Remove ComboBox.Recordset
         Set .Recordset = rst
      End If
      
   End With
  
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:53
Joined
May 7, 2009
Messages
19,245
not tested but
i think the .RowSource is the SQL string, so if you remove it, then how can we get the recordset?

it errors when you Close the recordset, then assign to it rst.
i think when you Closed an object, there is no way to assign anything to it.
like if you use ADODB.Connection, when you close it, then you can't assign
any ConnectionString to it. you need to Open another Connection.
 

Josef P.

Well-known member
Local time
Tomorrow, 00:53
Joined
Feb 2, 2023
Messages
827
My suspicion is that there may be problems if RowSource and the later bound recordset do not match.
With Set .Recordset = rst I set the new reference. Thus the possibly existing recordset should go away.
I will add Set .Recordset = Nothing as well just to be safe.

Unfortunately, I do not succeed in generating a runtime error in my example. Therefore I simply exclude all problems known to me so far. ;)
 
Last edited:

apr pillai

AWF VIP
Local time
Tomorrow, 04:23
Joined
Jan 20, 2005
Messages
735
This example filters data by character matching data from the left-side, check whether it helps to solves your issue.
 

Users who are viewing this thread

Top Bottom