Solved Smart combo boxes (1 Viewer)

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Hi. I am testing the smart combo boxes, so that I filter as I write (disappear the results that do not match). But there is a combo box that I can not get it to work correctly, and it is the one that interests me the most (it shows a list of products for a POS), and I can not find the error. I copy here what I've done, to see if you can tell me where I'm wrong:

I have created a blank form with the same dropdown of the POS form and the configurations with which I have made it work in other dropouts:
dropbox.com/s/ztypa9hntr6hzh2/01.jpg?dl=0
dropbox.com/s/unnmwn18t8b7rxo/02.jpg?dl=0
dropbox.com/s/6c3uvt0kdk7sbuk/03.jpg?dl=0

I've put in VBA to analyze the code. So, when I write, look what happens:

dropbox.com/s/8vh9tldb4cemk22/04.jpg?dl=0
dropbox.com/s/etspqph6opqchn0/05.jpg?dl=0

The image does not come out, but the value that the CboArticle field takes is "a". Just what I wrote in the drop-down.

Also, if I try the code in a query, with any parameter, it works for me:

dropbox.com/s/l5mazog9pky2qqi/06.jpg?dl=0
dropbox.com/s/2lm844l731cma3i/07.jpg?dl=0

I tried to put the property "Inherit list of values" in No, but it does not work either.

If you need the database, more data, you want me to try something else, I do not know, anything, tell me. I am interested in solving this problem.

Thank you.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Hello thank you very much.
What I do not understand is how to make it work in a particular form with a particular combo box.
What do I have to put in the form? Or in the combo box? I am referring to events.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
Thanks. I probably need to edit that post a little better. However, read the instructions in the class module.

In the form there is no events. The class module takes care of all of that. In the form you need to do the following

Code:
 Public faytProducts As New FindAsYouTypeCombo
 
 Form_Open(Cancel As Integer)
   faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", False
 End Sub

faytProducts ' can be replaced with whatever name you want
Me.cmbProducts 'needs to be replaced with the name of the combo box
"ProductName" ' needs to be replaced with the field name you are filtering.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Okay. It gives me two errors:
The first says that comments can only appear after End Sub, End Function or End Property.

And then, I have the code Form_Open (Cancel As Integer) in red. I leave you an image of the code as I have copied it.

dropbox.com/s/bdtdwd8b7cg4ty4/08.jpg?dl=0
 

Minty

AWF VIP
Local time
Today, 09:58
Joined
Jul 26, 2013
Messages
10,368
@zelarra - Just a bit of advice, please copy and paste actual code (use the # in the editor above) rather than image links, for a couple of reasons:

Firstly, a lot of people are suspicious of dropbox links and simply won't look at at them.

Secondly, it's simple for us to cut and paste your code to test , rather than to have to try and retype it from an image.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
You can even post a database to this site and people are willing to look at that. Use the "Go Advanced" button for more features. Also see the images above the reply box. These provide a lot of features.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Thanks for the advices. Here is the database. The form is called Formulario1
 

Attachments

  • Tienda BD - CuadrosCombinadosInteligentes.zip
    1.2 MB · Views: 83

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
I just made it work. However, I have a doubt.
When I already have the filtered list, and it shows me, for example, four articles, and I use the keyboard to move and select the one I want, I can not, since I choose the first one.
How can I fix that?
Thank you!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
The name of the Class module has to be
FindAsYouTypeCombo

The form code should look like this
Code:
'At the top of the code
Public faytArticulo As New FindAsYouTypeCombo

Private Sub Form_Open(Cancel As Integer)
   faytArticulo.InitalizeFilterCombo Me.CboArticulo, "Articulo", False
End Sub
 

Attachments

  • Tienda BD - Update.zip
    1 MB · Views: 102

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Yes, I got that, thank you, but just above your answer I leave you with a problem that I have with the behavior.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
I will add in that behavior that allows the arrows. I will post back. You will just need to replace the code in the class module.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Another thing that I've been trying. In English we do not have that problem, but in Spanish we have accents, and many times you do not remember if a word has been written with tilde or without it. I have found a module that solves this, and I have modified your code so that it does not distinguish the tildes. I pass the module and the change in the module, in case you want to add it.

Code:
Private Sub FilterList()
  On Error GoTo errLable
  Dim rsTemp As DAO.Recordset
  Dim strText As String
  Dim strFilter As String
  strText = mCombo.Text
  If mFilterFieldName = "" Then
    MsgBox "Must Supply A FieldName Property to filter list."
    Exit Sub
  End If
  If mFilterFromStart = True Then
    strFilter = mFilterFieldName & " like '" & Buscaacent(Nz(strText, Null)) & "*'"
  Else
    strFilter = mFilterFieldName & " like '*" & Buscaacent(Nz(strText, Null)) & "*'"
  End If
  Set rsTemp = mRsOriginalList.OpenRecordset
  rsTemp.Filter = strFilter
  Set rsTemp = rsTemp.OpenRecordset
  If rsTemp.RecordCount > 0 Then
    Set mCombo.Recordset = rsTemp
  End If
  mCombo.Dropdown
  Exit Sub
errLable:
  If Err.Number = 3061 Then
    MsgBox "Will not Filter. Verify Field Name is Correct."
  Else
    MsgBox Err.Number & "  " & Err.Description
  End If
End Sub

Thank you so much
 

Attachments

  • mdlBuscaAcent.zip
    701 bytes · Views: 79
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
Here is the updates and should provide the functionality that you want. Post back if having any issues.

Code:
Option Compare Database
Option Explicit


'Class Module Name: FindAsYouTypeCombo
'Purpose: Turn any combobox into a "Find As You Type" 'Combobox
'Created by: MajP
'Demonstrates: OOP, and With Events
'
'Use: To use the class, you need a reference to DAO and code
'similar to the following in a form's module.

'Parmaters:
'  TheComboBox: Your Combobox object passed as an object
'  FilterFieldName: The name of the field to Filter as
'  string
'  FilterFromStart: Determines if you filter a field that
'  starts with the text or if the text appears anywhere in
'  the record.
'
'*******START: Place Code like this in the Form *******************
'
' Option Compare Database
' Option Explicit
' Public faytProducts As New FindAsYouTypeCombo
'
' Form_Open(Cancel As Integer)
'   faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", False
' End Sub
'
'******* END: Form Code ******************


Private WithEvents mCombo As Access.ComboBox
Private WithEvents mForm As Access.Form
Private mFilterFieldName As String
Private mRsOriginalList As DAO.Recordset
Private mFilterFromStart As Boolean
Private mHandleArrows As Boolean
Private mAutoCompleteEnabled As Boolean


'---------------------------------------- Properties --------------------------
Public Property Get FilterComboBox() As Access.ComboBox
  Set FilterComboBox = mCombo
End Property
Public Property Set FilterComboBox(TheComboBox As Access.ComboBox)
  Set mCombo = TheComboBox
End Property
Private Sub mCombo_Change()
  Call FilterList
End Sub
Public Property Get FilterFieldName() As String
  FilterFieldName = mFilterFieldName
End Property
Public Property Let FilterFieldName(ByVal theFieldName As String)
  mFilterFieldName = theFieldName
End Property
Public Property Get HandleArrows() As Boolean
  HandleArrows = mHandleArrows
End Property

Public Property Let HandleArrows(ByVal TheValue As Boolean)
  mHandleArrows = TheValue
End Property

'------------------------------------------- Handled Events ----------------
Private Sub mCombo_GotFocus()
   'If mAutoCompleteEnabled = True Then mCombo.Dropdown
End Sub
Private Sub mCombo_AfterUpdate()
  If Not mAutoCompleteEnabled Then Call unFilterList
End Sub
Private Sub mForm_Current()
  Call unFilterList
End Sub
Private Sub mForm_Close()
  ' Code provided by BenSacheri to keep Access from crashing
   Call Class_Terminate
End Sub

Private Sub mCombo_KeyDown(KeyCode As Integer, Shift As Integer)
  ' Handle keys that affect the auto-complete feel of the combobox.  BS 10/13/2015
    If mHandleArrows = True Then
    Select Case KeyCode
      Case vbKeyDown, vbKeyUp, vbKeyReturn, vbKeyPageDown, vbKeyPageUp
          ' When these special keys are hit they begin to select records
          ' from the dropdown list.  Without this, as soon as one record
          ' is selected (by highlighting it) then the entire filter is
          ' set to that item making it impossible to use the keyboard to
          ' scroll down and pick an item down in the list.
          mAutoCompleteEnabled = False
        Case Else
          mAutoCompleteEnabled = True
        End Select
    End If
End Sub


'----------------------------------  Procedures ----------------------------
Public Sub InitalizeFilterCombo(TheComboBox As Access.ComboBox, FilterFieldName As String, Optional FilterFromStart As Boolean = True, Optional HandleArrows As Boolean = True)
   On Error GoTo errLabel
   Dim rs As DAO.Recordset
   If Not TheComboBox.RowSourceType = "Table/Query" Then
      MsgBox "This class will only work with a combobox that uses a Table or Query as the Rowsource"
      Exit Sub
   End If
   Set mCombo = TheComboBox
   Set mForm = TheComboBox.Parent
   mHandleArrows = HandleArrows
   mAutoCompleteEnabled = True
   If mHandleArrows = True Then
      mCombo.OnKeyDown = "[Event Procedure]"
      mCombo.OnClick = "[Event Procedure]"
   End If
   mFilterFieldName = FilterFieldName
   mFilterFromStart = FilterFromStart
   mForm.OnCurrent = "[Event Procedure]"
   mCombo.OnGotFocus = "[Event Procedure]"
   mCombo.OnChange = "[Event Procedure]"
   mCombo.AfterUpdate = "[Event Procedure]"
   mForm.OnClose = "[Event Procedure]"
   With mCombo
     .SetFocus
     .AutoExpand = False
   End With
   If mCombo.Recordset Is Nothing Then
     Set rs = CurrentDb.OpenRecordset(TheComboBox.RowSource)
     Set mCombo.Recordset = rs
   End If
   Set mRsOriginalList = mCombo.Recordset.Clone
   Exit Sub
errLabel:
    MsgBox Err.Number & " " & Err.Description
End Sub

Private Sub FilterList()
  On Error GoTo errLable
  Dim rsTemp As DAO.Recordset
  Dim strText As String
  Dim strFilter As String
  strText = mCombo.Text
  If mFilterFieldName = "" Then
    MsgBox "Must Supply A FieldName Property to filter list."
    Exit Sub
  End If
  If mAutoCompleteEnabled = False Then Exit Sub
  If mFilterFromStart = True Then
    strFilter = mFilterFieldName & " like '" & strText & "*'"
  Else
    strFilter = mFilterFieldName & " like '*" & strText & "*'"
  End If
  Set rsTemp = mRsOriginalList.OpenRecordset
  rsTemp.Filter = strFilter
  Set rsTemp = rsTemp.OpenRecordset
  If rsTemp.RecordCount > 0 Then
    Set mCombo.Recordset = rsTemp
  End If
  mCombo.Dropdown
  Exit Sub
errLable:
  If Err.Number = 3061 Then
    MsgBox "Will not Filter. Verify Field Name is Correct."
  Else
    MsgBox Err.Number & "  " & Err.Description
  End If
End Sub
Private Sub unFilterList()
  On Error GoTo errLable
  Set mCombo.Recordset = mRsOriginalList
   Exit Sub
errLable:
  If Err.Number = 3061 Then
    MsgBox "Will not Filter. Verify Field Name is Correct."
  Else
    MsgBox Err.Number & "  " & Err.Description
  End If
End Sub

Private Sub Class_Terminate()
    Set mForm = Nothing
    Set mCombo = Nothing
    Set mRsOriginalList = Nothing
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
Thanks I will incorporate the accent code. I also realized I have to do something similar in english and I will update.

A search string with an apostrophe needs to get replaced with double apostrophe
Mike's needs to become Mike''s. I will add both in.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Cool. Thank you.
It works fine.
Where do you insert the accent and apostrophe codes?
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
In the filterlist code at the very beginning
Code:
  strText = mCombo.Text
  'Add the following afterwards
  strText = Replace(strText, "'", "''")
  strText = Replace(strText, "#", "[#]")
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
I simplified the buscaaccent code, but not sure how this works.
I get same results with this and much simpler
Code:
Public Function Buscaacent2(ByVal strText As String) As String
       
    Buscaacent2 = Replace(strText, "A", "[AÁÀÂÄaá]")
    Buscaacent2 = Replace(Buscaacent2, "E", "[EÉÈÊËeé]")
    Buscaacent2 = Replace(Buscaacent2, "I", "[IÍÌÎÏií]")
    Buscaacent2 = Replace(Buscaacent2, "O", "[OÓÒÔÖ0oó]")
    Buscaacent2 = Replace(Buscaacent2, "U", "[UÚÙÛÜuú]")

End Function

If I run the demo for Maria I get back
M[AÁÀÂÄaá]r[IÍÌÎÏií][AÁÀÂÄaá]
So the code would be
like "*M[AÁÀÂÄaá]r[IÍÌÎÏií][AÁÀÂÄaá]*"
Not sure I understand how that string would match Maria or any accented versions.
 

zelarra821

Registered User.
Local time
Today, 10:58
Joined
Jan 14, 2019
Messages
809
Hi. The code that you have copied before does not incorporate the apostrophes or the accents.
On the other hand, I enclose the database from which I took the accents, in case you want to see more. You could pass the manual how it is done, but it is in Spanish.
Anyway, what I can tell you is that we can test the code, since, as you have been able to verify, it is for a POS for a store. So we'll see it easy if it works or not.
Thank you very much for the help.
 

Attachments

  • MSdP.Acentos.accdb
    548 KB · Views: 76

MajP

You've got your good things, and you've got mine.
Local time
Today, 04:58
Joined
May 21, 2018
Messages
8,525
Hi. The code that you have copied before does not incorporate the apostrophes or the accents
That is correct. Just add this in in the filterlist procedure. Once I get all updates I will repost a clean version.

Code:
 strText = mCombo.Text
  'Add the following afterwards
  strText = Replace(strText, "'", "''")
  strText = Replace(strText, "#", "[#]")
 

Users who are viewing this thread

Top Bottom