make textbox text = listbox selection

G

Guest

Hello
I want to have a selection in a listbox go to a textbox.
So far I have...
Private Sub lstEmployee_Click()
txtAddEmployee.Text = lstEmployees.ListIndex
End Sub

but I get the error "Run-time error '2185'
You can't reference a property or method for a control unless the control
has focus"

thanks for any help
Steve
 
M

Marshall Barton

Delete said:
I want to have a selection in a listbox go to a textbox.
So far I have...
Private Sub lstEmployee_Click()
txtAddEmployee.Text = lstEmployees.ListIndex
End Sub

but I get the error "Run-time error '2185'
You can't reference a property or method for a control unless the control
has focus"


Don't use the Text property, it has a completely different
use in Access. Use the Value property instead, but since
it's the default property, you sone even have to specify it:
txtAddEmployee= lstEmployees.ListIndex

Are you sure you want the number of the selected row from
the list box.
 
G

Guest

Try and Drop the text
Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees.ListIndex
End Sub
 
G

Guest

One more thing, the ListIndex will display the location of the cursor and not
the value, if you want the value then Drop also the ListIndex

Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees
End Sub
 
G

Guest

Hello Ofer
thanks for the reply

Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees
End Sub

gives the cursor location not the text of the lstBox.

Steve
 
G

Guest

thanks Marshall Barton
Yes, you are right I don't want the cursor location.

Private Sub lstEmployee_Click()
Me.txtAddEmployee.Value = Me.lstEmployees
End Sub

gives the cursor location.

so does...
Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees
End Sub
 
G

Guest

It shouldn't give you the cursor location, but it might return the Id of the
list box, If the location of the data you want to be displayed in the text
box, is the second column in the list box, then you should try this

Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees.column(1)
End Sub
Th column number start with 0
 
G

Guest

Yes, that did it. Excellent!
thank you!

Ofer said:
It shouldn't give you the cursor location, but it might return the Id of the
list box, If the location of the data you want to be displayed in the text
box, is the second column in the list box, then you should try this

Private Sub lstEmployee_Click()
Me.txtAddEmployee = Me.lstEmployees.column(1)
End Sub
Th column number start with 0
 
M

Marshall Barton

The reason the Value is the same as ListIndex is because you
set the BoundColumn property to 0. It should be the number
of the column (starting at 1) that contains the value you
want.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top