Results 1 to 10 of 10
  1. #1
    RoccoV is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2016
    Posts
    5

    Need help with my custom made autocomplete dropdown

    Hi, I'm trying to set up an auto complete functionality on a Combo Box; you know where it updates the list as you type. I've figured out this is not possible with a Combo Box so I move on to a Text Box and List Box combination.



    It's starting to come together but I've come across a problem I can't solve to get it to work the way I want.

    Here's what I've done so far:
    • Created the bound Text Box.
    • Added an unbound List Box below it with a query in the Row Source that filters the list based on the Text Box contents.
    • In the Text Box On Change event it trigger the List Box Requery to update the list.
    • In the List Box On Click event it sets the Text Box to whatever was clicked and returns focus to the Text Box.


    This works fine. What I wanted to do next is start with the List Box hidden and make it visible as I edit the Text box. That was easy enough but the problem I have is trying to re-hide the List Box when the Text Box looses focus. If I use the Text Box Lost Focus event to hide the List Box I come across the problem that if I try to click a value in the List Box the Text Box looses focus and the List Box disappears without the On Click event for the List Box Firing.

    So in summary: Is there any way to hide the List Box when the Text Box looses focus but not when I click the List Box? I haven't been able to find anyway. The only thing I haven't tried, that I can think of, is to have some sort of global Lost Focus event, which seems clumsy if it's even possible.

    Thanks in advance.

  2. #2
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    I'm trying to set up an auto complete functionality on a Combo Box; you know where it updates the list as you type. I've figured out this is not possible with a Combo Box
    it is, why do you think it isn't?

  3. #3
    RoccoV is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2016
    Posts
    5
    Because when I try to update the list of a Combo Box using Requery, as I'm editing the edit box part of it, I get an error saying something like "You have to save the field first". And every thing I've seen online indicates it's not possible.

    Edit: I should probably point out that the List is populated with the values in the same field as the text box. It's not a separate list.

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    so you don't mean autocomplete, you want to modify the rowsource based on what has been typed by the user. Only thing I don't understand is if your list is say

    Aaaa
    Bbbbb
    Ccc
    Dddddd
    ...
    ..

    and the user types C, which then displays Ccc - why would you want to modify the list and what to?

  5. #5
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    but back to your original question, suggest rather than trying to hide the control, set it's height to zero, whether the control has focus or not won't matter

  6. #6
    RoccoV is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2016
    Posts
    5
    and the user types C, which then displays Ccc - why would you want to modify the list and what to?
    I thought that would be obvious. The example you showed only had 1 match but if there were multiple matches it would match the first one but you could click the second or third etc.

    but back to your original question, suggest rather than trying to hide the control, set it's height to zero, whether the control has focus or not won't matter
    That's a good idea. I'll try that and let you know how it goes.

  7. #7
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    The example you showed only had 1 match but if there were multiple matches it would match the first one but you could click the second or third etc.
    agreed so for example list is

    Aaaaaa
    Caaaaa
    Cabbbb
    Cacccc
    Cabcdd

    user types C - Caaaa is selected
    user then types a - no change
    use then types c - Caccc is selected

    So how does your updating rowsource differ from this behaviour?

  8. #8
    RoccoV is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2016
    Posts
    5
    Quote Originally Posted by Ajax View Post
    agreed so for example list is

    Aaaaaa
    Caaaaa
    Cabbbb
    Cacccc
    Cabcdd

    user types C - Caaaa is selected
    user then types a - no change
    use then types c - Caccc is selected

    So how does your updating rowsource differ from this behaviour?
    True I guess if I'm only filtering from the beginning of the text then it would essentially be the same, only a little more neater. I guess it's only useful if you filter within the whole text to bring separate items together, example.

    111 beat
    234 dog
    345 bat
    456 better

    User types b -
    111 beat
    345 bat
    456 better

    User types be -
    111 beat
    456 better

    etc.

    Now I just have to decide if I need it or not.

    I've done the work so I might post what I've done for future reference.

  9. #9
    RoccoV is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jan 2016
    Posts
    5
    So this is what I've come up with. I've tried to keep it as simple as possible. This adds a drop down box with filtered results as you type in a text box. The list appears as you start typing and disappears when you move to another control. If you want it to appear when the text box gets focus you will need to add the On Focus event to the text box and copy the code from the On Change event.
    1. Create or use existing bound text box. Mine is called txtDestination and is bound to the Destination field of my table.
    2. Add an unbound Listbox below it. I've called mine lstDestinationDrowpdown and add a query to the Row Source property of the Listbox. Mine looks like this.
    Code:
    SELECT DISTINCT [Group Journeys].Destination FROM [Group Journeys] WHERE ((([Group Journeys].Destination) Like "*" & [Forms]![Group Journeys1]![txtDestination].[Text] & "*")) ORDER BY [Group Journeys].Destination;
    In this example my table is called Group Journeys and my form is called Group Journeys1. Sorry for the confusing names. They were like that already. I might fix them in the future.
    3. Add the On Change and On Lost Focus events to the Textbox.
    4. Add the On Click and On Enter events to the Listbox.
    5. Copy the following vb code
    Code:
    Option Compare Database
    
    ' Updates the dropdown list and shows it
    Private Sub txtDestination_Change()
        Me![lstDestinationDropdown].Requery
        Call Show_Custom_ListBox_Dropdown(Me![lstDestinationDropdown])
    End Sub
    
    ' Closes the dropdown when the destination box looses focus
    Private Sub txtDestination_LostFocus()
        Call Hide_Custom_ListBox_Dropdown(Me![lstDestinationDropdown])
    End Sub
    
    ' Sets the Destination to the selected item and returns focus to the Destination box
    Private Sub lstDestinationDropdown_Click()
        Me![txtDestination] = Me![lstDestinationDropdown]
        Me![txtDestination].SetFocus
        Call Hide_Custom_ListBox_Dropdown(Me![lstDestinationDropdown])
    End Sub
    
    ' Stops the dropdown from disappearing when clicking in it.
    Private Sub lstDestinationDropdown_Enter()
        Call Show_Custom_ListBox_Dropdown(Me![lstDestinationDropdown])
    End Sub
    
    ' Custom function to show the dropdown Listbox with formatting
    Private Sub Show_Custom_ListBox_Dropdown(Box As ListBox)
        With Box
            If .ListCount = 0 Then
                Call Hide_Custom_ListBox_Dropdown(Box)
            Else
                If .ListCount < 7 Then
                    .Height = .ListCount * 290 + 100
                Else
                    .Height = 7 * 290 + 95
                End If
                Box.SpecialEffect = 4
            End If
        End With
    End Sub
    
    ' Custom function to hide the dropdown list box and remove formatting that would make it still visible.
    Private Sub Hide_Custom_ListBox_Dropdown(Box As ListBox)
        Box.Height = 0
        Box.SpecialEffect = 0
    End Sub
    That's about it. Change the names as necessary. The top 4 subs are the event handlers and the last 2 are custom functions to show and hide the list box.

  10. #10
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    Ah - now understand what you are trying to do differently.

    As you say you would have to use a separate control to do this

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Autocomplete in a subform does not work
    By CarlS in forum Forms
    Replies: 2
    Last Post: 06-21-2015, 06:41 AM
  2. autocomplete text
    By bendamari in forum Access
    Replies: 2
    Last Post: 05-16-2015, 08:37 PM
  3. Replies: 1
    Last Post: 07-09-2014, 03:36 PM
  4. Replies: 2
    Last Post: 05-27-2014, 11:32 AM
  5. ComboBox Autocomplete
    By AdrianKitchen in forum Forms
    Replies: 3
    Last Post: 10-13-2009, 11:12 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums