Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2010
    Posts
    3

    Question Unanswered: Conditionally displaying or disabling textboxes based on checkboxes?

    Hi everyone, this is my first post to dBforums

    I've been mulling over a problem for quite a few days.

    If anybody could help, that would be greatly appreciated.

    Is there a way to have a question be enabled or disabled based on the response in a checkbox in an option group?

    I'm also having issues with the checkboxes in my option group.

    Specifically... the question is
    "Have you ever been taught? If so how?"
    Yes... education
    Yes... job skills.
    No.

    And since the checks can be any of the three (I'm making this to someone's specifications, otherwise I would have simply allowed for a text box), I want a text box to display if education or job skills are checked.

    However, the text box should not display when "no" is chosen. Currently I have the field for the question as a cell in a table, and two text boxes (one for education, one for job skills) being displayed. Currently, clicking on a checkbox causes an option button (since the question's type is set to Yes/No) to enable, and disable if I click on the option button again.

    But... the checkboxes themselves so not check/uncheck. They simply highlight.

    Any help is appreciated, thanks in advance!

  2. #2
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Welcome to the forum!

    I'm sorry but I don't understand your explanation of the problem. For instance, what do you mean with "I have the field for the question as a cell in a table"? Also the relationship among the checkbox, option button, option group and textbox is not clear.

    If you have several checkboxes or option buttons in an option group then every one of them has a unique value: it's OptionValue property. To know which control is active ("selected") in the option group you can test the Value property of the option group, not any property of the controls (checkboxes or option buttons) included in the option group.

    let's suppose that you have three option buttons:
    Option_1 (OptionValue = 1)
    Option_2 (OptionValue = 2)
    Option_3 (OptionValue = 3)
    included in an option group:
    Option_Group (DefaultValue = 1)

    When the form opens, Option_1 is active ("selected"), this is because the DefaultValue property of Option_Group is set to 1. If you click on another option button (Option_2 or Option_3), this will activate the AfterUpdate event of the Option_Group control and the Value property of Option_Group will become the same as the OptionValue property of the selected option button.

    So, in your case it should be something like:
    Code:
    Private Sub Option_Group_AfterUpdate()
    
        If Me.Option_Group.Value = 1 Then        ' 1 = education
        
            ' do something, among others:
            '
            Me.TextBox_1.Visible = True
        ElseIf Me.Option_Group.Value = 2 Then    ' 2 = job skills
        
            ' do something, among others:
            '
            Me.TextBox_1.Visible = True
        ElseIf Me.Option_Group.Value = 3 Then    ' 3 = no
        
            ' do something, among others:
            '
            Me.TextBox_1.Visible = False
        End If
            
    End Sub
    If this does not solve your problem, do not hesitate to call for more help.

    More generally, when you post a question please try to be more specific: supply the list of the objects (controls) involved, their name and relevant properties, and also any portion of code that is related to the situation you describe. In other words, help us to help you faster and better.
    Have a nice day!

  3. #3
    Join Date
    Apr 2010
    Posts
    3
    My apologies for the vagueness!

    Let me elaborate (I also apologize for the lateness, I had a final exam today and I was focused on that):

    I have an option group for the question
    "Have you ever been taught? If so how?" as a field name with data type Yes/No. The thing is, the question really has three responses:
    Yes... education
    Yes... job skills.
    No.
    So I created an option group with 3 check boxes in there, and currently when I load the form in Form View, there is one radio button (pertaining to the Yes/No I think), and 3 checkboxes in an option group.
    Corresponding to the checkboxes are text boxes. Or, well, I'd like them to correspond anyway. Two textboxes exist with data types Text, named "If education explain" and "If job skills explain". I don't need a text box or anything for No though, since it needs no elaboration. I'm wondering if perhaps I have just gone about this entirely wrong.

    I am doing this in a group, and I was told the data type Yes/No for the "Have you ever been taught? If so how?" was correct, but I was thinking that it should be Text since there are 3 actual values.

    I'm terrible at macro/visual basic (coding in general)... I haven't had to do much of it since my freshman year of college, and I am hoping to not disappoint the people who I offered to help make a form for.

    I'm terribly confused, and thank you for your help so far!

  4. #4
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    An option group is used to return one unique value among several options. In you case, you can consider that you have a system with 3 values: Yes-Education, Yes-Job skills, No. In this case you can use a single option group control with 3 option buttons, checkboxes or radio buttons. This is the simplest and correspond to the code I posted previously:
    Code:
    If Me.Option1.Value = 0 Then    ' 0 = No
        Me.TextBoxEducation.Visible = False
        Me. TextBoxJobSkills.Visible = False
    ElseIf Me.Option1.Value = 1 Then    ' 1 = Education
        Me. TextBoxEducation.Visible = True
        Me. TextBoxJobSkills.Visible = False
    Else                                   ' 2 = Job skills
        Me. TextBoxEducation.Visible = False
        Me. TextBoxJobSkills.Visible = True
    End If
    You can also consider that you have a system with 2 levels:
    Level 1: No, Yes (0, 1)
    Level 2: Education, Job skills, (Null in case level 1 = No) (1, 2)
    In this case, you can have a first option group control whith 2 option buttons, checkboxes or radio buttons: Yes and No, then a second option group control that's only visible (or enabled) when the value of the first option group means Yes. This second option group also have two controls (checkboxes, etc.) : Education and Job skills.

    In this case, the way to control the visibility of the textboxes (and of the second option group) is a bit more complex:
    Code:
    If Me.Option1.Value = 0 Then    ' 0 = No
        Me. TextBoxEducation.Visible = False
        Me. TextBoxJobSkills.Visible = False
        Me.Option2.Visible = False   ' or: Me.Option2.Enabled = False
    Else
        Me.Option2.Visible = True    ' or: Me.Option2.Enabled = True
        If Me.Option2.Value = 1 Then       ' 1 = Education
            Me.TextBoxEducation.Visible = True
            Me.TextBoxJobSkills.Visible = False
        Else                                  ' 2 = Job skills
            Me.TextBoxEducation.Visible = False
            Me.TextBoxJobSkills.Visible = True
        End If
    End If
    Have a nice day!

  5. #5
    Join Date
    Apr 2010
    Posts
    3
    Thanks for your help! I'm getting a weird error though:
    The expression On Click you entered as the event property setting produced the following error: Invalid outside procedure.

    * The expression may not result in the name of a maro, the name of a user-defined function, or [Event Procedure].
    * There may have been an error evaluating the function, event, or macro.

    This is strange, since I made sure to set the textboxes' visibilities to no in their Properties as well, and this didn't change anything.

    I set the VB code to:


    Private Sub Option01_Click()
    If Me.Option01.Value = 0 Then ' 0 = No
    Me.EducationalExplain.Visible = False
    Me.JobSkillExplain.Visible = False
    ElseIf Me.Option01.Value = 1 Then ' 1 = Education
    Me.EducationalExlain.Visible = True
    Me.JobSkillExplain.Visible = False
    Else ' 2 = Job skills
    Me.EducationalExlain.Visible = False
    Me.JobSkillExplain.Visible = True
    End Sub

    Did I need to still put TextBox in front of the EducationalExplain and JobSkillExplain? My apologies, thanks again!

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Do not use the Click event, use the AfterUpdate event instead.
    Have a nice day!

  7. #7
    Join Date
    Aug 2009
    Location
    Up Nort' Wi
    Posts
    140
    Mornin,

    That and if you copied this code directly from the db I think you may have a typo.

    "Me.EducationalExlain.Visible = True" Should that be "Me.EducationalExplain.Visible = True" ? Looks like it's in there a couple times.


    Sam, just a shot in the dark
    Good, fast, cheap...Pick 2.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •