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-23T10:24:58+00:00

    Here is the print screen to show you the field has a yes/no data type.  I took out the default value of "no" and used your nz code.  It now looks like this:

    Private Sub Who_Was_Notified_Police_AfterUpdate()

    If Nz(Who_Was_Notified_Police, False) Then

        Me.Who_Was_Notified_Police_Name.Enabled = True

        Me.Who_Was_Notified_Police_Date.Enabled = True

        Me.Who_Was_Notified_Police_Report_No.Enabled = True

        Me.Who_Was_Notified_Police_Name.SetFocus

      Else

        Me.Who_Was_Notified_Police_Name.Enabled = False

        Me.Who_Was_Notified_Police_Date.Enabled = False

        Me.Who_Was_Notified_Police_Report_No.Enabled = False

      End If

    End Sub

    I looked into the nz code and it is basically saying (correct me if I am wrong) that if the "Who_Was_Notified_Police" is null to return "false" and if not return the value of "Who_Was_Notified_Police". I don't understand how this still works if the answer is "no".  How does this code still know to disable the fields properly?  I ask this because to me, it doesn't seem to say enable the fields only when the "Who_Was_Notified_Police" is "true" and disable when "false".  I would've thought that you needed it to say something like "Who_Was_Notified_Police=who_was_notified_police_name and Nz(Who_Was_Notified_Police, False) somehow for when the value is null. The second code works too, but it actually works with just the "if Who_Was_Notified_Police = True" without the "If Not IsNull(Who_Was_Notified_Police)".  This second code seems to say only when the "Who_Was_Notified_Police" is "yes" or "true" or "-1" to enable me.who_was... fields, so if it is "no" "false" or "null" to disable and that one makes sense to me.  Sorry for so much questions, but I am just trying to understand these codes and how they work without just copying stuff you all help me with.  I appreciate all the time you all take to help us newbies.

    Was this answer helpful?

    0 comments No comments
  2. Anonymous
    2010-11-23T05:02:22+00:00

    Ahhh, you want to remove the value as well as disable the control.  That should be just a matter of setting it to Null:

        . . .

        Me.Who_Was_Notified_Police_Name.Enabled = True

      Else

        Me.Who_Was_Notified_Police_Name = Null

        Me.Who_Was_Notified_Police_Name.Enabled = False

      End If

    Are you sure that the field for Who_Was_Notified_Police is not a Text field in the table?  It's sort of looking like it's either that or you are confusing the Format with the DataType.  Did you get the If statement to work?  If you did, what does it finally look like?

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-11-23T00:48:20+00:00

    It may not be null but it isn't "no" if I don't specify "no" as the default value.  If I leave the default value blank the code I had errors out with the null error.  This goes for all the fields that are shown in my print screen with yes/no as the data type.  Both codes that you specified above does work.  I think the reason the if not isnull didn't work the first time because if you uncheck the box it actually isn't null or whatever it supposed to be since you say it can't be null and is no.  Since it is no it will still enable the other fields.  I needed the "and who_was_notified_police = true".  So the codes you gave me works, does that mean it isn't a yes/no data type?  If so how can this be if it is a yes/no data type in the table?

    Do you know how I can take out data from a disabled control if box is unchecked?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-11-23T00:35:13+00:00

    I don't know how many times I have to say that a Yes/No field can never be null (unless it comes from a nonexistent record in an outer join query).  If you are sure that Who_Was_Notified_Police is Null, then it is not a Yes/No field.

    To treat Null the same as False, try using:

       If Nz(Who_Was_Notified_Police, False) Then

    or

       If Not IsNull(Who_Was_Notified_Police) And Who_Was_Notified_Police = True Then

    But, if Who_Was_Notified_Policeis not a Yes/No field, then that probably will not work either.

    Was this answer helpful?

    0 comments No comments