Solved Smart combo boxes (1 Viewer)

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
I have tried the apostrophe and I see that it works well.
What does not work is the accents. I added the function in the FindAsYouTypeCombo module, and then retouched the code. That is to say:

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

Code:
  If mFilterFromStart = True Then
    strFilter = mFilterFieldName & " like '" & Buscaacent2(Nz(strText, Null)) & "*'"
  Else
    strFilter = mFilterFieldName & " like '*" & Buscaacent2(Nz(strText, Null)) & "*'"
  End If

I do not know if I'll be doing something wrong.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
Thanks. I learnt something here. The like operator is more powerful than I thought and you can do patterns. This is like a simplified REGEX (regular expressions). If you put something in brackets it treats it like a range. However, I replaced my simplified code in the example and it works. If you want to use that.

So if the string is
m[AÁÀÂÄaá]r[IÍÌÎÏií][AÁÀÂÄaá]
It has to match m, any of these AÁÀÂÄaá, r, any of IÍÌÎÏií, and any of AÁÀÂÄaá

https://www.lifewire.com/pattern-matching-in-sql-server-queries-1019799

So my question is what about the non vowels. Is there not tildas on "n"? Or is this Portugese not Spanish?
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
In Spanish only the vowels are accentuated. In Portuguese I only know presunto and Cristiano Ronaldo, so look at my level.
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
Well, I'm retiring now. I'll wait until you have a clean code prepared. Where will you hang it? In this post or the first one that you passed me?
Thank you very very much. A greeting.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
Good Morning.

One thing is the vowels, and another different case are those that do not have a tilde, but another different orthographic sign, as is the case of the ñ. In this case, Acess filters it well; The problem would be in people who do not have the Spanish keyboard, with the ñ key, and, therefore, it does not filter correctly. If you wish, add it, it seems an interesting option.

Regarding the article that you have passed me, if you have achieved it with what you proposed to me, do so. I think it is not difficult, then, to introduce new cases. What I did want to tell you is that I can not get it to work well. I have put the function in the class module, but it does not work. Do I have to add something else?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
Try this. It works for me. I gave you the option to search for "international characters" or not. It is defaulted to search, but a user may want to turn that off for speed. May not make a real difference. It is in the initialize method as a parameter. I added a few more characters. When I get to a different computer I will post the demo. It used the Northwind database that is great for the demo. It has lots of different characters in the set not just spanish, but German, Scandinavian, and others. Thanks for the suggestions, I will repost to the original site.

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
'
'To Use: Place this code in a Class Module
'   The class MUST be called "FindAsYouTypeCombo"
'
'*******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, True, False
' End Sub
'
'******* END: Form Code ******************
'
'
'Parameters of the InitializeFilterCombo:
'  TheComboBox: Your Combobox object passed as an object reference
'  FilterFieldName: The name of the field to Filter passed as a string variable
'  FilterFromStart: Determines if you filter a field that
'    starts with the desired text or if the text appears anywhere in the record
'  HandleArrows: This controls the behavior to move up and down the list with arrow keys
'    and not select the first value. Boolean variable
'  HandleInternationalCharacters: This allows you to search for international characters. (a = á,N = Ñ, etc.)
'    This may slow down the procedure because it does a lot of replacements. Boolean
'



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
Private mHandleInternationalCharacters 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()
   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
Private Sub mCombo_Click()
  mAutoCompleteEnabled = False
End Sub

'----------------------------------  Class Procedures ----------------------------
Public Sub InitalizeFilterCombo(TheComboBox As Access.ComboBox, FilterFieldName As String, _
                      Optional FilterFromStart As Boolean = True, Optional HandleArrows As Boolean = True, _
                      Optional HandleInternationalCharacters 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
   mHandleInternationalCharacters = HandleInternationalCharacters
   'HandleArrows allows you to use the arrow keys to move up and down without selecting the value
   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.OnClick = "[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
  strText = Replace(strText, "'", "''")
  strText = Replace(strText, "#", "[#]")
  If mHandleInternationalCharacters Then
    strText = InternationalCharacters(strText)
  End If
  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
'------------------------------------ To Handle International Characters  ---------------------------
Private Function InternationalCharacters(ByVal strText As String) As String
    InternationalCharacters = Replace(strText, "A", "[AÁÀÂÄaá]")
    InternationalCharacters = Replace(InternationalCharacters, "E", "[EÉÈÊËeé]")
    InternationalCharacters = Replace(InternationalCharacters, "I", "[IÍÌÎÏií]")
    InternationalCharacters = Replace(InternationalCharacters, "O", "[OÓÒÔÖ0oóøØ]")
    InternationalCharacters = Replace(InternationalCharacters, "U", "[UÚÙÛÜuú]")
    InternationalCharacters = Replace(InternationalCharacters, "N", "[NnñÑ]")
    InternationalCharacters = Replace(InternationalCharacters, "C", "[CcçÇ]")
End Function
'-----------------------------------------End --------------------------------------------
Private Sub Class_Terminate()
    Set mForm = Nothing
    Set mCombo = Nothing
    Set mRsOriginalList = Nothing
End Sub
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
Hi. Thank you. I do not know where I will be doing it wrong, but I do not filter the accents well :confused::confused:. If I put álcatel (the first one with an accent, when the list appears without an accent), I do not filter anything. I give you the database so you can see it, but also a couple of images.

On the other hand, if I wanted to apply this class module in two combo boxes in the same form, how should I do it? In this database that happened to you, I tried to do it in the form 03-E Dialogo Resultados Mensual.
 

Attachments

  • Tienda BD - Update.zip
    970.4 KB · Views: 98
  • ScreenShot001.jpg
    ScreenShot001.jpg
    86.3 KB · Views: 70
  • ScreenShot002.jpg
    ScreenShot002.jpg
    98.6 KB · Views: 65

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
In the demo I have three different combo boxes on the form.


Code:
Option Compare Database

Public faytProducts As New FindAsYouTypeCombo
Public faytProductsNoInt As New FindAsYouTypeCombo
Public faytCustomers As New FindAsYouTypeCombo

Private Sub Form_Load()
  faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", False
  faytProductsNoInt.InitalizeFilterCombo Me.cmboProductNoInternational, "ProductName", False, , False
  faytCustomers.InitalizeFilterCombo Me.cmboCustomers, "CompanyName", True, False
End Sub
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
In the forms that contain the word "Dialogo" it gives me two errors. I attached a screenshot of both.

With regard to the international characters, could you give me an example where it works for you? I have not succeeded.

Thank you!
 

Attachments

  • ScreenShot001.jpg
    ScreenShot001.jpg
    73.8 KB · Views: 55
  • ScreenShot002.jpg
    ScreenShot002.jpg
    90.7 KB · Views: 63

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
Thank you. I do not know where I will be doing it wrong, but I do not filter the accents well
I do not think you are doing it wrong, you are doing the reverse from what I was expecting. I thought you wanted to type "a" and find "à" but you want to type "à" and find "a'". Is that correct? That did not occur to me because I do not have an easy way to type "à" and that would be the opposite thing for an English speaker.

let me take a look at that to handle both directions.

Also in the initialize function there is this code
Code:
   With mCombo
     .SetFocus
     .AutoExpand = False
   End With

delete the .setfocus. Not sure what its purpose is and will cause your hidden comobos to fail. I also noticed your name repeated another object name. To be safe use something like
faytAno instead of Ano (which was already used)
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
so if you type á it will first turn it back to "a" and then get the correct range. You can add as needed to the list. You should not need to worry about both capital and small case since sql is case insensitive.

Code:
Private Function InternationalCharacters(ByVal strText As String) As String
   InternationalCharacters = strText
   'If you type international turn first to english
    'Type international and get english
    InternationalCharacters = Replace(InternationalCharacters, "á", "a")
    InternationalCharacters = Replace(InternationalCharacters, "é", "e")
    InternationalCharacters = Replace(InternationalCharacters, "í", "i")
    InternationalCharacters = Replace(InternationalCharacters, "ó", "o")
    InternationalCharacters = Replace(InternationalCharacters, "ú", "u")
    InternationalCharacters = Replace(InternationalCharacters, "ü", "u")
    InternationalCharacters = Replace(InternationalCharacters, "ñ", "n")
    'Add others as necessary á, é, í, ó, ú, ü, ñ
    
   ' Debug.Print InternationalCharacters
   'Type english and get international
    InternationalCharacters = Replace(InternationalCharacters, "A", "[AÁÀÂÄaá]")
    InternationalCharacters = Replace(InternationalCharacters, "E", "[EÉÈÊËeé]")
    InternationalCharacters = Replace(InternationalCharacters, "I", "[IÍÌÎÏií]")
    InternationalCharacters = Replace(InternationalCharacters, "O", "[OÓÒÔÖ0oóøØ]")
    InternationalCharacters = Replace(InternationalCharacters, "U", "[UÚÙÛÜuú]")
    InternationalCharacters = Replace(InternationalCharacters, "N", "[NnñÑ]")
    InternationalCharacters = Replace(InternationalCharacters, "C", "[CcçÇ]")

End Function
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
Also in the initialize function there is this code
Code:
   With mCombo
     .SetFocus
     .AutoExpand = False
   End With

delete the .setfocus. Not sure what its purpose is and will cause your hidden comobos to fail. I also noticed your name repeated another object name. To be safe use something like
faytAno instead of Ano (which was already used)

It works perfectly for the first combo, but for the next ones, which are related to the previous one, no. From what I intuit, the problem is that it does not catch the changes of origin of the data. You can see it in the database that I attached.

so if you type á it will first turn it back to "a" and then get the correct range. You can add as needed to the list. You should not need to worry about both capital and small case since sql is case insensitive.

Solved. Thank you so much.
 

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
I'm putting the code in all the combos in the database, but I can not get the related combos to work. I think it is because it takes from the beginning of the instruction the origin of the second and subsequent related combos, instead of "waiting" for you to update the first one to update the following ones based on the first one.

How can I solve that?

Thanks for the help!
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
I did not see an attachment. I think you want to do "cascading combos" where the second combo is dependent on the first. I will see if I can add a feature to do that. I will get back.

If you provide an attachment it would help if you provided a stripped down version with just the needed table and single form. I would be a lot easier to check and demonstrate.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
Try this module. Please note that the initilialize event has slightly changed. I have added a property to allow you to change the rowsource. Each time I add something I break something else so please report any problems.

On my demo form I have 5 comboboxes that I initialize slightly differently. Look at the last one which is a cascading combobox. I change its rowsource when I enter it. So now you will have a cascading FAYT combo.

Code:
Public faytProducts As New FindAsYouTypeCombo
Public faytProductsNoInt As New FindAsYouTypeCombo
Public faytProductForward As New FindAsYouTypeCombo
Public faytForwardNoHandles As New FindAsYouTypeCombo
Public faytCascade As New FindAsYouTypeCombo





Private Sub Form_Load()
  faytProducts.InitalizeFilterCombo Me.cmbProducts, "ProductName", AnywhereInString, True
  faytProductsNoInt.InitalizeFilterCombo Me.cmboProductNoInternational, "ProductName", AnywhereInString, , False
  faytProductForward.InitalizeFilterCombo Me.cmboProductForward, "ProductName", FromBeginning, True
  faytForwardNoHandles.InitalizeFilterCombo Me.cmboBeginningNoHandles, "ProductName", FromBeginning, False
  faytCascade.InitalizeFilterCombo Me.cmboCascadeProducts, "ProductName"
End Sub

Private Sub cmboCascadeProducts_Enter()
  Dim strSql As String
  strSql = "SELECT Products.ProductID, Products.ProductName FROM Products where SupplierName = '" & Nz(Me.cmboSupplier, "") & "' ORDER BY Products.[ProductName]"
  Me.cmboCascadeProducts.RowSource = strSql
  Me.cmboCascadeProducts.Requery
  faytCascade.RowSource = strSql
End Sub


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
'
'To Use: Place this code in a Class Module
'   The class MUST be called "FindAsYouTypeCombo"
'
'*******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, True, False
' End Sub
'
'******* END: Form Code ******************
'
'
'Parameters of the InitializeFilterCombo:
'  TheComboBox: Your Combobox object passed as an object reference
'  FilterFieldName: The name of the field to Filter passed as a string variable
'  SearchType: Determines if you filter a field that
'    starts with the desired text or if the text appears anywhere in the record
'  HandleArrows: This controls the behavior to move up and down the list with arrow keys
'    and not select the first value. Boolean variable
'  HandleInternationalCharacters: This allows you to search for international characters. (a = á,N = Ñ, etc.)
'    This may slow down the procedure because it does a lot of replacements. Boolean
'



Private WithEvents mCombo As Access.ComboBox
Private WithEvents mForm As Access.Form
Private mFilterFieldName As String
Private mRsOriginalList As DAO.Recordset
Private mSearchType As SearchType
Private mHandleArrows As Boolean
Private mAutoCompleteEnabled As Boolean
Private mHandleInternationalCharacters As Boolean
Private mRowSource As String
Public Enum SearchType
  AnywhereInString = 0
  FromBeginning = 1
End Enum


'---------------------------------------- Properties --------------------------
'Only Needed for reassigning a new reocordsource. Not related to FAYT.  Still required to have a recordsource initially
Public Property Get RdSource() As String
  RowSource = mRowSource
  
End Property
Public Property Let RowSource(ByVal NewRowSource As String)
  Dim rs As DAO.Recordset
  mRowSource = NewRowSource
  'If mCombo.Recordset Is Nothing Then
     Set rs = CurrentDb.OpenRecordset(NewRowSource)
     Set mCombo.Recordset = rs
  'End If
  Set mRsOriginalList = mCombo.Recordset.Clone
End Property
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

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_Change()
  Call FilterList
  mAutoCompleteEnabled = True
 ' mCombo.Dropdown
End Sub
Private Sub mCombo_AfterUpdate()
  mAutoCompleteEnabled = True
  unFilterList
  'MsgBox "After" & mAutoCompleteEnabled
End Sub
Private Sub mForm_Current()
  Call unFilterList
End Sub
Private Sub mForm_Close()
   Call Class_Terminate
End Sub
Private Sub mCombo_Click()
  mAutoCompleteEnabled = False
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


'----------------------------------  Class Procedures ----------------------------
Public Sub InitalizeFilterCombo(TheComboBox As Access.ComboBox, FilterFieldName As String, _
                      Optional TheSearchType As SearchType = SearchType.AnywhereInString, _
                      Optional HandleArrows As Boolean = True, _
                      Optional HandleInternationalCharacters 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
   mHandleInternationalCharacters = HandleInternationalCharacters
   'HandleArrows allows you to use the arrow keys to move up and down without selecting the value
   mCombo.OnClick = "[Event Procedure]"
   If mHandleArrows = True Then
      mCombo.OnKeyDown = "[Event Procedure]"
      mCombo.OnClick = "[Event Procedure]"
   End If
   mFilterFieldName = FilterFieldName
   mSearchType = TheSearchType
   mForm.OnCurrent = "[Event Procedure]"
   mForm.OnClose = "[Event Procedure]"
   mCombo.OnGotFocus = "[Event Procedure]"
   mCombo.OnChange = "[Event Procedure]"
   mCombo.OnClick = "[Event Procedure]"
   mCombo.AfterUpdate = "[Event Procedure]"
   mForm.OnClose = "[Event Procedure]"
   
   With mCombo
      .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
  strText = Replace(strText, "'", "''")
  strText = Replace(strText, "#", "[#]")
  If mHandleInternationalCharacters Then
    strText = InternationalCharacters(strText)
  End If
  If mFilterFieldName = "" Then
    MsgBox "Must Supply A FieldName Property to filter list."
    Exit Sub
  End If
  'Debug.Print mAutoCompleteEnabled
  If mAutoCompleteEnabled = False Then Exit Sub
  If mSearchType = SearchType.FromBeginning Then
    strFilter = mFilterFieldName & " like '" & strText & "*'"
  Else
    strFilter = mFilterFieldName & " like '*" & strText & "*'"
  End If
  Set rsTemp = mRsOriginalList.OpenRecordset
  rsTemp.Filter = strFilter
  Set rsTemp = rsTemp.OpenRecordset
 
  If Not (rsTemp.EOF And rsTemp.BOF) Then
    rsTemp.MoveLast
    rsTemp.MoveFirst
    'Debug.Print rsTemp.RecordCount & " Count " & strFilter
  Else
    beep
    mAutoCompleteEnabled = True
  End If
  Set mCombo.Recordset = rsTemp
  If rsTemp.RecordCount > 0 Then
    
    mCombo.Dropdown
  End If
 
  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
'------------------------------------ To Handle International Characters  ---------------------------
Private Function InternationalCharacters(ByVal strText As String) As String
   InternationalCharacters = strText
   'If you type international turn first to english
    'Type international and get english
    InternationalCharacters = Replace(InternationalCharacters, "á", "a")
    InternationalCharacters = Replace(InternationalCharacters, "é", "e")
    InternationalCharacters = Replace(InternationalCharacters, "í", "i")
    InternationalCharacters = Replace(InternationalCharacters, "ó", "o")
    InternationalCharacters = Replace(InternationalCharacters, "ú", "u")
    InternationalCharacters = Replace(InternationalCharacters, "ü", "u")
    InternationalCharacters = Replace(InternationalCharacters, "ñ", "n")
    'Add others as necessary á, é, í, ó, ú, ü, ñ
    

   'Type english and get international
    InternationalCharacters = Replace(InternationalCharacters, "A", "[AÁÀÂÄaá]")
    InternationalCharacters = Replace(InternationalCharacters, "E", "[EÉÈÊËeé]")
    InternationalCharacters = Replace(InternationalCharacters, "I", "[IÍÌÎÏií]")
    InternationalCharacters = Replace(InternationalCharacters, "O", "[OÓÒÔÖ0oóøØ]")
    InternationalCharacters = Replace(InternationalCharacters, "U", "[UÚÙÛÜuú]")
    InternationalCharacters = Replace(InternationalCharacters, "N", "[NnñÑ]")
    InternationalCharacters = Replace(InternationalCharacters, "C", "[CcçÇ]")

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

zelarra821

Registered User.
Local time
Today, 11:12
Joined
Jan 14, 2019
Messages
809
I have tried the code in the form 03-E Dialogo IVA. I enclose the database so that you can see the failure it gives me.

Do not worry if something breaks, I warn you. Thank you so much for everything.
 

Attachments

  • Tienda BD.zip
    1 MB · Views: 64

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
Try this new procedure. Hopefully it does not break anything else
Code:
Public Sub InitalizeFilterCombo(TheComboBox As Access.ComboBox, FilterFieldName As String, _
                      Optional TheSearchType As SearchType = SearchType.AnywhereInString, _
                      Optional HandleArrows As Boolean = True, _
                      Optional HandleInternationalCharacters 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
   mHandleInternationalCharacters = HandleInternationalCharacters
   'HandleArrows allows you to use the arrow keys to move up and down without selecting the value
   mCombo.OnClick = "[Event Procedure]"
   If mHandleArrows = True Then
      mCombo.OnKeyDown = "[Event Procedure]"
      mCombo.OnClick = "[Event Procedure]"
   End If
   mFilterFieldName = FilterFieldName
   mSearchType = TheSearchType
   mForm.OnCurrent = "[Event Procedure]"
   mForm.OnClose = "[Event Procedure]"
   mCombo.OnGotFocus = "[Event Procedure]"
   mCombo.OnChange = "[Event Procedure]"
   mCombo.OnClick = "[Event Procedure]"
   mCombo.AfterUpdate = "[Event Procedure]"
   mForm.OnClose = "[Event Procedure]"
   
   With mCombo
      .AutoExpand = False
   End With
   If mCombo.Recordset Is Nothing And Not mCombo.RowSource = "" Then
     Set rs = CurrentDb.OpenRecordset(TheComboBox.RowSource)
     Set mCombo.Recordset = rs
   End If
   If Not mCombo.Recordset Is Nothing Then
     Set mRsOriginalList = mCombo.Recordset.Clone
   End If
   Exit Sub
errLabel:
    MsgBox Err.Number & " " & Err.Description
End Sub

This will allow you to not have to have a rowsource at start up.
This is what was modified.

Code:
  If mCombo.Recordset Is Nothing And Not mCombo.RowSource = "" Then
     Set rs = CurrentDb.OpenRecordset(TheComboBox.RowSource)
     Set mCombo.Recordset = rs
   End If
   If Not mCombo.Recordset Is Nothing Then
     Set mRsOriginalList = mCombo.Recordset.Clone
   End If

The intent for FAYT is on large text searches. I do not think you gain anything on these small numeric combos. You actually lose the autoexpand capability.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 05:12
Joined
May 21, 2018
Messages
8,525
thanks for debugging this. You have found a lot of things I would not have thought of.
 

Users who are viewing this thread

Top Bottom