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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

data missing from listbox?

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
This is a copy of the post I put in the VBA forum - not having much luck getting a reply there, so I thought I would try here as VBA is about the same as VB6. This refers to a problem with a program I created in VBA in Microsoft Word - the items referenced here appear on a standard form.

This is really freaking me out. Here is a real simple program:

First, I have a form with 3 listboxes, a textbox, and a command button. Then I populate the listboxes with data. Then I click the command button to execute this code:

Private Sub CommandButton4_Click()

For x = 0 To 3
ListBox1.ListIndex = x
ListBox2.ListIndex = x
ListBox3.ListIndex = x
TextBox4.Text = TextBox4.Text & ListBox1.Value & " "
Next x

End Sub

In theory, this loops 4 times, each time selecting the next item in each listbox in order, then adds the selected item from listbox1 (and a space) into the textbox.

Didn't work - the textbox was empty. I scratched my head and tried a bunch of stuff, no luck. Then, just for kicks I tried putting the selected item from textbox2 in the textbox:

Private Sub CommandButton4_Click()

For x = 0 To 3
ListBox1.ListIndex = x
ListBox2.ListIndex = x
ListBox3.ListIndex = x
TextBox4.Text = TextBox4.Text & ListBox2.Value & " "
Next x

End Sub

It worked. listbox2 contained:
name1
name2
name3
name4

and I got:
name1 name2 name3 name4

in the textbox. It worked for listbox 3 as well. So I figured OK, maybe the listbox1 is corrupted. So I deleted listbox1, and then recreated it. Tested the code:

Private Sub CommandButton4_Click()

For x = 0 To 3
ListBox1.ListIndex = x
ListBox2.ListIndex = x
ListBox3.ListIndex = x
TextBox4.Text = TextBox4.Text & ListBox1.Value & " "
Next x

End Sub

and it worked! Listbox1 contained:
id1
id2
id3
id4

and I got:
id1 id2 id3 id4

in the textbox. So I figured problem solved. But get this - NOW it was working with listbox1, but listbox2 STOPPED working!!!! If I tried it with listbox2, I was back to getting nothing in the textbox. So I deleted listbox2.... and 3 stopped working... so I deleted 3... and 1 stopped working... and no matter WHAT I do I can't get all three to work at once! Just for kicks, I also tried changing things to:

Private Sub CommandButton4_Click()

For x = 0 To 3
ListBox1.Selected(x) = 1
ListBox2.Selected(x) = 1
ListBox3.Selected(x) = 1
TextBox4.Text = TextBox4.Text & ListBox1.Value & " "
Next x

End Sub

and the exact same thing would happen... 2 of them would work, 1 would not.

Does anyone have a clue WHAT THE ^%!$#^%$!#^$%! is going on here?!?!?!? I am freaked out trying to get this running...

Thanks
Steve
 
I don't understand why you are trying to select the list box items. Also why are you referencing ListBox2 and ListBox3 as you don't use them?

I suspect that the problem is that Windows hasn't yet processed the messages to set the SelectedIndex so the controls haven't yet changed.

You should try referencing the items in the ListBox's List collection directly:
Code:
TextBox4.Text = ""
For x = 0 to 3
    TextBox4.Text = TextBox4.Text & ListBox1.List(x, 0) & " "
Next
This will select each item from the List collection of the ListBox in turn and append it to the Text in TextBox4.


Bob Boffin
 
This program is a "pre-program" - when I work on a project I often write small snippits of it just to see if it is going to work the way I want it to - that way i can change things around, try things, etc., and not have to worry about keeping it clean and neat. The actual program will use all three list boxes. I will be loading (for example) information on 20 people from a file into the list boxes. The file will contain an id number, a husbands name, and a wifes name - the file will look like this:

0001
tom
sally
0002
fred
tammy
0003
ralph
diane

etc. - and I want to load it into three list boxes to make it easier to work with - like this

listbox idnum listbox hname listbox wname
0001 tom sally
0002 fred tammy
0003 ralph diane

I am selecting the list box items simply because it was the first way I thought of to access the value stored there - and having had problems with it I immediately focused in on getting to work rather then another way to access the information lol. Accessing the information directly should work. But I still want to know why this isn't working. I have done some additional experimenting since posting this message - written code to transfer the data from all three listboxes to three different textboxes - same thing - when I click the command button, data from two of the three transfers perfectly. Your idea that Windows has not yet processed the message was a good one, but it doesn't scan - after deleting (and recreating) listbox 1 (for example) listbox 2 doesn't work - and it's listbox 2 that doesn't work EVERY time until I try deleting and recreating it - then on of the others stops working. So the code processes listbox1, then 2, then 3 - 1 and 3 work, and if it was a Windows not getting the message problem then it shoudl be either 1 or 3 that doesn't work - or a random choice of the 3 - not listbox 2 every time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top