ListBox.ListCount property problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • julietbrown
    New Member
    • Jan 2010
    • 99

    ListBox.ListCount property problem

    Puzzled!
    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.ListCoun t = 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!!)
    Last edited by julietbrown; May 4 '10, 03:24 PM. Reason: ambiguous punctuation!
  • Megalog
    Recognized Expert Contributor
    • Sep 2007
    • 378

    #2
    Originally posted by julietbrown
    Puzzled!
    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.ListCoun t = 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!!)
    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:

    Code:
    If Me.MyList.Rowsource = "" Then
    'Do something
    End If

    Comment

    • julietbrown
      New Member
      • Jan 2010
      • 99

      #3
      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.

      Comment

      • Megalog
        Recognized Expert Contributor
        • Sep 2007
        • 378

        #4
        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.

        Comment

        • julietbrown
          New Member
          • Jan 2010
          • 99

          #5
          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!)

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32634

            #6
            Originally posted by julietbrown
            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?.

            Comment

            • julietbrown
              New Member
              • Jan 2010
              • 99

              #7
              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!

              Comment

              Working...