Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems with Listboxes if item is Selected from the list and the Text

Status
Not open for further replies.

tudor30

Technical User
Jan 7, 2004
49
US
I'm using three sets of listboxes, textboxes, and a commandbutton for each textbox.

The PROBLEM:
I can enter a value in the textbox, click the commandbutton and the listbox populates. BUT if I select an item from the listbox and then try to change the value of the textbox that decides what is in the same listbox the code ERRORS.

Could not get the list property. Invalid array index. It then highlights one of the three lines below for example:
UserForm1.Frame2.ListBox2.List(UserForm1.Frame2.ListBox2.ListIndex)

Private Sub ListBox2_Change()
TextBox.Text = ""
TextBox.Text = UserForm1.Frame2.ListBox2.List(UserForm1.Frame2.ListBox2.ListIndex)
End Sub

Private Sub ListBox3_Change()
TextBox.Text = ""
TextBox.Text = UserForm1.Frame1.ListBox3.List(UserForm1.Frame1.ListBox3.ListIndex)
End Sub

Private Sub ListBox4_Change()
TextBox.Text = ""
TextBox.Text = UserForm1.Frame3.ListBox4.List(UserForm1.Frame3.ListBox4.ListIndex)
End Sub

Any thoughts?

Thanks,
John
 
Hi,

When it errors, turn on the debugger and select UserForm1.Frame2.ListBox2.ListIndex - right click and add a watch.

Do the same for UserForm1.Frame2.ListBox2.List(UserForm1.Frame2.ListBox2.ListIndex)

What are the values in the Watch Window?

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Skip,

I haven't used the watch window before (hope you're not too surprised by that.

Iclicked on watch window but I am unsure how to enter what you said to watch.

John
 
okay I figured out how to add watch
 
Once you code encounters an error, hit the [Debug] button and then right click the objects I afore mentioned SELECTING Add Watch

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
I have the items added to the watches.

What do I need to communicate to you from the Watches?

by the way...thanks for helping.
John
 
Watch : : UserForm1.Frame2.ListBox2.ListIndex : -1 : Variant/Long : UserForm1.ListBox2_Change

Watch : + : UserForm1.Frame2.ListBox2.List : <No Variables> : Variant/Variant(0 to -1, 0 to 9) : UserForm1.ListBox2_Change
 
Code used for Commandbutton, ListBox, and TextBox

Private Sub CommandButton2_Click()
optioncode_Search UserForm1.Frame2.TextBox12.Text
End Sub

Private Sub optioncode_Search(SearchFor As String)
Dim rngFind As Range
Dim strFirstAddress As String

ListBox2.Clear
ListBox3.Clear
ListBox4.Clear
With Workbooks("S350 T3 Test with Form.xls").Worksheets("Master Tracking").Range("A:A")
Set rngFind = .Find(SearchFor, LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns)
If Not rngFind Is Nothing Then
strFirstAddress = rngFind.Address
Do
UserForm1.Frame2.ListBox2.AddItem rngFind.Offset(0, "1")
UserForm1.Frame2.ListBox2.List(UserForm1.Frame2.ListBox2.ListCount - 1, 1) = rngFind
Set rngFind = .FindNext(rngFind)
Loop While Not rngFind Is Nothing And rngFind.Address <> strFirstAddress
End If
End With
With UserForm1.Frame2.ListBox2
If .ListCount = 0 Then
.AddItem
.List(0, 1) = "No Match Found"
.Enabled = False
Else
.Enabled = True
End If
End With

End Sub
 
I was mostly interested in the VALUES in the watch window.

So, what happens when you have a ListIndex of -1? That is your problem -- NOTHING WAS SELECTED

Skip,

Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top