By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,122 Members | 921 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,122 IT Pros & Developers. It's quick & easy.

Highlight List Box

PhilOfWalton
Expert 100+
P: 1,430
Simplified Version:

I Have a table of Entrants with EntantID as the Primary Key, EntrantName and AddressID as a Foreign Key

I Have a table of Addresses with AddressID as the primary Key and Address as text.

I have a list box (LstAddressID) showing all the addresses, so when I add a new Entrant, if the address already exists, I select it from the list box and the AfterUpdate adds the AddressID into the Entrants Table.
No Problem.

If I have to add a new address, I start typing into an unbound field called NewAddress1. Now I am assuming that this really is a new Address, but it may not be, so what I want to happen is the LstAddressID to highlight the first address that partially matches the letters typed into NewAddress1.

I have tried
Expand|Select|Wrap|Line Numbers
  1. Private Sub NewAddress1_Change()
  2.  
  3.  'start where we are in the list and go forward
  4.     Dim iLength As Integer
  5.     Dim irow As Integer
  6.     iLength = Len(Me.NewAddress1.Text)
  7.  
  8.     If iLength = 0 Then
  9.         Exit Sub
  10.     End If
  11.  
  12.     For irow = 0 To Me.LstAddressID.ListCount - 1
  13.         Me.LstAddressID.SetFocus
  14.         Me.LstAddressID.ListIndex = irow
  15.         Me.NewAddress1.SetFocus
  16.         If Left(Me.LstAddressID.Column(1), iLength) = Me.NewAddress1 Then
  17.             Exit Sub
  18.         End If
  19.     Next
  20.  
  21. End Sub
  22.  
Me.LstAddressID.ListIndex = irow doesn't work without setting the focus to the listbox, and of course once you move the focus away from the list box, the AfterUpdate fires. Not what is wanted.

Any ideas please

Phil
May 8 '17 #1
Share this Question
Share on Google+
6 Replies


NeoPa
Expert Mod 15k+
P: 31,419
Hi Phil.

I'm not sure about this but I believe, with a ComboBox at least so may be similar for non-multi-value ListBoxes, that you can set the .Value property of the control without it having the focus. That's how I normally select an item in a ComboBox programmatically.

If that gives no joy then you may be out of luck. Of course you could always set a flag and allow the AfterUpdate procedure to skip its code when that flag's set. Not sure if that suits but could be a worst case scenario.

Anyway, good luck with your project :-)

-Ade.
May 8 '17 #2

jforbes
Expert 100+
P: 1,107
You might try using a DCount() (or DLOOKUP to get the ID) with the same WHERE clause as the ListBox while adding the criteria for LEFT() matching. Then navigate to the ListBox, or set it's .Value only if you get a hit.
May 9 '17 #3

PhilOfWalton
Expert 100+
P: 1,430
Having slept on it, I think I might replace the list box with a continuous subform, and filter the subform using the OnChange of the letters in the TextBox NewAddress1

I'll let you know results in due course, but your remarks at least prompted me to think outside the box, so as ever, thanks guys.

Phil
May 9 '17 #4

NeoPa
Expert Mod 15k+
P: 31,419
Always a pleasure Phil.

Frankly I prefer a rethink than simply following advice. It indicates a greater level of engagement. Good for you.
May 9 '17 #5

PhilOfWalton
Expert 100+
P: 1,430
Thanks, NeoPa & Ade.

The unbound subform works perfectly.
The OnCurrent of the main form used the AddressID to find the appropriate Address in the subform.

If I start to type a new address in, I apply a filter to the subform, based on the letters typed in so far, so the subform shows the best match. If it is a new address, the subform is blank.

Perfect.

Phil
May 11 '17 #6

NeoPa
Expert Mod 15k+
P: 31,419
Phil:
Perfect.
That's what we aim for. Very happy to see you achieve it on this occasion. Nice work :-)
May 11 '17 #7

Post your reply

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