Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FontBold not equal to TRUE

Status
Not open for further replies.

N2Life

Programmer
Dec 21, 2002
90
0
0
US
I am using Access 2010. I have a form with two buttons, btnHigh and btnLow. If the user clicks btnHigh, the font is made bold, and the other button is set to not bold.
btnHigh.FontBold = True
btnLow.FontBold = False
Then in my code I use this line:
If btnHigh.FontBold = True Then
...
I was surprised to discover that if this button is Bold, btnHigh.FontBold = 1
And since True = -1, even though the font is bold, the test fails because 1 is not equal to -1.

This works:
If btnHigh.FontBold = Abs(True) Then
But that seems an odd way to have to proceed.
Is this commonly known? Are there other cases in VBA where a test of "truthiness" yields 1 instead of -1?
 
Sadly a 'feature' of the FontBold property, which is NOT defined as a Boolean in Access forms, but as an integer (and there is some jiggery pokery going on under the hood along with that).

[tt]
>This works:
> If btnHigh.FontBold = Abs(True) Then
[/tt]
But the following might be more logical:
[tt]
If CBool(btnHigh.FontBold) = True Then
[/tt]

 
strongm . . .

Yes that should work as [blue]CBool[/blue] converts any non-zero value to true. BTW I fired-up an old machine with 2003 and wa able to repeat the problem. Now thats a scary thing ...

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Thank you, StrongM. That is cool. I like elegance! I agree that it is scary, TheAceMan1. I immediately started rummaging around in several other programs I have written to see if I had depended on this in other cases. I don't often program buttons in this way, but I like it for shop-floor use because it's so simple for the user. Of course I could always set a global variable instead of depending on the boldness of the caption, but this CBool approach does the trick.
 
Many times in the situations like this one I take this approach:
False = 0, True is anything else but 0

So this logic should always work:

[pre]
If btnHigh.FontBold = 0 Then
Font is Bold
Else
Font is NOT Bold
End If
[/pre]

Or in many cases I Enable or dis-Able a command button based on information if I have anything in the recordset or not:
[tt]
rst.Open SomeSQLHere

cmdReport.Enable = rst.RecordCount[/tt]

The same approach: 0 gives me dis-abled button, any other value enables the button.

Have fun.

---- Andy
 
Andrzejek . . .

You've just described what [blue]CBool[/blue] does ... and could I possibly be looking at a [blue]wooops![/blue]

Code:
[blue]If btnHigh.FontBold = 0 Then
    Font is Bold
Else[/blue]

See Ya . . .

Be sure to see FAQ219-2884 Worthy Reading! [thumbsup2]
Also FAQ181-2886 Worthy Reading! [thumbsup2]
 
Yes, ooops :) Sorry. Flip-flop the logic.
My point is - I check for 0 (False) and anything else than 0 is True, and I don't check for specific True = 1 or True = -1. True for me is <> 0

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top