471,893 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,893 software developers and data experts.

Highlight List Box

1,430 Expert 1GB
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()
  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)
  8.     If iLength = 0 Then
  9.         Exit Sub
  10.     End If
  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
  21. End Sub
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

May 8 '17 #1
6 877
32,470 Expert Mod 16PB
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 :-)

May 8 '17 #2
1,107 Expert 1GB
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
1,430 Expert 1GB
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.

May 9 '17 #4
32,470 Expert Mod 16PB
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
1,430 Expert 1GB
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.


May 11 '17 #6
32,470 Expert Mod 16PB
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.

Similar topics

2 posts views Thread by Colin Steadman | last post: by
1 post views Thread by Shyguy | last post: by
4 posts views Thread by pavanip | last post: by
reply views Thread by YellowAndGreen | last post: by
reply views Thread by zermasroor | last post: by

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.