Count items in listbox with specific text (1 Viewer)

theantz

New member
Local time
Today, 04:15
Joined
Dec 13, 2015
Messages
7
Hi,

I am trying to count the number of rows in my listbox where value is "Completed"

For example, I have a listbox with 2 columns.

Job Name | Job Status

The name of listbox is SearchResults

For each row, the value of job status can be Pending or Completed

I want to be able to count the number of jobs that has status of "Completed"

I tried using this formula as a rowsource of my textbox
Code:
=Sum(IIf([SearchResults].[column](1)="Completed",1))

but I get #error as a result.

I double check if I am referencing the right object by counting everything using this formula
Code:
=[SearchResults].[ListCount]-1

and it return the total number of rows in the listbox.

Anyone knows how to count specific row in a listbox based on it's text/value?

Thanks
 

namliam

The Mailman - AWF VIP
Local time
Today, 02:15
Joined
Aug 11, 2003
Messages
11,695
You cant count a listbox, well you can perhaps if you loop it.... However in most cases re-doing the select statement sourcing the listbox but adapting it to suite your (new) needs is easiest.
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 08:15
Joined
May 7, 2009
Messages
19,242
you should make a public function and put it in a module.
on your textbox controlsource:
=fnCountCompletedInList([yourListboxName])

public function fnCountCompletedInList(byval lst As Access.ListBox) As Integer

dim i as integer
dim iCount as integer
for i = 0 to lst.ListCount - 1
if lst.column(1, i) = "Completed" then iCount = iCount + 1
next
fnCountCompletedInList=iCount
End Function
 

theantz

New member
Local time
Today, 04:15
Joined
Dec 13, 2015
Messages
7
you should make a public function and put it in a module.
on your textbox controlsource:
=fnCountCompletedInList([yourListboxName])

public function fnCountCompletedInList(byval lst As Access.ListBox) As Integer

dim i as integer
dim iCount as integer
for i = 0 to lst.ListCount - 1
if lst.column(1, i) = "Completed" then iCount = iCount + 1
next
fnCountCompletedInList=iCount
End Function

WOW!

Works like a charm!
 

Users who are viewing this thread

Top Bottom