473,472 Members | 2,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

filtering a listbox

21 New Member
I have searched and not found an answer to my question so I am posting.

I have a table named FieldData with a lookup field that gets data from another table TrainerInfo that full names in a single field (John Q. Public, Sr.) and it works great.

What I would like to know is when start to type the name "John", can the dropdown list filter names only with John in the field. Also, is there a way (similiar to searching windows explorer) where as I type the field is reduced to only matches of what I have typed no matter where the letter string. As an example typing Pub would narrow the list to John Q. Public, Al's Pub, Public Morals, etc.

The list has over a thousand records so this would make data entry more efficient when typing say John Smith and scrolling through hundres of names to get to John Jones, as well as if I only have an initial say J. Brown I can type Brown and find John Brown.

I know this happens in web pages and windows so I would like to know how to implement if possible.

Thanks
Jul 20 '15 #1
11 1914
jforbes
1,107 Recognized Expert Top Contributor
You might want to look at this code. It was working pretty well last time I ran it: http://bytes.com/topic/access/answer...em#post3787104 You'll also want to set the AutoExpand Property to No. That is addressed a little further down the post.
Jul 21 '15 #2
rich1838
21 New Member
I have tried to implement this and it does not appear to be working. I will attach the code from the query that the combo box uses. it has a filter built in already as trainers data is track specific therefore their name may appear multiple times in the table.

Expand|Select|Wrap|Line Numbers
  1. SELECT TrainerData.ID, TrainerData.Trainer, TrainerData.Track
  2. FROM TrainerData
  3. WHERE (((TrainerData.Track)=[Forms]![RaceEntryForm]![Track]));
  4.  
Jul 21 '15 #3
jforbes
1,107 Recognized Expert Top Contributor
You'll have to merge the Where Clause of the TrainerData.Track with what is typed in by the User... Something like this:
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  Option Explicit
  3.  
  4.  Private bLookupKeyPress As Boolean
  5.  
  6.  Private Sub cboLookup_Change()
  7.      Dim sSQL As String
  8.      Dim sNewLookup As String
  9.  
  10.      If Not bLookupKeyPress Then
  11.          sNewLookup = Nz(Me.cboLookup.Text, "")
  12.         sSQL = sSQL & "SELECT TrainerData.ID, TrainerData.Trainer, TrainerData.Track "
  13.         sSQL = sSQL & " FROM TrainerData "
  14.         sSQL = sSQL & " WHERE TrainerData.Track=[Forms]![RaceEntryForm]![Track] "
  15.         If Len(sNewLookup) <> 0 Then
  16.            sSQL = sSQL & " AND TrainerData.Trainer LIKE '*" & sNewLookup & "*'"
  17.          End If
  18.          Me.cboLookup.RowSource = sSQL
  19.          Me.cboLookup.Dropdown
  20.      End If
  21.      bLookupKeyPress = False
  22.  End Sub
  23.  
  24.  Private Sub cboLookup_KeyDown(KeyCode As Integer, Shift As Integer)
  25.      Select Case KeyCode
  26.          Case vbKeyDown, vbKeyUp
  27.              bLookupKeyPress = True
  28.          Case Else
  29.              bLookupKeyPress = False
  30.      End Select
  31.  End Sub
  32.  
  33.  Private Sub cboLookup_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  34.      bLookupKeyPress = True
  35.  End Sub 
  36.  
At anytime while you are getting this to work, you can post your code and we can help you out.
Jul 21 '15 #4
rich1838
21 New Member
May be simple but just to confirm. Is this going in the on change or AfterUpdate property. Reading post not sure
Jul 21 '15 #5
jforbes
1,107 Recognized Expert Top Contributor
cboLookup was the name of the Control in the example I played around with. You'll probably need to update all the occurances of cboLookup with the Name of your Control.

To answer your question, it's the OnChange Event:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cboLookup_Change()
For a little more explanation, there are three events involved:
  1. OnChange - Where the ComboBox Rowsource is updated based on what has been entered so far.
  2. OnKeyDown - To flag the Input as User input and not something changed by Access, like navigating to a different row.
  3. OnMouseDown - To show the Dropdown list when the user clicks in the Combobox.
Expand|Select|Wrap|Line Numbers
  1. Private bLookupKeyPress As Boolean
will need to be put at the top of the Form's code. It's the Flag that is set whenever a user performs some input in the ComboBox.
Jul 21 '15 #6
rich1838
21 New Member
Worked like a champ. Thanks for the help!
Jul 22 '15 #7
rich1838
21 New Member
Stuck again! The search works great, however I now have a problem where when I go to the next record, the last chosen item is in the field instead of going blank.
Jul 22 '15 #8
jforbes
1,107 Recognized Expert Top Contributor
I think I see what you are talking about. The Dropdown would still be filtered to what was used on the previous record. It would need to be reset as the User navigates through the records. I played around with it a bit and came up something like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Current()
  2.     Dim sSQL As String
  3.     sSQL = sSQL & "SELECT TrainerData.ID, TrainerData.Trainer, TrainerData.Track "
  4.     sSQL = sSQL & " FROM TrainerData "
  5.     sSQL = sSQL & " WHERE TrainerData.Track=[Forms]![RaceEntryForm]![Track] "
  6.     If Me.cboLookup.RowSource <> sSQL Then Me.cboLookup.RowSource = sSQL
  7. End Sub
This will reset the RowSource of the ComboBox whenever the User navigates to a different record. I put the Test in there so that the RowSource is only Reset if it needs to be. It should run better that way.

To make this a bit cleaner, you may want to relocate this into it's own function and call the function from the OnCurrent event.
Jul 23 '15 #9
rich1838
21 New Member
Placed it in OnCurrent and get Compile Error. Method of Data Member not found. TrainerLookup is my control box name
Expand|Select|Wrap|Line Numbers
  1. Option Compare Database
  2.  
  3. Private Sub Form_Current()
  4.  
  5.     Dim sSQL As String
  6.     sSQL = sSQL & "SELECT TrainerData.ID, TrainerData.Trainer, TrainerData.Track "
  7.     sSQL = sSQL & " FROM TrainerData "
  8.     sSQL = sSQL & " WHERE TrainerData.Track=[Forms]![RaceEntryForm]![Track] "
  9.     If Me.TrainerLookup.RowSource <> sSQL Then Me.TrainerLookup.RowSource = sSQL
  10. End Sub
Jul 23 '15 #10
jforbes
1,107 Recognized Expert Top Contributor
Hmmm, looks like it's having trouble resolving something. It could be the where clause. Might want try commenting out line 8 just to rule it out. You may also want to check line 6 and 7 to make sure they match your Column names and Table name.

The idea behind this it to reset the RowSource so that it is wide open. So you would want to set it to whatever you set it to for the OnCurrent Event, minus additional filtering from what the user typed in. The code I posted is just a guess based on what you provided.

If you are still stuck after looking at it again, maybe post all the code you have and we can try to get it working.

... I'm about to go on a 5 day vacation, so if I can't help you, hopefully someone else can. I hope to check in from time to time, but It might be difficult while canoeing. =)
Jul 23 '15 #11
rich1838
21 New Member
I just realized this is on a subform and that has no OnCurrent. The code was placed in the RaceEntryForm which has the child FieldData Subform. All the tables and fields are correct
Jul 23 '15 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: William Wisnieski | last post by:
Hello Everyone, I have a main form with a datasheet subform that I use to query by form. After the user selects two criteria on the main form and clicks the cmdShowResults button on the main...
1
by: diskoduro | last post by:
Hi! I'm trying to get help to an unexpected problem that has appeared while I was writing a new application. I want to opeon a report of product sales by filtering previously from a listbox in a...
2
by: Colleyville Alan | last post by:
I have a listbox that is populated with info and when the user selects categories of things from the list, it brings up detailed items from which to choose. I want to be able to put a filter on...
6
by: Alpha | last post by:
I have a listbox with datasource from a dataview. When a user selects a different item in a combobox then I need to refresh the listbox to the appropriate listing based on that combobox's selected...
1
by: hackerslacker | last post by:
I have an ordering form that use two combo boxes to filter down the records of a Products table. This worked fine with the after_Update of the first filtering the records and creating the...
3
by: Prochot | last post by:
I'm trying to design a database to track projects and the associates assigned to them. I have almost no experience with this. I have three tables: -Projects -Associates -Assignments where...
1
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a ListBox on my Visual Studio C# form with several entries (14,000). I have to put a search field on the form. As text is entered into the search field, I want the ListBox to remove...
2
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
I've got a ListBox on my form with lots of data in it (14000 entries). I have to put a search field on the form. As text is entered into the search field, I want the ListBox to remove entries...
2
by: lhsiber | last post by:
I am new to access and am having a problem with filtering. Here is a little bit of my setup: I have a main form that has a listbox so that users can choose one or many groups in which to display...
2
by: Jeroen3131 | last post by:
I have a Form with four comboboxes (cboSelectDepartment, cboSelectOperation, cboSelectModel and cboSelectVariant), a Listbox (selectionList) and a Button (cmdSelectStep). The Listbox displays a...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.