Results 1 to 9 of 9
  1. #1
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94

    How do I get a count of items in a ListBox ?

    I've a listbox on a form.



    Using VB code, I want to count the number of item/entries in the listbox.

    Everything I've research indicates that listbox.ListCount will always give a result of 1 even if there are no entries, that's what I'm getting.

    And. .ListCount is defined as selected entries. I want the count of entries that are displayed, not selected.

    Any ideas?

    Thank you for your help!

  2. #2
    ajetrumpet is offline VIP
    Windows Vista Access 2007
    Join Date
    Mar 2010
    Location
    N/A
    Posts
    2,694
    Quote Originally Posted by KathyL View Post
    Everything I've research indicates that listbox.ListCount will always give a result of 1 even if there are no entries
    that is not correct. LISTCOUNT gives the number of entries you have. Listcount does NOT have an index associated with it. the property looks at the FIRST field in the listbox, if there is more than one.

  3. #3
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    This is working to give me the number of items in the list (A2000):

    MsgBox Me.lstReservations.ListCount - IIf(Me.lstReservations.ColumnHeads, 1, 0)
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  4. #4
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    If you know the record source of the listbox, you could cretae a query to count the records invovled? Haven't tried it, but just a thought.

    Well I did try it and got the following:
    The listbox I have is lboAllEnt on form frmSysEnt.

    RecordSource: SELECT tblSampleEnt.EntitlementID, tblSampleEnt.EntitlementName AS Expr1 FROM tblSampleEnt WHERE (((tblSampleEnt.Flag)=False)) ORDER BY tblSampleEnt.EntitlementID;

    I ran the query and got 205 as the record count ( I just used the query wizard).

    Then I tried as small test:

    Sub testList()
    'code to count the number of entries in a listbox
    'listbox is lboAllEnt
    ' Form is frmSysEnt
    DoCmd.OpenForm "frmSysEnt"
    Debug.Print Forms.Item("frmSysEnt").Controls.Item("lboallEnt") .ListCount
    End Sub
    The debug also gives me 205.

    I'm using Acc2003 on XP SP3.

  5. #5
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94

    I'm using DCount

    Quote Originally Posted by orange View Post
    If you know the record source of the listbox, you could cretae a query to count the records invovled?
    I'm using Acc2003 on XP SP3.
    I'm using Acc2007 on both XP SP3 and W7 SP2.

    .ListCount most definately doesn't give a record count, but only records selected by user.

    My list was always based on a query.
    I've solved it by using DCOUNT against a query, it's giving me accurate counts.

    Thanks all!

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I'd be interested in seeing exactly how you used ListCount, or a sample db where it fails. I just tested with A2007 and the code I posted above worked correctly. The rowsource was a query. Not saying you're wrong, but trying to identify the circumstances under which it fails, since it generally works.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows XP Access 2003
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,716
    Agree totally with Paul - would like to see what conditions are causing the .ListCount to give you the result you are getting.

    The test I posted in #4 was with ACC2003 on XP sp3.

    I just tested Paul's code and it works for me also --205 records.

  8. #8
    KathyL is offline Advanced Beginner
    Windows 7 Access 2007
    Join Date
    Aug 2010
    Posts
    94
    Quote Originally Posted by orange View Post
    Agree totally with Paul - would like to see what conditions are causing the .ListCount to give you the result you are getting.

    The test I posted in #4 was with ACC2003 on XP sp3.

    I just tested Paul's code and it works for me also --205 records.
    Simple. VB code on a OnCurrent event:
    Me.txtcnt = Me.listCustChanges.ListCount

    ListCount result would always show 1 even when the listbox had no records.

    Listbox rowsource was "SELECT [NEW PO Spec customer name changes].[Date of Change], [NEW PO Spec customer name changes].[Previous Customer], [NEW PO Spec customer name changes].[Customer - Spec Change] FROM [NEW PO Spec customer name changes] ORDER BY [NEW PO Spec customer name changes].[Date of Change] DESC;"

    The "NEW PO Spec customer name changes" is a query.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,518
    I think it would do that if the column heads property was on, as it counts the column heads as a row. That's why I had the ColumnHeads bit in my code. What did it show if there were records in the listbox? If it was always 1 high, it was likely the column heads.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 9
    Last Post: 01-20-2011, 02:22 PM
  2. Count Items by Time Period
    By pawslover in forum Queries
    Replies: 1
    Last Post: 11-15-2010, 03:57 PM
  3. Selected items in listbox
    By tomodachi in forum Access
    Replies: 1
    Last Post: 09-09-2010, 01:14 PM
  4. Query Not Listing All Items
    By Rawb in forum Queries
    Replies: 7
    Last Post: 05-14-2010, 08:00 AM
  5. list box items
    By thewabit in forum Forms
    Replies: 12
    Last Post: 01-01-2010, 08:59 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums