Listbox ListCount Zero or One??

R

Rick A

I found this on Deja but there is no resolution and I'm having the same
issue. Can anyone help?

**************
I have a number of queries which return results in a listbox, I'm trying to
code a 'nothing returned' message. When I get no matches, sometimes my
listcount is zero and sometimes it is 1. Sometimes I get the listbox
headings and sometimes I dont'.
Can anyone explain this? Should I be testing for listcount zero or <2 ?

One response...
Any consistency between when you get 0 and when you get 1? Do you have
headers turned on every time you get 1, and never when you get 0?
**************

In my case the listbox header is always on.
 
D

Dirk Goldgar

Rick A said:
I found this on Deja but there is no resolution and I'm having the
same issue. Can anyone help?

**************
I have a number of queries which return results in a listbox, I'm
trying to code a 'nothing returned' message. When I get no matches,
sometimes my listcount is zero and sometimes it is 1. Sometimes I get
the listbox headings and sometimes I dont'.
Can anyone explain this? Should I be testing for listcount zero or <2
?

One response...
Any consistency between when you get 0 and when you get 1? Do you have
headers turned on every time you get 1, and never when you get 0?
**************

In my case the listbox header is always on.

If you have column headers turned on (the ColumnHeads property is
Yes/True), then the ListCount includes the header row. So it's one more
than the number of rows returned by the rowsource.

One trick you can use to write code that works regardless of whether
there are column headers or not, is to use the fact that ColumnHeads, as
a boolean property, has a numeric value of -1 if it's True, and 0 if
it's False. A general solution for getting the number of rows in the
list box, not counting the header row if it's displayed, is to add the
value of the ColumnHeaders property to the value of the ListCount
property, like this:

Dim lngRowCount As Long

With lstMyListBox
lngRowCount = .ListCount + .ColumnHeads
End With

However, testing (with Access 2002) seems to indicate that there's a
delay between the time the list box's rowsource is queried and the time
the header row is counted. I opened a form with a list box, with
ColumnHeads = True and a rowsource that returned no rows. Then in the
Immediate window I tested the ListCount property twice in rapid
succession and got different results:

?Forms(0).lstHobbies.ListCount
0
?Forms(0).lstHobbies.ListCount
1

If I waited for a while before checking the ListCount, though, I got 1
the first time. Since I was doing this manually, that indicates a
substantial delay.

Therefore, if your goal is to determine whether the rowsource query
returned any rows, you would do well to calculate the adjusted row count
as I showed above, but then test for >0:

If lngRowCount > 0 Then
' The query returned rows.
Else
' It didn't.
End If
 
R

Rick A

Dirk,

Thanks for the tip. I'll give it a try. I'm glad you too experienced the
zero / one (0 / 1) issue. At least I have not lost my mind
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top