Row Count for List Box on Form

Posted on 2004-11-07
Last Modified: 2014-01-23
Is there an easy way to get the number of rows in a list box on a form?  This is Access 97.  I have a listbox that dynamically updates with form filters and want to display the number of rows for each filter.  Is there an easy way to do it?  I don't want to recreate the SQL (list box source) in VBA.

Question by:Michael Vasilevsky
    LVL 34

    Expert Comment

    Here';s some code to do it

    Dim rs As DAO.Recordset
    Set rs = CurrentDb.OpenRecordset(Me.List1.RowSource)

    If Not rs.EOF Then
    End If
    MsgBox rs.RecordCount

    Set rs = Nothing
    LVL 58

    Accepted Solution

    The property is called ".ListCount", available for combos and boxes. You can retrieve any data from them with the .Column() property, which has an optional row parameter.
    Example (immediate pane)

        ? Forms(0)!lstMyList.ListCount
        ? Forms(0)!lstMyList.Column(1, 23)

    Good Luck
    LVL 48

    Expert Comment

    by:Gustav Brock
    Well, you need to take into account if you show column heads for this listbox as these will count for one row:

      lngListCount = Me!lstYourListbox.ListCount - Abs(Me!lstYourListbox.ColumnHeads)


    Expert Comment

    Thanks Gustav was a great help to me
    LVL 48

    Expert Comment

    by:Gustav Brock
    After 9½ years. Thanks!


    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Join & Write a Comment

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
    In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now