checkbox value -1 vba (1 Viewer)

aman

Registered User.
Local time
Today, 13:06
Joined
Oct 16, 2008
Messages
1,250
Hi guys

I have couple of checkboxes on my form. When the button is clicked I want to display the values in them. They are showing -1 or null but just wondering it should show 1,0 or true or false.

Any help will be much appreciated.

Thanks
 

plog

Banishment Pending
Local time
Today, 15:06
Joined
May 11, 2011
Messages
11,646
Checkboxes on a form can be null. You should code such that those checkboxes are not null. You accomplish this by setting a default value to them.

As for why -1 is True and 0 is false, you can search this forum for lengthy, boring discussions with each new poster trying to outdo the others with their knowledge. I advise you to just accept that's the way the world works and know that -1 is true and 0 is false.
 

Cronk

Registered User.
Local time
Tomorrow, 06:06
Joined
Jul 4, 2013
Messages
2,772
"As for why -1 is True and 0 is false..."

Yes, for binary but any value other than -1 is true in logic operations.

However, check boxes can hold values other than 0, null or -1. I found this out some years ago doing some enhancement work on a database I had not created where an unbound check box was being used to store a date and it showed on the form as being checked. Go figure why it was done this way.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:06
Joined
Feb 28, 2001
Messages
27,175
Cronk, I think any other value than 0 is TRUE. You said it backwards.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 06:06
Joined
Jan 20, 2009
Messages
12,852
As for why -1 is True and 0 is false, you can search this forum for lengthy, boring discussions with each new poster trying to outdo the others with their knowledge. I advise you to just accept that's the way the world works and know that -1 is true and 0 is false.


This is one of the threads plog refers to.

Personally I found it quite interesting but then I am a curious person and like to understand what is behind things. Moreover I consider it sharing knowledge rather than a competition.

Anyone who bothered to take the time to understand would see that my explanation in post 9 of that thread actually does comprehensively explain why negative one is used for True.
 

Orthodox Dave

Home Developer
Local time
Today, 21:06
Joined
Apr 13, 2017
Messages
218
If you really must show something on your form other than a tick in a box, you could

1. In your main table, change the yes/no field type to Byte.

2. On your form put a combo or list box with a VALUE LIST of 2 columns, with a row source such as:
0;"Not as Such";1;"Yes Definitely" [Don't use -1 as the Byte type won't accept it]

3. The bound column is column 1 and its column width is 0", so the 2nd column is displayed.
 

Cronk

Registered User.
Local time
Tomorrow, 06:06
Joined
Jul 4, 2013
Messages
2,772
Doc

Your comment "any other value than 0 is TRUE". Seems any value other than -1 equates to false.

? (1 = true)
False

? (0 = true)
False

? (-1 = true)
True

? (-1.1 = true)
False

Glaxiom, you refer to your post that "True is determined as Not 0"
In fact (Not anything) except (Not -1) is true
 
Last edited:

static

Registered User.
Local time
Today, 21:06
Joined
Nov 2, 2015
Messages
823
Debug.Print CBool(1)
Debug.Print CBool(0)
Debug.Print CBool(-1)
Debug.Print CBool(-1.1)
Debug.Print CBool("true")
Debug.Print CBool("false")
Debug.Print CBool("yes") ' err 13
Debug.Print CBool("no") ' err 13

¬_¬
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 15:06
Joined
Feb 28, 2001
Messages
27,175
Cronk: Remember that constant name "TRUE" is -1 and "FALSE" is 0, and ALSO remember that for expressions to be evaluated, automatic promotion occurs to force compatibility for the subsequent comparison.

? (1 = true)
False
because 1 is not equal to -1.

? (0 = true)
False
because 0 is not equal to -1.

? (-1 = true)
True
because -1 IS equal to -1.

? (-1.1 = true)
False
because -1.1, being at least a SINGLE, requires conversion of -1 to -1.0, which is NOT equal to -1.1.

Static was doing this the other way - taking the constants and converting them to their Boolean values to see how they react. In other words, you were doing things in a way to allow typecasting to higher types; he was testing in a way that forced typecasting to lower types.
 

plog

Banishment Pending
Local time
Today, 15:06
Joined
May 11, 2011
Messages
11,646
Just posting back to let everyone know I'm not one of those people who is above saying 'I told you so':

I told you so.
 

isladogs

MVP / VIP
Local time
Today, 21:06
Joined
Jan 14, 2017
Messages
18,217
Just posting back to let everyone know I'm not one of those people who is above saying 'I told you so':

I told you so.

Using frothingsloth's approach I'd like to say
"Game, set & match to Plog"

Somehow I expect this will go on and on ....
 

Users who are viewing this thread

Top Bottom