Option Button Value (1 Viewer)

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
Hi All,

I have a stand alone option button on a form. When the form is updated my code checks for the value of this button to see whether it is True of False. I'm using a message box that shows me the value of the button. Sometimes, when the value is "True" the message box returns "True" as it should. However, sometimes when the value is "True" the message box returns -1. When this happens my code doesn't work because it's only looking for a True or False value. How can I resolve this?

Thanks,
TS
 

bob fitz

AWF VIP
Local time
Today, 15:22
Joined
May 23, 2011
Messages
4,717
Try checking for -1 or 0 instead of True or False
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:22
Joined
Feb 19, 2002
Messages
42,970
Option Groups hold numeric values. Look at the properties for each of the options to see what was assigned to the Yes and No buttons. Usually the first option will default to 1 and the second to 2.
 

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
Hi Pat - it's not an option group - it's just a stand alone option button. The property is set to Yes/No. If I change the code to check for -1 (as Bob suggested) my message box still returns True at times. Other times it returns -1.
 

bob fitz

AWF VIP
Local time
Today, 15:22
Joined
May 23, 2011
Messages
4,717
Hi Pat - it's not an option group - it's just a stand alone option button. The property is set to Yes/No. If I change the code to check for -1 (as Bob suggested) my message box still returns True at times. Other times it returns -1.
Can you show us your code
 

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
Code:
'*** Email routine ***
If chk_EmailSent.Value = False Then
    If opt_Dir.Value = True Then
        Globals.glb_Client = txt_Clients.Value
        Email_Code = "B"
        Location = cmb_Locations.Value
        PrepareGroupEmails Location, Email_Code
        CDOMail Email_Code
        Globals.glb_DirectorReviewFlag = False
        MsgBox "Emails will be sent to the directors in your group.", vbExclamation, "Success"
        sql2 = "Update tbl_Incidents set Email_Sent = True where UID = " & text_TransNo.Value
        DoCmd.SetWarnings False
        DoCmd.RunSQL (sql2)
        DoCmd.SetWarnings False
    End If
End If
 
Last edited by a moderator:

CJ_London

Super Moderator
Staff member
Local time
Today, 15:22
Joined
Feb 19, 2013
Messages
16,553
please use the code tags (highlight your code and click on the </> button) to preserve indentation which makes the code much easier to read. I've done it for you on this occasion

.Value is not required as it is the default property.

try putting

debug.print opt_Dir

before your if line so you can see what the value actually is

you are aware that unless you assign a default value or the control is bound to a field, the initial value will be null which cannot be used in an if clause
 

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
Thanks CJ. I have a message box telling me what the value actually is. While I set a default setting (False) for the field in the table I did not set the default in the form itself. I just corrected that. Perhaps that will help.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 15:22
Joined
Feb 19, 2013
Messages
16,553
if you have prexisting records where you did not set the default (so the value is null), then that could be the reason.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:22
Joined
Feb 19, 2002
Messages
42,970
In addition, when referencing controls on "this" form or report, use the "me." reference.
Code:
If Me.chk_EmailSent = False Then
    If Me.opt_Dir = True Then

Always set Yes/No fields to be required and make the default 0 or -1 depending on what makes sense. If you want triple state (Yes, No, Null), then don't define the field as Yes/No, define it as an Integer.
 

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
Hi Pat - I followed your suggestions. I can't find anywhere to make the "Yes/No field required". I looked on the form and in the table. How can I make it "required"? Also, would it make a difference if the default was "False" instead of "0"? Thanks so much for your help.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 11:22
Joined
Feb 19, 2002
Messages
42,970
The table is where you would make it required. Click on the Y/N field and look at its properties.

The ONLY values allowed for a Y/N field are 0, -1 and Null. They are displayed as True/False or Yes/No but in Code, you must use the numeric value or True/False. You cannot use Yes/No.
 

Isaac

Lifelong Learner
Local time
Today, 08:22
Joined
Mar 14, 2017
Messages
8,738
if you have tristate watch for null too.
but why in the world have a single radio button? those are usually used for 2 or more mutually exclusive choices..
 

Cronk

Registered User.
Local time
Tomorrow, 02:22
Joined
Jul 4, 2013
Messages
2,770
And shouldn't the second Docmd.setwarnings be
Code:
DoCmd.SetWarnings true
 

TheSearcher

Registered User.
Local time
Today, 11:22
Joined
Jul 21, 2011
Messages
304
The table is where you would make it required. Click on the Y/N field and look at its properties.

The ONLY values allowed for a Y/N field are 0, -1 and Null. They are displayed as True/False or Yes/No but in Code, you must use the numeric value or True/False. You cannot use Yes/No.
Hi Pat - There's no "Required" property for Yes/No fields in my table. See attached screenshot. I'm using Access 2016.
 

Attachments

  • Doc1.pdf
    27.6 KB · Views: 106

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:22
Joined
Feb 28, 2001
Messages
26,999
why in the world have a single radio button?

@Isaac - Not a radio button group, from the OP's discussion. An OPTION button which can give you T/F responses. Think of it as a different type of checkbox. I've used it many times. The REASON I use it that way is that it has a caption I can change when I toggle it, so that I can see which choice is currently selected by having a word or short phrase in the button's caption. Yes, I could just as easily use a check box, but the option button allows me to "spiffy up" the appearance.

@TheSearcher - the problem is that in Access the Yes/No and Boolean fields are implemented as a typecast of BYTE (short short integer). In this typecast, 0 represents FALSE and -1 represents TRUE. When you see -1 and didn't want that, You are seeing the integer side of the variable. Use a CBool() function to force it to be treated as either TRUE or FALSE. However, also remember that what you are seeing is a matter of formatting, because even for a typecast, it is possible to use the actual value of the variable rather than its typecast value.
 

Isaac

Lifelong Learner
Local time
Today, 08:22
Joined
Mar 14, 2017
Messages
8,738
@Isaac - Not a radio button group, from the OP's discussion. An OPTION button which can give you T/F responses. Think of it as a different type of checkbox. I've used it many times. The REASON I use it that way is that it has a caption I can change when I toggle it, so that I can see which choice is currently selected by having a word or short phrase in the button's caption. Yes, I could just as easily use a check box, but the option button allows me to "spiffy up" the appearance.

@TheSearcher - the problem is that in Access the Yes/No and Boolean fields are implemented as a typecast of BYTE (short short integer). In this typecast, 0 represents FALSE and -1 represents TRUE. When you see -1 and didn't want that, You are seeing the integer side of the variable. Use a CBool() function to force it to be treated as either TRUE or FALSE. However, also remember that what you are seeing is a matter of formatting, because even for a typecast, it is possible to use the actual value of the variable rather than its typecast value.
What you are talking about is called a toggle button...isn't it?
Technically there is no such thing as an Option Button, so I had to guess

I'm always in favor of teaching people to use the right terms. I mean---you went to so much trouble to teach them things about Byte, Typecast, Integer, Cbool........if you're going to do all that, the first thing you might as well do is have them correctly refer to the thing that is the whole subject of the post. Not using the correct terms is not learning IMO
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 10:22
Joined
Feb 28, 2001
Messages
26,999
OK, toggle button if you prefer. But I was using the words of the OP. Guess I should have clarified it a little.
 

Users who are viewing this thread

Top Bottom