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-10-15T16:25:30+00:00

    To add to what Scott has said I'd draw a distinction between control names and column (field) names.

    I myself use 'tags' for control names , txt for text box, cbo for a combo box, lst for a list box, chk for check box and so on.  Do not use these for the names of columns in a table, however, or for the names of tables.   The tag tbl is commonly used for table names, and sometimes tags will be used for column names, to identify the data type and/or the role of the column, e.g. pk for a primary key, fk for a foreign key column; I'm not sure what would be done with composite primary keys made up of more than one column where the individual columns are foreign keys, or indeed where a primary key is also a foreign key in a one-to-one relationship?

    Personally I dislike the use of tags at all for table and column names as I find these get in the way of the semantics, particularly when writing or reading complex SQL statements.  I've always followed Joe Celko's lead and try and use plural or collective names for tables, e.g. Employees, as this reflects the fact that a table is a set.  For columns I try and use singular nouns, e.g. FirstName as this reflects the fact that a column represents a single attribute of the entity type which the table represents

    One should not be prescriptive about this sort of thing, however.  At the end of the day it's best to use whatever one is most comfortable with.

    One thing I would adopt a firm position on, however, is that spaces or other special characters should be avoided in all object names; it makes life a lot easier.  For compound words like  FirstName then CamelCase can be used as here, though some people prefer camelCase, firstName, and others all lower case, firstname, for column names,  and proper case for table names.  Again the choice is yours.  The names used for objects are completely irrelevant as far as the user is concerned of course, as in any application worth the name they will only interface the database via forms and reports, in which the captions can be whatever is appropriate regardless of the names of the underlying objects.


    Ken Sheridan, Stafford, England

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,840 Reputation points Volunteer Moderator
    2010-10-15T12:21:35+00:00

    Thanks I got this to work now.  Is there a place that I can look at that shows all these different codes because I noticed that the code for a check box, combo box, and text box are all different?  Also, is there a way to have the field name be supervisor's instead of just supervisors? I changed the code to not include the ' because this was causing the code to change?

     

     

    First, the code is NOT different. The only thing that changes is the control names. See my previous explanation. 

    Second, you should not use spaces in object names, and especially not apostrophes. Most developers use a naming convention to identify the type of control like chkcontrolname for check boxes or txtcontrolname for text boxes. If you search on Access naming conventions you will find examples of commonly used prefixes.


    Hope this helps, Scott<> P.S. Please post a response to let us know whether our answer helped or not. Microsoft Access MVP 2009 Author: Microsoft Office Access 2007 VBA Technical Editor for: Special Edition Using Microsoft Access 2007 and Access 2007 Forms, Reports and Queries

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2010-10-15T00:20:32+00:00

    Thanks I got this to work now.  Is there a place that I can look at that shows all these different codes because I noticed that the code for a check box, combo box, and text box are all different?  Also, is there a way to have the field name be supervisor's instead of just supervisors? I changed the code to not include the ' because this was causing the code to change?

    Was this answer helpful?

    0 comments No comments
  4. Anonymous
    2010-10-14T21:26:56+00:00

    Ok, I tried this code based on a text box as shown below, but it apparently didnt do anything. I was trying to have another box disabled or greyed out if another text box was left blank or null:

    Private Sub Text_box_AfterUpdate()

    If Me.text_box.Enabled = Null Then

    Me.Text_box_2.Enabled = False

    Else

    Me.Text.box_2.Enabled = True

    End If

    End Sub

    Was this answer helpful?

    0 comments No comments