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: Most helpful
  1. Anonymous
    2010-11-18T20:46:59+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.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-11-18T20:19:47+00:00

    okay, in order to make all similar fields perform like this, do I do this adding it to the form's current event? What about the private subs that don't have a setfocus code? What would I place for those in the form's current event?

    Thanks again.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-11-18T19:52:05+00:00

    I don't see anywhere in the code where Null would be a problem.

    But, I just noticed that the SetFocus line(s?) makes the code in the two procedures different so my argument about using a separate procedure to consolidate the code was a waste of time.  I guess you should skip that idea, delete the Check_Persons_Present_Other procedure and just add the line:

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

    to the form's Current event.  Leave the AfterUpdate event procedure as it is.

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-11-18T19:21:57+00:00

    okay so i tried this with the following code:

    Private Sub Persons_Present_Other_AfterUpdate()

    If Persons_Present_Other Then ' persons_present_other is ticked

         Me.Persons_Present_Other_Specify.Enabled = True

         Me.Persons_Present_Other_Specify.SetFocus

      Else

         Me.Persons_Present_Other_Specify.Enabled = False

      End If

    End Sub

    **Private Sub Check_Persons_Present_Other()**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

    Then it gives me an error for invalid use of null for the bolded.

    Was this answer helpful?

    0 comments No comments