Disable or Grey out Check box If Combo or Text Box Not Filled In

Anonymous
2010-10-14T18:47:39+00:00

Hi,

I could get a combo box to disable text boxes using the code below, but how can I make this work if it isn't a text box and it is a check box?:

Private Sub Combo_Box_AfterUpdate()

Me.Text_Box.Enabled = (Me.Combo_Box.ListIndex >= 0)

End Sub

Thanks

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
Anonymous
2010-10-14T21:56:54+00:00

A few things wrong here:

1.  You cannot compare something to Null.  Nothing equals Null, not even Null.  The IsNull function is used to determine if something is Null.

2.  In any case the Enabled property is either True or False, not Null.

So:

Private Sub Text_box_AfterUpdate()

    If IsNull(Me.text_box) Then

        Me.Text_box_2.Enabled = False

    Else

        Me.Text.box_2.Enabled = True

    End If

End Sub

or more succinctly:

Private Sub Text_box_AfterUpdate()

     Me.Text.box_2.Enabled = Not IsNull(Me.text_box)

End Sub

If this is in a bound form and you want the Text.box_2 to be disabled when you move to an existing record where Text_box is Null, then also put the code in the form's Current event procedure.


Ken Sheridan, Stafford, England

Was this answer helpful?

0 comments No comments

56 additional answers

Sort by: Oldest
  1. Anonymous
    2010-11-18T21:09:24+00:00

    If all the AfterUpdate event does is enable/disable another control I don't see a significant reason to create a separate procedure for just one line of code though there would be nothing wrong with the idea if you want to do it.

    AFAICT, everything should be fine if you just add the one line to the Current event for each control.

    Okay so if first of all what is the Me.Persons_Present_Other_Specify.Enabled = Nz(Persons_Present_Other, False) saying?  How did you get the NZ part? I'm just trying to understand these codes instead of just copying your code so I can hopefully be able to do this on my own.  Also, how do you place this code within the form's current event so that it will take effect even when you leave the record and return?

    Private Sub Persons_Present_Other_AfterUpdate()

    If Persons_Present_Other Then ' persons_present_other is ticked

         Me.Persons_Present_Other_Specify.Enabled = True

      Else

         Me.Persons_Present_Other_Specify.Enabled = False

      End If

    End Sub

    If you created this:

    Private Sub check_Persons_Present_Other_AfterUpdate()

    If Persons_Present_Other Then ' persons_present_other is ticked

         Me.Persons_Present_Other_Specify.Enabled = True

      Else

         Me.Persons_Present_Other_Specify.Enabled = False

      End If

    End Sub

    You can simply place in "check_Persons_Present_Other" in the form's current event so that the code is always in effect?

    Was this answer helpful?

    0 comments No comments
  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Anonymous
    2010-11-18T23:44:44+00:00

    Me.Persons_Present_Other_Specify.Enabled = Nz(Persons_Present_Other, False)

    It's possible for a check box to have a Null value if it is bound to a numeric type field (eg. Integer) so I added that just in case.  All the Nz function is doing here is returning False if the check box value is Null.  Your procedure's code uses If [check box] Then and the If will take the Else path if the check box's value is Null.  You can not set the Enabled property to Null, so I used Nz to make sure that in all cases the one line did the same thing your five line procedure does.  If your check boxes are bound to Yes/No type fields, the Nz serves no purpose and can be omitted.

    If you want to create a separate procedure, it would be:

       Private Sub check_Persons_Present_Other()

    without _AfterUpdate in it's name, then you can use:

       check_Persons_Present_Other

    in the Current event (instead of the above Enable line) to cover the case where you just navigate to the record.

    OTOH, if your AfterUpdate procedure, does not have the SetFocus, you can call the AfterUpdate procedure from the Current event without creating a separate procedure.  This way, the line in the Current event would be:

       Persons_Present_Other_AfterUpdate

    You have quite a few choices with such a simple procedure and as long as you remember that the code is used or called in two places, there is not a great distinction which way you choose to go.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-11-19T00:13:01+00:00

    Thanks.  Here is the code that I currently have but it gives me and invalid use of null error on the bolded procedure:

    Private Sub Form_Current()

    Frame28_AfterUpdate

    Participant_First_Name_AfterUpdate

    Event_Location_AfterUpdate

    End Sub

    **Private Sub Frame28_AfterUpdate()**If Frame28 Then ' if frame28 is ticked

        Me.Participant_Last_Name.Enabled = True

        Me.Participant_First_Name.Enabled = True

        Me.Birth_Date.Enabled = True

        Me.Unit_No.Enabled = True

        Me.Provider_Name.Enabled = True

        Me.Island.Enabled = True

        Me.Contact_Person.Enabled = True

        Me.Telephone_No.Enabled = True

        Me.Fax_No.Enabled = True

        Me.Relationship.Enabled = True

        Me.Supervisor_Name_Designated_Rep_Title.Enabled = True

      Else

        Me.Participant_Last_Name.Enabled = False

        Me.Participant_First_Name.Enabled = False

        Me.Birth_Date.Enabled = False

        Me.Unit_No.Enabled = False

        Me.Provider_Name.Enabled = False

        Me.Island.Enabled = False

        Me.Contact_Person.Enabled = False

        Me.Telephone_No.Enabled = False

        Me.Fax_No.Enabled = False

        Me.Relationship.Enabled = False

        Me.Supervisor_Name_Designated_Rep_Title.Enabled = False

      End If

    End Sub

    Private Sub Participant_First_Name_AfterUpdate()

    Me.SECTION_A_GENERAL_INFORMATION_TAB.Enabled = Not IsNull(Participant_First_Name)

    End Sub

    Private Sub Event_Location_AfterUpdate()

    If Event_Location = "Other" Then

        Me.Event_Location_Other_Specify.Enabled = True

      Else

        Me.Event_Location_Other_Specify.Enabled = False

      End If

    End Sub

    Was this answer helpful?

    0 comments No comments