Solved Combo Box List count (1 Viewer)

ypma

Registered User.
Local time
Today, 17:10
Joined
Apr 13, 2012
Messages
643
Expert advise is sort ,
I am using the listcount to obtain the number of rows in a combo box .The text box calculated control I am currently using gives the correct count, unless the number of rows is zero , in that case the output is -1 ,which is the same result as when I do not use minus columnheads .

Code:
=[Combo841].[ListCount]-Abs([Combo841].[ColumnHeads])

Any pointers would be appreciated

Regards Ypma
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:10
Joined
Oct 29, 2018
Messages
21,358
What do you get with tihs?
Code:
=[Combo841].[ListCount]+[Combo841].[ColumnHeads]
 

ypma

Registered User.
Local time
Today, 17:10
Joined
Apr 13, 2012
Messages
643
=[Combo841].[ListCount]+[Combo841].[ColumnHeads]
theDBguy thank you for replying to my question. unbelievable the result is the same -1, strange, as it must be counting the column head .
Does the list count begin a zero or 1 ?
Any other suggestions ?

Ypma
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:10
Joined
May 21, 2018
Messages
8,463
The "-1" is the value of a listbox with no items selected. Why it returns that, is likely a limitation of the calculated control. I would build a UDF or set this value in code if this is a single form view.
 

Isaac

Lifelong Learner
Local time
Today, 10:10
Joined
Mar 14, 2017
Messages
8,738
The "-1" is the value of a listbox with no items selected.
You mean when you use Listbox.Listcount, (assuming no heads), you get -1 even if there are, say, 10 things in the listbox?
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 13:10
Joined
May 21, 2018
Messages
8,463
sorry, I believe -1 is returned as the value of the listindex when nothing is selected. I am saying the doing this with an expression probably will not work when nothing is selected, thus the weird results for both adding and subtracting. Untried but I am sure it will work in vba or UDF. Expressions are pretty limited.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:10
Joined
Oct 29, 2018
Messages
21,358
theDBguy thank you for replying to my question. unbelievable the result is the same -1, strange, as it must be counting the column head .
Does the list count begin a zero or 1 ?
Any other suggestions ?

Ypma
Hi. Try it this way then.
Code:
=IIf([Combo841].[ListCount]>0,[Combo841].[ListCount]+[Combo841].[ColumnHeads],0)
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 12:10
Joined
Feb 28, 2001
Messages
26,999
ListCount and ListIndex are two different things. ListIndex is -1 when there is no selection because (like all collections) the rows are zero-based. The ListCount should be the number of rows equal to n+1 if you have N as the highest row. Yes, combobox rows start counting from 0 because they are collections.

Code:
=[Combo841].[ListCount]-Abs([Combo841].[ColumnHeads])
=[Combo841].[ListCount]+[Combo841].[ColumnHeads]

Either expression would display -1 if (a) the list count was zero and (b) you had selected that you wanted to see column headers. This is because .ColumnHeads is T/F, not a number. If it happens to be TRUE, when you bring it into an arithmetic expression, the TRUE becomes -1. (FALSE would have been 0). So I would fully believe the result of -1 with an empty list and column headers enabled.
 
Last edited:

ypma

Registered User.
Local time
Today, 17:10
Joined
Apr 13, 2012
Messages
643
Hi. Try it this way then.
Code:
=IIf([Combo841].[ListCount]>0,[Combo841].[ListCount]+[Combo841].[ColumnHeads],0)
theDBguy, Worked like a charm, the -1 was annoying me , but now all is well in my little world .
Thank you

Regards Ypms
 

ypma

Registered User.
Local time
Today, 17:10
Joined
Apr 13, 2012
Messages
643
MajP and Isaac, thank you for your input. Isaac , sorry if I did not fully explain the problem as Isaac pointed out the result of -1 only shows
when there were no rows count .
theDBguy solution has solved my problem

Regards Ypma
 

theDBguy

I’m here to help
Staff member
Local time
Today, 10:10
Joined
Oct 29, 2018
Messages
21,358
theDBguy, Worked like a charm, the -1 was annoying me , but now all is well in my little world .
Thank you

Regards Ypms
Hi. You're welcome! We were all happy to assist. Good luck with your project.
 

Users who are viewing this thread

Top Bottom