ListBox Index Lookup

G

Guest

Howdy-

I'm fairly new to access (very new to VB), and I have not been able to
figure out something extremely basic that's going to make me look stupid. My
guess is that I'm completely missing something here, but here it goes:

All I want to do is lookup the index of a specific text item in a
listbox/combobox. Is there some native way to do this without a lot of code?
For example, we have the following listbox (and according indexes):

index text
---------------
0 JOHN
1 JANE
2 FRED
3 BILLY

For complexity, let's say I have a few columns:
ID, NAME, NICKNAME, ADDRESS, etc.

But I have all the columns other than NAME hidden (0";1";0";0";etc).

And of course, the ID is bound.

How do I lookup the index (not ID, but INDEX) of a specific NAME in the
LISTBOX?

For example, how would I lookup the index of FRED (and likewise have a value
of 2 returned for the index).

Is there a function where you can COMBOBOX.INDEXOF(column, value) and get
the index back?

Any help anyone can provide would be very, very much appreciated.

Most Sincerely,
JC
 
G

Guest

There is no built in function to do what you want. Here is a function you can
use to locate that information. Pass it the control and the value you want to
find. If it finds it, it will return the index of the item. If it does not,
it will return -1.

Here is an example of how to call it:

lngTheIndex = FindListNumber(Me.MyComboName, "FRED")

Function FindListNumber(ctl As Control, strFind As String) As Long
Dim lngItems As Long
Dim lngCtr As Long
Dim blnFoundIt As Boolean
lngItems = ctl.ListCount - 1
For lngCtr = 0 To lngItems
If ctl.ItemData(lngCtr) = strFind Then
FindListNumber = lngCtr
blnFoundIt = True
Exit For
End If
Next lngCtr
If Not blnFoundIt Then
FindListNumber = -1
End If
End Function
 
G

Guest

This looks like it would work perfectly, but for some reason it will not
successfully pass the control object to the function. It's just throwing out
null for the ctrl value when check within the function... And it's valid
before I pass it to the function.

Any ideas?

-JC
 
G

Guest

Ok... Please disregard the previous email. I was calling it from a utility
class I created, but wasn't using the preprended utility to indicate the
class I was calling the function from... Oddly enough... (magically) it did
not throw an error and the intellisense even picked up the function
description...

Who knows...

Thanks for the help Klatuu!

-JC
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top