Listcount

G

Guest

I was using the .listcount property of a listbox but realized it behaved
pretty wierd.

1. When I first open a form and the listbox is empty, it showed 1
2. When I select something from a combo box which will display in the
listbox, it showed the number of items + 1
3. So thinking I was smart, I went to the control with the listbox and added
a -1 to it in the control source as in: =lstListbox.listcount - 1
4. Guess what, when I selected something from the combo box which HAD NO
items to display, the control shows -1 now

Why is it behaving this way and how can I get it to work right? Thanks.
ck
 
M

Michel Walsh

Hi,


If the RowSource is empty (Me.List0.RowSource = ""), the ListCount
returns 1; it returns 0 in case like:


SELECT * FROM tableName WHERE false;

and if there is no column header.

If the RowSource statement is defined, the ListCount returns the number
of records, plus one if you set the column header visible.

The only point that you describe and that I cannot reproduce (Access
2003) is about using =ListBoxName.ListCount - 1; It returns 0 when there
is nothing in it, here. Did you make a Me.Recalc to be sure the computation
was "recent".




Hoping it may help,
Vanderghast, Access MVP
 
G

Guest

Thanks, Michel for tips. You were right: the column headers were throwing me
off. But what I couldn't figure out is that why is it that with listcounts,
it always display as 1 when a form is first opened even though there are no
records in the listbox?
ck
 
M

Michel Walsh

Hi,


I don't claim it is logical, but that seems to just be so. I would try
to supply a SELECT statement in design mode, so the code would not have to
deal with that particular aberration.


Hoping it may help,
Vanderghast, Access MVP
 

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