Fill text field from listbox selection (1 Viewer)

speedman_2001

Registered User.
Local time
Today, 08:09
Joined
May 23, 2008
Messages
30
Ok so here goes. I have a page with a form on the left and list box of users on the right. The list box is populated by a php mysql query as soon as the page is loaded and the page is used to administer users for the site. What I'm trying to do is make the page a little more interactive. As soon as the user chooses/clicks on a user from the list, I would like the user's username, first name, and last name to populate into the correct fields on the form on the left. All of the information needed for the text fields is contained in the list, but it appears I can only pass the index value from the list to the onclick event of the list box. Am I looking at this completely the wrong way or is there something I've missed?
 

NickHa

CITP
Local time
Today, 13:09
Joined
Jan 29, 2012
Messages
203
Use the ListBox Columns property to retrieve the individal item column data. The syntax is
Code:
FirstCol = Listbox.Column(0)
SecondCol = Listbox.Column(1)
 

speedman_2001

Registered User.
Local time
Today, 08:09
Joined
May 23, 2008
Messages
30
I think I understand what you're getting at, but how to implement it is another. I pretty new to the web development and learning as I go. Here's the php that's being used to build the list and output/alert the information that is selected.

PHP:
	echo '<select name="usernameselection" size="14" onclick="alert(this[selectedIndex].value)"; value=""></option>';
	while ($lister=mysql_fetch_array($UserList))
	{
		echo "<option value=$lister[UserID]>  $lister[UserName]  $lister[UserFirstName]  $lister[UserLastName]      </option>";
	}
	echo "</select>";

Outputting the first column isn't a problem as it is also the index, it's when I need to get columns 2 (UserName), 3 (FirstName), and 4 (LastName) outputted is where I'm having issues.
 

NickHa

CITP
Local time
Today, 13:09
Joined
Jan 29, 2012
Messages
203
Outputting the first column isn't a problem as it is also the index, it's when I need to get columns 2 (UserName), 3 (FirstName), and 4 (LastName) outputted is where I'm having issues.
That's exactly what the code I gave you does!:)
In the Click event of your list box, you simply use the Column indices to get the fields, like this:
Code:
Private Sub lsbItems_Click()
UserName = lsbItems.Column(1)
FirstName = lsbItems.Column(2)
LastName = lsbItems.Column(3)
End Sub
Note I have used indices 1, 2 & 3 and not 2, 3 & 4 - this is because the columns are zero-relative (the first column is 0).
You substitute your listbox name wherever I used 'lsbItems'.

Note that the ListIndex gives you the item which was clicked. If you use multi-select, then its a bit more involved, but I don't think you need that.
 

NickHa

CITP
Local time
Today, 13:09
Joined
Jan 29, 2012
Messages
203
I have just realised that the code I gave you doesn't work!:eek: My apologies. I was thinking about Combo boxes and applying the same logic to listboxes which is incorrect.

I have examined the properties of a bound listbox I have and it does not show the columns anywhere. I also have an unbound listbox which has a Value List as its RowSource type. in this case, I can retrieve the selected item from the RowSource property once the values are loaded. It may be that this latter case will work for you, so I've included a solution at the end of this post.

The only alternative suggestion I can offer are:
1. Use a Value List as the Row Source Type and add the PHP items to it using the AddItem method. When the user clicks on an item, you can retieve the RowSource to a string array and use the listindex to reference the elements you need (see example below).
2. Use a Combo box for your PHP data - in which case the column selection does work or
3. Create a table of your PHP inputs and use the table as your RowSource. As you say, you can get the index of the selected item from the listbox and you can then use this to retrieve the item details from the table.

I'm sorry if I have caused you grief! I'm surprised no-one else jumped on me for this error!

Example of listbox with Value List:
Code:
Dim i As Integer
Dim varArray As Variant
varArray = Array("one", "two", "three", "four", "five")
Rem the following clears the listbox entries
Me.lsbItems.RowSource = vbNullString
Rem the following adds values from an array to the listbox with two columns
For i = 0 To UBound(varArray)
  Rem first entry looks like this: "01" "One" (columns 0 & 1 respectively)
  Me.lsbItems.AddItem Format(i + 1, "00") & ";" & StrConv(varArray(i), vbProperCase)
Next
In the click event of the listbox:
Code:
Private Sub lsbItems_Click()
Dim strItems() As String
Rem retrieve the rowsource to an array - elements are delimited by ";"
strItems = Split(Me.lsbItems.RowSource, ";")
Rem retrieve columns 0 & 1 for the item selected in the listbox
Rem for a 2-column listbox, column 0 data are in even numbered elements of the array (0, 2, ...), column 1 data are in odd numbered elements of the array (1, 3, ...)
MsgBox "Selected item:" & vbCr & "Column 0 is '" & strItems(Me.lsbItems.ListIndex * Me.lsbItems.ColumnCount) & "'" & vbCr & "Column 1 is '" & strItems(Me.lsbItems.ListIndex * Me.lsbItems.ColumnCount + 1) & "'"
End Sub
 

speedman_2001

Registered User.
Local time
Today, 08:09
Joined
May 23, 2008
Messages
30
Hey Nick

Sorry it took so long to get back to you, and thank you for the help. What I ended up doing was calling a java script to populate 1 text box, which in turn called another script to populate the next box and so on and so on until all text boxes I needed populated were. Not the prettiest thing in the world since it ends up making calls to 4 php pages (1 for each text box) in order to query the database and get the appropriate information.
 

NickHa

CITP
Local time
Today, 13:09
Joined
Jan 29, 2012
Messages
203
Use the ListBox Content residence to recover the individal product primary data
Would you explain this please? I don't understand what you mean.:confused:

The problem speedman_2001 is having is that there is no column(index) property in a list box. If this is present in some other guise, then I can't find it (other than the value list in the RowSource). If you look at my original response, you'll see I said the same as you, but I later found it didn't work (I was confusing it with Combo boxes).

So if you know something which is escaping me, I'd like to know about it (please!):)
 

Users who are viewing this thread

Top Bottom