Listbox madness involving Selected(lRow:=1) = True

  • Thread starter Thread starter Jon Furman
  • Start date Start date
J

Jon Furman

Hello everyone...I've run into a problem that I'm suspecting is an Access
bug and am wondering if anyone else has run into it. Here's the deal I have
form with two listbox controls, what I want to do is have the second listbox
update it's contents based on the selection of the first listbox. So far so
good...now I also want the second listbox to always have at least one item
selected. I'm using VBA code to make all of this happen, so on the
afterupdate event of the first listbox I take the selected value and
programmatically reset the data source for the second listbox..that works
fine. Now I use the following code to make the first item selected in the
second listbox:

Let Me.lstBox2.Selected(lRow:=1) = True

That line of code completely destroys the workings of the form. The second
box becomes unresponsive and won't update and some areas of the form start
responding to click events that have no procedures assigned (I get an
hourglass). I've tried several permutations but none of them fix the root
problem although some of the things that I've done have changed the symptom.
Very very strange. All that's required to reproduce the issue is a form with
2 listboxes and the one line of code above. Anyone else seen this? If anyone
want's to actually see the problem in action I can post a sample MDB on a
webserver and you all can see the details. All ideas are appreciated. Thanks
all.

Jon
 
Well, you generally work it the other way around. Have the RowSource query
of the second listbox reference the value of the first. Something like
this:

SELECT zip FROM Table7 WHERE city = [forms]![frmSimpleListBox]![List1];

where "frmSimpleListBox" is the name of the form and "List1" is the name of
the first listbox. Then you requery the second listbox in the AfterUpdate
event of the first listbox.

Me!List2.Requery

To make the first item in the second listbox selected use:

Me.List2.Selected(0) = True

Note: numbering starts from zero.

--
--Roger Carlson
Access Database Samples: www.rogersaccesslibrary.com
Want answers to your Access questions in your Email?
Free subscription:
http://peach.ease.lsoft.com/scripts/wa.exe?SUBED1=ACCESS-L
 
Thanks for the reply.Unfortunately I'm still dead in the water, the reason I
have Me.List2.Selected(1) = True is because my list box uhas column headers
on so they take up the zero slot. This is weirdest problem. Just for kicks
changed the listbox data reference to how you had it below but it didn't
matter. Strange strange strange.

BTW...nice website you got going on there.



Jon
 
Sorry Roger I was wrong about the list starting with 0 versus 1 it just
looked that way to me...because...it appears that Access doesn't completely
resolve the listbox's properties immediately after a requery. If you only
have one list box on the form then everyhting works as expected but with
mutliples it looks like something is going on behind the scenes that foul up
any immediate refernces to the listbox properties. I figured this out by
playing around with DoEvents statements and examining the Selected data and
listindex and listcount propoerties at different points after a requery. I
beleive that it takes some time for these properties to reach a final state
and if yo manipulate them too soon all hell breaks loose. What I found to be
the best solution to my problem was to instead of manipulating the selected
property to instead manipulate the default value property. This seems to
have made my listbox code bulletproof. Hope this helps someone out there.

Jon
 
As you have discovered the ListBox control is not fully instantiated
during the Form's Open and Load events. I ran into this problem during
the development of my Tooltips class a couple of years ago. I'm sure if
you had searched GoogleGroups you would have found a couple of relevant
threads.

--

HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Too true I'm sure....I did a bunch of searching before posting but was
unable to turn up anything directly related to the symptoms of what turned
out to be the issue. So I had to retreat to the programmers backup strategy:
trial and error debugging!

Running into undocumented behviours like this is very frustrating but at
least troubleshooting it keeps the brain sharp.

Jon
 
Jon Furman said:
Too true I'm sure....I did a bunch of searching before posting but was
unable to turn up anything directly related to the symptoms of what turned
out to be the issue. So I had to retreat to the programmers backup strategy:
trial and error debugging!

Running into undocumented behviours like this is very frustrating but at
least troubleshooting it keeps the brain sharp.

Jon

I am a firm believer that the challenge of solving these difficult
issues keeps the brain tuned up.
:-)

--
HTH
Stephen Lebans
http://www.lebans.com
Access Code, Tips and Tricks
Please respond only to the newsgroups so everyone can benefit.
 
Back
Top