Adding values to a 2-column listbox

G

Guest

I'm trying to add 2 colums of values to a listbox by selecting the combo box
(which has 2 columns). I have in the afterupdate of the combobox:

If Me!lstCompany.ListCount <= 0 Then

Me!lstCompany.RowSource = Me!cboCompany.Column(1)
Else
Me!lstCompany.RowSource = Me!lstCompany.RowSource & ";" &
Me!cboCompany.Column(1)
End If

this works great for 1-column, but what's the syntax for adding 2 columns?
Or is it possible? thanks in advance.
 
M

Marshall Barton

Samantha said:
I'm trying to add 2 colums of values to a listbox by selecting the combo box
(which has 2 columns). I have in the afterupdate of the combobox:

If Me!lstCompany.ListCount <= 0 Then

Me!lstCompany.RowSource = Me!cboCompany.Column(1)
Else
Me!lstCompany.RowSource = Me!lstCompany.RowSource & ";" &
Me!cboCompany.Column(1)
End If

this works great for 1-column, but what's the syntax for adding 2 columns?
Or is it possible? thanks in advance.


I'm having trouble imagining what UI purpose this might
serve, but I guess that's neither here nor there.

Assuming you have set the ColumnCount and RowSourceType
properties correctly, you just need to add the other value
along with another ";" separator:

If Me!lstCompany.ListCount <= 0 Then
Me!lstCompany.RowSource = Me!cboCompany.Column(1) _
& ";" & Me!cboCompany.Column(2)
Else
Me!lstCompany.RowSource = Me!lstCompany.RowSource _
& ";" & Me!cboCompany.Column(1) _
& ";" & Me!cboCompany.Column(2)
End If

If you're using AXP or A03, then you should explore using
the AddItem method. It may be a little easier than what you
have (at least you would not need the If.
 
G

Guest

thanks Marsh. That works great!
Now, how would I remove a selected item on the list box (unbound to any
tables or query)? I figure that I can find out which item is being selected
with the statement: Me!lstCompany.ListIndex. But what's the method to remove
the line once I figured the index number? Any help is very much appreciated.
thanks again.
 
M

Marshall Barton

If you're using the latest versions of Access the RemoveItem
method would be the easy way to go.

In earlier versions, you have to parse the value list string
(using InStr), counting semicolons, to find the item you
want to get rid of. Then piece the parts you want to keep
back together. Alternatively, if you have saved the values
in an array or collection, you could reconstruct the list
from the saved values. This whole concept is a pain and one
of the reasons that I never use a value list for anything
beyond trivial values such as Male/Female, etc. Small
lookup tables are easier to filter than going through all
this rigmarole.
 

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