Greying Out One Field Based On Another

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shortstuff12345
    New Member
    • May 2007
    • 11

    Greying Out One Field Based On Another

    I'm trying to use VBA code to disable a field based on the value of another field in a form. The code I have properly updates the enabled property of the field when it changes; however, it changes it for the form as a whole instead of a single record. My form is set as a single form.

    For example on record 1, field1's value is "records" and field2's enabled property is set to false. When I go to the next record (record 2), field2's enabled property remains set to false even though field1's value is now "measurable " which should make field2's enabled property as true. Here's the code I have right now:

    Code:
    Private Sub Field1_AfterUpdate()
    Me.Field2.Enabled = True
    If Me.Field1 = "records" Then
        Me.Field2.Enabled = False
    ElseIf Me.Field1 = "measurable" Then
        Me.Field2.Enabled = True
    End If
    I've tried using a requery setting on the form and also on field1's properties of GotFocus, but I haven't had any success. Any help would be greatly appreciated.

    Thanks!
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    Anytime you do something like this in the AfterUpdate event you also have to place the same code in the Form_Current event in order to persist the formatting as you move from record to record.

    Welcome to Bytes!

    Linq ;0)>

    Comment

    • Krandor
      New Member
      • Aug 2008
      • 50

      #3
      Also, adding DoEvents after text field changes forces an immediate screen update instead of waiting for a later time. For many situations, it is the only way you are going to see the change.

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Originally posted by Krandor
        Also, adding DoEvents after text field changes forces an immediate screen update instead of waiting for a later time. For many situations, it is the only way you are going to see the change.
        What are you talking about? DoEvents is not needed for the OP's code to work under any circumstance that I'm aware of! In point of fact, his code was changing the Enabled state of the other control without problem. The problem was that when Field2 was disabled in RecordA and the user moved to RecordB Field2 was also disabled there. Placing the code in the Form_Current event solves this.

        DoEvents is primarily used to fix timing issue problems. It returns control from Access to Windows so that Windows can complete an operation, such as printing a report before Access tries to start printing a second report, or completing the running of one query before running another query, where the first must be completed before the second can be run with the correct results.

        Comment

        • Shortstuff12345
          New Member
          • May 2007
          • 11

          #5
          Linq -

          I added my code to the form's OnCurrent property and it does exactly what I want. Thanks for your help!

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            Glad we could help!

            Linq ;0)>

            Comment

            Working...