Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Listbox Help

Status
Not open for further replies.
May 29, 2003
3
US
I am trying to pass multiple selections from a simple ListBox to a field in my table. If I have it set to only allow one selection, it passes the info with no problem, but if I try and select more than one option, the field remains blank

Thanks for the help
 
Hi!

A multiselect list box will always have a null value. To use the values selected in this type of list box you will need to use VBA. Let us know exactly what you are attempting and we can most likely help.



Jeff Bridgham
bridgham@purdue.edu
 
Ok heres what Im trying to do: I have a list box that gets its value's from a table.

The table contains 53 country names, the user needs to be able to select multiple countries, that will in turn be put in my Main table (tblMain) in a field called country.

This needs to be able to be changed by using the same form and listbox at a later time.

Im not used to using Access forms, I normally just use databases for web programming, so I am having some difficulties doing this in Access alone.

thanks for the help and quick reply!
 
Try this

country = ""
For iList = 0 To ListBox.ListCount - 1
If ListBox.Selected(iList) = True Then
country = country & ListBox.ItemData(iList)
End If
Next
 
Hi!

Sorry about the delay. For a multiselect box do:

Dim varRow As Variant

For Each varRow In YourListBox.ItemsSeleced
If Nz(Len(YourTextBox.Value), 0) = 0 Then
YourTextBox.Value = YourListBox.Columns(0, varRow)
Else
YourTextBox.Value = YourTextBox.Value & ";" & YourListBox.Columns(0, varRow)
End If
Next varRow

hth


Jeff Bridgham
bridgham@purdue.edu
 
Maybe Im a moron or totally blind, but Im not getting this to work. My fields are Listbox is lbCountry and the textfield is txtCountry
 
Hi!

Well your code should read:

Dim varRow As Variant

For Each varRow In lbCountry.ItemsSelected
If Nz(Len(txtCountry.Value), 0) = 0 Then
txtCountry.Value = lbCountry.Columns(0, varRow)
Else
txtCountry.Value = txtCountry.Value & ";" & lbCountry.Columns(0, varRow)
End If
Next varRow

I had left out a t in ItemsSelected so if you just copied and pasted that may have caused the problem.

hth


Jeff Bridgham
bridgham@purdue.edu
 
With your control names:

txtCountry = ""
For iList = 0 To lbCountry.ListCount - 1
If lbCountry.Selected(iList) = True Then
txtCountry = txtCountry & lbCountry.ItemData(iList)
End If
Next

or if you want a semicolon inserted:
txtCountry = txtCountry & ";" & lbCountry.ItemData(iList)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top