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 w/ two adjoining fields, I want to extract just one

Status
Not open for further replies.

Neenas19

Technical User
Apr 10, 2002
22
US
Hello,
I have a listbox which is populated with two fields from a database. For example:
list1.AddItem rs.Fields("ID") & " - " & rs.Fields("Name")
So that the list in the listbox will look like this:
57-John Doe
89-Jane Doe
9447-Joe Smith
Etc....
What I would like to do is then extract ONLY the ID from the above list. So if I selected both John and Jane, I would have 57,89. Does this make sense?
I have tried to loop through the selections, but I get the entire concatenated selection, when really I only want the first part. Any ideas?
Thank you!
 
Look at the Instr() and Left() functions in VBHelp - that should do the trick
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
You can use the Split function..

Dim lststr() As String
Dim code() As String

For x = 0 To List1.ListCount - 1
If List1.Selected(x) Then
lststr = Split(List1.List(x), "-")
Debug.Print lststr(0) 'ID
End If
Next
 
Actually, if the ID number is with-in the range of a long integer, you can use the ItemData property of the list box:

Dim i As long

With rs
Do until .EOF
list1.AddItem .Fields("ID") & " - " & .Fields("Name"),1
list1.ItemData(1) = .Fields("ID")
i = i +1
.MoveNext
Loop
End With

Then, when an item is selected:

Dim ID As String
If List1.ListIndex >= 0 Then ID = List1.ItemData(List1.ListIndex) [/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CCLINT, Thank you much, this worked great!
 
You are welcome.
Just one problem: In copying the code, I made a mistake. I forgot to replace the indexs with the counter variable:

list1.AddItem .Fields("ID") & " - " & .Fields("Name"), i
list1.ItemData(i) = .Fields("ID")
[/b][/i][/u][sub]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top