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: Newest
  1. Anonymous
    2010-11-18T19:01:14+00:00

    When you need to use the same code in more than one place, you create a code maintenace problem because any change in one copy must be duplicated in the other place(s) and it's a pain to corrently make exactly the same changes multiple times.  Think of it as a kind of code normalization rule.

    If you can come up a fairly rigid pattern for a bunch of control that do essentially the same kind of thing, you may be able to use arguments to a single procedure that can be used for the whole bunch of controls, but that's starting to get moderately advanced.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-11-18T18:26:51+00:00

    ok, just to make sure I got what you were saying.  Are you saying that along with my current code:

    Private Sub Event_Location_AfterUpdate()

    If Event_Location = "Other" Then

       Me.Event_Location_Other_Specify.Enabled = True

       Me.Event_Location_Other_Specify.SetFocus

    Else

       Me.Event_Location_Other_Specify.Enabled = False

       Me.Persons_Present_Agency_Staff.SetFocus

    End If

    End Sub

    I should create a new sub for each one of these and replicate the code like this:

    Private Sub Check_Event_Location()

    If Event_Location = "Other" Then

       Me.Event_Location_Other_Specify.Enabled = True

       Me.Event_Location_Other_Specify.SetFocus

    Else

       Me.Event_Location_Other_Specify.Enabled = False

       Me.Persons_Present_Agency_Staff.SetFocus

    End If

    End Sub

    Then I place all the check_event_location, check_some_other field etc in the current form.  What is creating the check procedures for?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-11-18T04:50:43+00:00

    That's because you only have the code run when you change the entry in Event_Location.  You need to have the same code in the form's Current event so it runs when you navigate to a record.

    The amount of code you have is about to the point where it would be a good idea to put it in it's own procedure:

    Private Sub Check_Event_Location()

    If Event_Location = "Other" Then

       Me.Event_Location_Other_Specify.Enabled = True

       Me.Event_Location_Other_Specify.SetFocus

    Else

       Me.Event_Location_Other_Specify.Enabled = False

       Me.Persons_Present_Agency_Staff.SetFocus

    End If

    End Sub

    THen you can call it from the AfterUpdate event and from the Current event.  All it takes is simply using:

       Check_Event_Location

    You'll probably want to do the same kind of thing for other controls so the Current event would look something like:

       Check_Event_Location

       Check_This

       Check_That

        . . .

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-11-18T01:31:01+00:00

    Okay so I got these codes to work, but I noticed an issue.  Say you have this code:

    Private Sub Event_Location_AfterUpdate()

    If Event_Location = "Other" Then

       Me.Event_Location_Other_Specify.Enabled = True

       Me.Event_Location_Other_Specify.SetFocus

    Else

       Me.Event_Location_Other_Specify.Enabled = False

       Me.Persons_Present_Agency_Staff.SetFocus

    End If

    End Sub

    If you selct the option "other", it allows you to proceed to the other box, but if you delete the word other and come back to this record it still allows you to type in that other box.  It won't grey it back out unless you reprocess the entire first box. I am having this same issue with check boxes that grey out other text fields. For example, if you check a box it ungreys a text field, but if you go to another record and come back to this record, the text field is greyed out again.

    Was this answer helpful?

    0 comments No comments