By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,851 Members | 1,183 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,851 IT Pros & Developers. It's quick & easy.

ListBox.ListCount property problem

P: 99
I have a list box, and I want code to behave in a particular way according to whether the listbox is empty (no rows displayed) or not.

I have written ...

If MyList.ListCount = 0 then do something, else do something else

This is refusing to work properly under some circumstances, as follows ...

(I've put in a message box to tell me what the code thinks the list count is)

If this is a new record, the code thinks the ListCount is 1, even though the list is empty on the form (and should be, as it has no row source until the record is saved).

If the user is editing an existing record, and the list box is displayed as empty, the code knows it is empty and tells me the ListCount is 0.

This seems to me to be very bizarre! Can anyone explain?

(By the way, what has happened to the Help system in Access 2007? MS seem to have changed it for me without consultation and it's gone from being annoying but reasonably helpful to totally unhelpful overnight!!)
May 4 '10 #1
Share this Question
Share on Google+
6 Replies

Expert 100+
P: 378
Very strange.. I just confirmed this myself. When there is no row source for the object, it still reports the listcount as a 1.

I suggest using:

Expand|Select|Wrap|Line Numbers
  1. If Me.MyList.Rowsource = "" Then
  2. 'Do something
  3. End If
May 5 '10 #2

P: 99
Sadly, I already tried your suggestion!! So, I know that's what it "thinks". I've tried all sorts of dubious things to encourage it to think differently, and it does as long as it's not a new record, even though the row source is just the same under those circs. Well, I'll let you know if/when I find a solution. It's very annoying as it's quite a critical issue for me.
May 5 '10 #3

Expert 100+
P: 378
Hm.. can you explain why you're changing the rowsource to the listbox in the first place? You are checking to see if the control has a rowsource for that record, not if you have selections made, right? I just want to make sure the problem is being stated correctly here, since I've never had a case where i change the rowsource to a control like this on separate records.
May 5 '10 #4

P: 99
I will try to explain! When a new 'Event' record is created and saved, the system generates a set of standard document titles associated with it (e.g., one is 'Invitation ...') and displays them in a listbox on the form. But, of course, when an Event is just edited, you don't want the system to create these again (to make two copies of all of them). So, my AfterUpdate routine looks to see if the set of docs is empty, and if it is it knows to create them, otherwise it knows they have already been created.

But it's ok ... I'm just going to base the decision on whether these docs are already in the underlying table, instead of checking whether the list box is empty. That will solve the current problem. It's frustrating not to understand why Access thinks an empty list box has an entry in it, though!

I have to leave this problem now, as I've found a far worse one that I've no idea how to deal with (see my new post if you have the time/patience/energy!)
May 6 '10 #5

Expert Mod 15k+
P: 31,308
I have to leave this problem now, as I've found a far worse one that I've no idea how to deal with (see my new post if you have the time/patience/energy!)
NB. The thread referred to is probably How can I trap clicking of 'Cancel'/hitting CTRL + Z?.
May 6 '10 #6

P: 99
I was asked to 'choose a best answer'! But in this case, there really isn't one. It remains mysterious. But the work around was to DCount the relevant records in the underlying table ... so I guess that's my own best answer!
May 15 '10 #7

Post your reply

Sign in to post your reply or Sign up for a free account.