Add textbox value to listbox

brharrii

Registered User.
Local time
Today, 00:43
Joined
May 15, 2012
Messages
272
I would like to type a value into a list box and then press a command button and have that value transfer to a list box for later use. I've been playing with the onclick event of my command button and this is what I have so far:


Code:
Option Compare Database
Private Sub Command4_Click()
List0.AddItem Text2.Text
List0.ListIndex = List0.ListCount - 1

 
End Sub


When I click the button I get an error:

2185 - You can't reference a property or method for a control unless the control has the focus.


Can anyone clue me into what I might be doing wrong?

thanks :)
 
The .Text property requires focus. Change it to .Value
 
You're going to get more errors, as well! Assuming that the line

List0.ListIndex = List0.ListCount - 1

is intended to select the newly added item, this will do it:

Code:
Private Sub Command4_Click()
  If Nz(Me.Text2,"") <> "" Then
   List0.AddItem Me.Text2
   Me.List0.Selected(List0.ListCount - 1) = True
  End If
End Sub
You do understand that this will only add this item to the Listbox until the Form is closed, correct? When the Form is re-opened the item will no longer be in the Listbox.

Linq ;0)>
 
Awesome, thank you!

I updated the code to Read:

Code:
Option Compare Database
Private Sub Command4_Click()
  If Nz(Me.Text2, "") <> "" Then
   List0.AddItem Me.Text2
   Me.List0.Selected(List0.ListCount - 1) = True
   
  End If
End Sub

It is now returning an error:

The RowsourceType property must be set to 'value list' to use this method.

I'm not sure what this refers to, any suggestions?

thanks again!
 
Uh, you have to change the row source type of the listbox to value list.
 

Users who are viewing this thread

Back
Top Bottom