Go Back   Access World Forums > Apps and Windows > Web Design and Development

 
Reply
 
Thread Tools Rating: Thread Rating: 35 votes, 5.00 average. Display Modes
Old 03-09-2012, 06:18 AM   #1
speedman_2001
Newly Registered User
 
Join Date: May 2008
Location: Nebraska
Posts: 30
Thanks: 4
Thanked 0 Times in 0 Posts
speedman_2001 is on a distinguished road
Fill text field from listbox selection

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?

speedman_2001 is offline   Reply With Quote
Old 03-09-2012, 06:26 AM   #2
NickHa
CITP
 
Join Date: Jan 2012
Location: South-East England
Posts: 203
Thanks: 0
Thanked 33 Times in 33 Posts
NickHa is on a distinguished road
Re: Fill text field from listbox selection

Use the ListBox Columns property to retrieve the individal item column data. The syntax is
Code:
FirstCol = Listbox.Column(0)
SecondCol = Listbox.Column(1)
NickHa is offline   Reply With Quote
Old 03-09-2012, 07:07 AM   #3
speedman_2001
Newly Registered User
 
Join Date: May 2008
Location: Nebraska
Posts: 30
Thanks: 4
Thanked 0 Times in 0 Posts
speedman_2001 is on a distinguished road
Re: Fill text field from listbox selection

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 Code:

    
echo '<select name="usernameselection" size="14" onclick="alert(this[selectedIndex].value)"; value=""></option>';
    while (
$lister=mysql_fetch_array($UserList))
    {
        echo 
"<option value=$lister[UserID]>&nbsp;&nbsp;$lister[UserName]&nbsp;&nbsp;$lister[UserFirstName]&nbsp;&nbsp;$lister[UserLastName]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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.

speedman_2001 is offline   Reply With Quote
Old 03-09-2012, 07:41 AM   #4
NickHa
CITP
 
Join Date: Jan 2012
Location: South-East England
Posts: 203
Thanks: 0
Thanked 33 Times in 33 Posts
NickHa is on a distinguished road
Re: Fill text field from listbox selection

Quote:
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 is offline   Reply With Quote
Old 03-10-2012, 01:46 AM   #5
NickHa
CITP
 
Join Date: Jan 2012
Location: South-East England
Posts: 203
Thanks: 0
Thanked 33 Times in 33 Posts
NickHa is on a distinguished road
Re: Fill text field from listbox selection

I have just realised that the code I gave you doesn't work! 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
NickHa is offline   Reply With Quote
The Following User Says Thank You to NickHa For This Useful Post:
speedman_2001 (03-21-2012)
Old 03-21-2012, 05:52 AM   #6
speedman_2001
Newly Registered User
 
Join Date: May 2008
Location: Nebraska
Posts: 30
Thanks: 4
Thanked 0 Times in 0 Posts
speedman_2001 is on a distinguished road
Re: Fill text field from listbox selection

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.
speedman_2001 is offline   Reply With Quote
Old 04-02-2012, 11:19 PM   #7
NickHa
CITP
 
Join Date: Jan 2012
Location: South-East England
Posts: 203
Thanks: 0
Thanked 33 Times in 33 Posts
NickHa is on a distinguished road
Re: Fill text field from listbox selection

Quote:
Use the ListBox Content residence to recover the individal product primary data
Would you explain this please? I don't understand what you mean.

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!)


NickHa is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto-fill a field based on the selection in another field BardiyaS Tables 7 11-23-2011 03:20 AM
Auto-fill a field with text ledmark Forms 2 02-23-2009 03:23 PM
Auto Fill Text Box From Combo Selection robinsonbst Forms 6 08-22-2005 09:51 AM
Moving selection from Listbox to another field williakr Forms 2 12-16-2004 01:23 AM
Multiple selection listbox into table field(s)? jjc_nj General 3 08-08-2004 07:28 AM




All times are GMT -8. The time now is 08:31 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Featured Forum post


Sponsored Links


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World