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

Populated Listbox always returns NULL

Status
Not open for further replies.

Bug16

Technical User
Oct 2, 2002
22
GB
I posted this on the Microsoft Access "Other" messageboard and it didn't generate much of a response to something that is really confusing me! Anybody here able to give me some help? :)

Using Access 2000 I have the following code on a button click event:

sSQLString = "SELECT * FROM MainArea"

DoCmd.Hourglass (Yes)
Form_ViewObsoleteColumns.List1.RowSource = sSQLString
DoCmd.Hourglass (No)

This successfully populates my listbox with the expect 100+ results.

I then used (still within the same button click event):

sTemp = Form_ViewObsoleteColumns.List1.Column(1)
msgbox(sTemp)

90% of the time this returns "NULL". The other 10% of the time it returns the expected value of "Milton".

My gut feel is that I've got a race condition happening that means when I do my

sTemp = Form_ViewObsoleteColumns.List1.Column(1)

code the listbox hasn't finished populating or something like that.

I've tried sTemp = Form_ViewObsoleteColumns.List1.SetFocus and this has made no difference.

Any ideas or pointers would be appreciated (as always!) :)
 
It might be worth putting the second piece of code on a separate button (just to test) click this after the listbox has loaded, at least this will tell you if you're right about what you think is going on.

There are two ways to write error-free programs; only the third one works.
 
Hmmm... now it gets even more interesting. I've cut the code and form down to the bare minimum. I.e. two buttons and a listbox. Button one performs the SQL lookup that populates the listbox correctly. Button two does the:

sTemp = Form_ViewObsoleteColumns.List1.Column(1)

I get the same results of 90% of the time NULL is returned and the other 10% I get the expected answer. Yet visually I can see that everytime the form is populated as expected.
 
I think its because there is no selection made,

If you specify a row as well

eg.

sTemp = Form_ViewObsoleteColumns.List1.Column(1,1)

or select an item in the list before clicking the second button, does that make a difference?

There are two ways to write error-free programs; only the third one works.
 
Again on trying:

sTemp = Form_ViewObsoleteColumns.List1.Column(1,1)

I get the same result. Everybody here who's seen the code have all uttered the immortal phrase "But that should always work!" so my code's probably doomed! :)
 
I've just solved it! But you pointed me in the right direction.

I need to say:

Form_ReplaceObsoleteColumns.List19.Selected(0) = True

and then examine the contents of the column and row.

Thanks for your help!
 
How many columns do you have in your list box?

There are two ways to write error-free programs; only the third one works.
 
Splendid !!
There are two ways to write error-free programs; only the third one works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top