Check Boxes that Disable Text Fields?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Eric`
    New Member
    • Aug 2010
    • 3

    Check Boxes that Disable Text Fields?

    I am trying to create a database that will track personal information on clients I am trying to create a checkbox for marital status specifically single that will disable the other text boxes such as names, phone, D.O.B. and other things like this the code that I have tried follows. The check box will also need to disable the other check boxes such as Couple, Family, and Single with Children, untill the original check box is unchecked. Can anyone help with this??
    Thanks in advance! :-)

    Code:
     
    Private Sub Single_Click()
        If "Y" Then
            txtLastName1.Enabled = False
            Else
            txtLastName1.Enabled = True
    End If
    End Sub
    or
    
    Private Sub Single_Click()
         If (Y) Then
             txtLastName1.enabled = False
             txtFirstName1.enabled = False
    End Sub
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi Eric. You need to test the value of the checkbox, which you are not doing at present. You are not actually referring to its value at all when you write If "Y" or If (Y).

    If "Y" <-- this is actually testing to see if string literal "Y" is true, which will always result in False.

    If (Y) <-- this will test to see if some variable called Y is set to true. If you are not getting a compiler error at this point it means you do not have explicit compiler checking for undeclared variables on - use Option Explicit at the heading of the code module to turn this feature on.

    Checkboxes return boolean values, True and False, represented as the integers -1 and 0 respectively.

    You will need something like the following in the after update event of the checkbox (if available), not its single-click event.

    Code:
    Private Sub YourCheckBoxName_AfterUpdate()
      Dim blIsEnabled as Boolean
      blIsEnabled = Not(Me.YourCheckBoxName)
      txtLastName.Enabled = blIsEnabled
      txtFirstName.Enabled = blIsEnabled
    End Sub
    If you are doing this in Access you will also need to run YourCheckBoxNam e_AfterUpdate from the On Current event of the form, to ensure that the correct fields are enabled/disabled for existing records when you display them.

    -Stewart
    Last edited by Stewart Ross; Aug 2 '10, 12:36 PM.

    Comment

    • Eric`
      New Member
      • Aug 2010
      • 3

      #3
      Stewart this is the code that I currently have, although I am getting a runtime error of '424' Object required, does the code look right to you? I am very new at this I appreciate you help very much! It was first highlighting Line 7 then it hightlighted line 8) I am also now getting runtime error '94' invalid use of null??? (Only when I opening of the form)
      Thanks Again for the help

      Code:
      Private Sub Form_Current()
      Single_AfterUpdate
      End Sub
      
      Private Sub Single_AfterUpdate()
      Dim blIsEnabled As Boolean
        blIsEnabled = Not (Me.Single)
        txtLastName.Enabled = blIsEnabled
        txtFirstName.Enabled = blIsEnabled
      End Sub

      Comment

      • Eric`
        New Member
        • Aug 2010
        • 3

        #4
        Sorry Stewart I became confused, It gives me runtime error 94 when I open and it highlights line 7 above, when I click the checkbox on the form it then gives me runtime error 424 and highlights line 8.

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          Hi Eric. The error occurs when you are on a new (blank) record, as initially the values of all controls on a new form are null, at least until you start entering data into one of the controls on the form.

          To guard against this error you can test the NewRecord property of the form and set variable blIsEnabled true if you are on a new record:

          Code:
          Private Sub Single_AfterUpdate() 
          Dim blIsEnabled As Boolean
            If Me.NewRecord then
              blIsEnabled = True
            Else
              blIsEnabled = Not (Me.Single) 
            End If
            txtLastName.Enabled = blIsEnabled 
            txtFirstName.Enabled = blIsEnabled 
          End Sub
          This small change will initially enable the fields when on a new record. They will be disabled should the checkbox Single be ticked subsequently.

          -Stewart
          Last edited by Stewart Ross; Aug 3 '10, 11:43 AM.

          Comment

          Working...