Results 1 to 5 of 5
  1. #1
    hcuk is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    22

    Question Grey out a field based on a different field (USING VBA)


    Hello,

    I am trying to write a code where if a user selects a value from a dropdown menu in a field, then it would grey out another field.

    Here is an example:
    I have a field called production type with two options in it, "Oil" or "Gas".

    I have another field called Oil production per year and another called Gas production per year.

    Let's say the user selects oil in the Production type field, I would like the gas production per year field to be disabled and the Oil Production per year field stays enabled.
    If the user selects Gas then the Oil production per year should be disabled and the Gas production per year is enabled.

    This is the code i tried.
    Code:
    Private Sub Production_Type_Combo_AfterUpdate()    If Me.Production_Type_Combo = "Gas" Then
        Me.Oil_Production_Increase_Per_Day_Text.Enabled = False And Me.Gas_Production_Day_Increase_Text.Enabled = True
        ElseIf Me.Production_Type_Combo = "Oil" Then
        Me.Gas_Production_Day_Increase_Text.Enabled = False And Me.Gas_Production_Day_Increase_Text.Enabled = True
        Else:
        Me.Gas_Production_Day_Increase_Text.Enabled = True And Me.Oil_Production_Increase_Per_Day_Text.Enabled = True
        End If
    End Sub
    Thank you in advance!

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    For starters you'd put each on it's own line without "And":

    Me.Oil_Production_Increase_Per_Day_Text.Enabled = False
    Me.Gas_Production_Day_Increase_Text.Enabled = True
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Is this on a datasheet or continuous view form? Use Conditional Formatting.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  4. #4
    hcuk is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    22
    Quote Originally Posted by pbaldy View Post
    For starters you'd put each on it's own line without "And":

    Me.Oil_Production_Increase_Per_Day_Text.Enabled = False
    Me.Gas_Production_Day_Increase_Text.Enabled = True
    This solved it. Thank you very much!

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Simpler construct:

    Private Sub Production_Type_Combo_AfterUpdate()
    Me.Oil_Production_Increase_Per_Day_Text.Enabled = (Me.Production_Type_Combo = "Oil")
    Me.Gas_Production_Day_Increase_Text.Enabled = (Me.Production_Type_Combo = "Gas")
    End Sub
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-09-2015, 05:48 PM
  2. Replies: 9
    Last Post: 08-25-2014, 05:09 PM
  3. Replies: 4
    Last Post: 01-23-2014, 04:34 PM
  4. Replies: 2
    Last Post: 03-07-2013, 04:50 PM
  5. Replies: 3
    Last Post: 10-03-2011, 02:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums