value of list to text box

jerry28ph

jerry
Local time
Yesterday, 16:20
Joined
Nov 16, 2008
Messages
141
I have a listbox that has a value from my query, and i want to transfer it to text box. I tried several ways like, text2 = me.list497.value, text2 = list497.column(1) but it doesn't work. Is there any other ways to do it??

I appreciate any help.
Thanks.
 
I think you can check the listbox.ItemsSelected.Count property to verify whether an item is selected. If nothing is selected, this might explain why your code isn't working.

To select the third row in a listbox do this:

Me.listbox1 = Me.listbox1.ItemData(2)
 
You can also do this:

textBox1 = listbox1.Column(colNo, rowNo)

where "rowNo" and "colNo" are zero-based indexes indicating the row number and column number.
 
I believe the listbox.ItemsSelected.Count only applies to multi-select enabled listboxes, which doesn't sound like the case here.

The real question here, I think, is where/how are you attempting to do this? Is this query returning multiple rows in your listbox? How is the desired row/data being selected? What event are you using to do the assignment?
 
I believe the listbox.ItemsSelected.Count only applies to multi-select enabled listboxes, which doesn't sound like the case here.
Incorrect. The following sample code can be used to verify my statements above.

Me.listbox1.RowSource = "select * from customers"
'select the third row.
Me.listbox1 = listbox1.ItemData(4)
MsgBox Me.listbox1.ItemsSelected.Count
 
I'll take your word for it, but this is from Access (2003) Help:

ItemsSelected property returns a read-only reference to the hidden ItemsSelected collection. This hidden collection can be used to access data in the selected rows of a multiselect list box control.

OF course, the OPs code

text2 = list497.column(1)

should work, assuming the desired data is in the 2nd column for the selected row. That fact that it doesn't probably has to do with where/how he's using it, which we're still waiting to hear.
 
There are good examples of using the listbox control in the Access help - search for ItemsSelected.

The following code loops through the listbox and prints the selected items, which can be used for multi-select and single-select listboxes. You could easily replace the Debug.Print with code to send the value to your textbox.

Code:
Sub BoundData()
    Dim frm As Form, ctl As Control
    Dim varItm As Variant
 
    Set frm = Forms!Contacts
    Set ctl = frm!Names
    For Each varItm In ctl.ItemsSelected
        Debug.Print ctl.ItemData(varItm)
    Next varItm
End Sub
 
I'll take your word for it, but this is from Access (2003) Help:

ItemsSelected property returns a read-only reference to the hidden ItemsSelected collection. This hidden collection can be used to access data in the selected rows of a multiselect list box control.
Apparently irrelevant, as it says nothing about whether the property does, or does not, apply to a non-multiselect listbox.
OF course, the OPs code

text2 = list497.column(1)

should work...
But as he said, this is precisely the sort of code that wasn't working. One likely reason is the one I gave above, and hence my solution.
 
Could it be that they need column(0) instead of column(1), remembering that it is zero-based. So, using (1) might not work because it isn't the right column. Just a suggestion...
 
Could it be that they need column(0) instead of column(1), remembering that it is zero-based. So, using (1) might not work because it isn't the right column. Just a suggestion...
I agree with you here - that's why I stated in my second post:

jal said:
You can also do this:

textBox1 = listbox1.Column(colNo, rowNo)

where "rowNo" and "colNo" are zero-based indexes indicating the row number and column
However, I am guessing that the code

textbox1 = listbox1.column(colno)

doesn't work if no row is selected. Hence my first post:

jal said:
To select the third row in a listbox do this:

Me.listbox1 = Me.listbox1.ItemData(2)

 
hi to all who responded to my postings,

OMG! you all have bright ideas on how to solve my problem. I took ideas of Jal and boblarson.
as jal quoted:

*********************************
textBox1 = listbox1.Column(colNo, rowNo)

where "rowNo" and "colNo" are zero-based indexes indicating the row number and column
*********************************

and it works, same as boblarson ideas that col() and row() should start from the based index 0.
I would like to thank you for all the help that you shared with me.



I agree with you here - that's why I stated in my second post:


However, I am guessing that the code

textbox1 = listbox1.column(colno)

doesn't work if no row is selected. Hence my first post:

[/size]
 

Users who are viewing this thread

Back
Top Bottom