Results 1 to 2 of 2

Thread: Passing a value from listbox to textbox when listbox has 2 values :( ?

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2004
    Posts
    101

    Question Passing a value from listbox to textbox when listbox has 2 values :( ?

    I have a listbox, that I want to pass a .text value from to a textbox.
    Problem is, The listbox is holding two values (as passed by 2 fields from a recordset) like this...

    Code:
    For i = 0 To rstCust.RecordCount - 1
    
    lstCust.AddItem rstCust.Fields(0).Value & " " & rstCust.Fields(1).Value
    rstCust.MoveNext
    
    Next
    Which displays the list like this:

    001 Smith
    002 Jones
    003 Black

    etc.


    When I click on the listbox, I want the value I click on to go to a textbox called txtCustID, but I dont want to send both values to it.

    I only want to send the rstCust.Fields(0).Value to the textbox (which would be 001, 002, 003 etc. but not the name).


    Can anyone help me do this?

    Thanks

  2. #2
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    Couple of ways to go here...you could use the split function which returns an array. It takes as a paremeter the delimiter which in the example you gave is a space. Are all the second column values only one word?

    Code:
    dim strArray as Variant
    
    strArray = Split(ListBox1.List(ListBox1.ListIndex), " ")
    Text1.Text = strArray(0)
    if there are spaces on the second column, I have done this in the past:

    when adding items to the listbox concatenate like so:

    Code:
    ListBox1.AddItem rs.Fields(0).Value & vbTab & rs.Fields(1).Value
    
    ...
    'then you could do
    
    strArray = Split(ListBox1.List(ListBox1.ListIndex), vbTab)
    
    ...
    
    'or you could use the ListBox.ItemData property which is also a collection like .Item:
    
    ListBox1.AddItem rs.Fields(0).Value & " " & rs.Fields(1).Value
    ListBox1.ItemData(ListBox1.NewIndex) = rs.Fields(0).Value
    
    'so when they click on the item, you pass the index of the listbox to the ItemData collection
    
    ListBox1.ItemData(ListBox1.ListIndex)
    
    'the ItemData way is my personal favorite
    hope that helps - cheers
    "Knowledge is gained when different people look at the same information in different ways"

    - Louis Pasteur

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width