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

listbox data missing?

Status
Not open for further replies.

random260

Programmer
Mar 11, 2002
116
US
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 should also mention that I have all three list boxes populated at all times, no matter which data I am trying to get into the text box.
 
I am not sure what app you are using.
In MS Access we use "TextBox.Value" or "TextBox" So you don't need to SetFocus on the control.


________________________________________________________
Zameer Abdulla
Help to find Missing people
There’s a world of difference between editorials and advertorials
 
Huh? You completely lost me - it's a listbox, not a textbox. I am using VBA in Microsoft Word. The listbox is on a form I created.
 
I ddn't I am talking about the bold part shown below
Code:
For x = 0 To 3
ListBox1.Selected(x) = 1
ListBox2.Selected(x) = 1
ListBox3.Selected(x) = 1
[b][COLOR=red]TextBox4.Text = TextBox4.Text[/color][/b] & ListBox1.Value & " "
Next x


________________________________________________________
Zameer Abdulla
Help to find Missing people
There’s a world of difference between editorials and advertorials
 
I'm still not sure what you're saying that has to do with the problem - I'm not setting focus on the textbox - I'm simply accessing the .text value of it to add text to it. The textbox doesn't seem to be the problem here.
 
random260,

A couple of questions, since when I mocked this up and ran your code, the TextBox displayed the contents of ListBox1 as you would have expected:

How are you populating you ListBoxes?

Do you have any event handler code associated with the ListBoxes (in particular, ListBox1)?


Regards,
Mike
 
Good question rmikesmith - I only wish that was the problem lol. I DID have an event handler for the listbox - but the first time I ran the code I noticed that the event handler was getting triggered, so I deleted it. I'm impressed that you thought of that though, I would have missed it entirely if it wasn't for the fact that the event it triggered was pretty damn noticeable lol.

I am populating the list boxes from a file.
 
I'm thinking there's something quirky going on here (that's a technical term for I have no idea what's happening). You could try stepping through the code and inspecting variables/property values. That may help. Also, give the following code a try, which captures the ListBox entries in a different manner (replace your TexBox4 assignment inside the loop):
Code:
TextBox4.Text = TextBox4.Text & ListBox1.List(x) & " "
This assumes ListBox1 is a single-column listbox. Adjust accordingly.

Mike
 
lol, I like your definition of quirky. OK, as soon as I get back to my computer I'll try that and see what it does.

Thanks
Steve
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top