Return value from list box

S

Sam

Hey everybody.

It's a simple thing that I want to do. I have list box populated from table.
I want to be able to choose a sertain row from list box and according to
selection put ID field's value to variable. Example below.

NAME ID
s | 1
f | 2
g | 3

So from example list box if I choose the second row (NAME=f) I want to put
'2' to variable. I'm pretty sure this is not hard to do, but I haven't done
VBA for a long time. Can someone help me a bit here?
 
C

Carl Rapson

Assuming the list box's Multi Select property isn't enabled, you can grab
the value from the second column in the current selection like this:

var = listbox.Column(1)

where 'var' is the name of your variable and 'listbox' is the name of your
listbox control. The columns in the listbox are zero-based, so the second
column is 1.

Carl Rapson
 
R

Ron2006

I didn't code it in the first place, but in a program I maintain that
uses listbox extensively it has

Tracking = CNDAPPBx.Column(6, CNDAPPBx.ListIndex + 1)

It is stating the column (0 based) AND the row.

Might be worth a try.

Ron
 
S

Sam

Hey guys and thank you for help. I might not have been clear enough. But I
want the program automatically detect the ID field depending on the the field
that is chosen from list box. So if I click second row I need '2' on the
variable. But if I click third row I want '3' or what ever is the current ID
of the record. The purpose of this all is to select a project name from list
and get the ID saved. In the next form according to this ID I want to see
detailed information of the record that is chosen from the list box.
 
R

Ron2006

Hey guys and thank you for help. I might not have been clear enough. But I
want the program automatically detect the ID field depending on the the field
that is chosen from list box. So if I click second row I need '2' on the
variable. But if I click third row I want '3' or what ever is the current ID
of the record. The purpose of this all is to select a project name from list
and get the ID saved. In the next form according to this ID I want to see
detailed information of the record that is chosen from the list box.
--
-Sam









- Show quoted text -

That is what both of us are saying.

In the afterupdate of the listbox the set the variable via either my
example or Carl's.

me.variablename = me.listbox.Column(1)
or
me.variablename = me.listbox.Column(1,me.listbox.listindex + 1)

This is true if the second field in the query for your listbox is the
field that you want.

Ron
 
S

Sam

Ok guys, you are absolutely right. Sorry about the confusion. I made it work
now. I have one additional question. If the ID field would not show in
listbox how should I get the ID value by clicking the name field on list box?
 

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