Results 1 to 5 of 5
  1. #1
    Pascal Notic is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2013
    Location
    Mauritius
    Posts
    2

    To enable or disable a field in a form by changing the value of other fields

    Hi everybody, I've got a form based on a table where i have a field called MaidenName. I want to enable or disable the field depending on the value of two other field called Sex and Status. For example i would like to enable the field of MaidenName only when i have 'female' and 'married' in the field of Sex and Status respectively. I have only two values to choose in each of these two fields, that is, Female and Male for Sex of course and Single and Married for the Status field. I would be much grateful if somebody could help on this issue. Thanks Pascal.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Set the MaidenName textbox as Enable No or Visible No. Then code in both Sex and Status AfterUpdate events like:

    Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
    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.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    It's not quite as simple as that. When the first Field (either sex or status) is populated the code will fire and you'll get an 'Invalid use of Null' error message, because the other Field is Null, at this point. So you need to only run the code if both Fields have data in them.

    You also need to use the Form_Current event to be sure that the Visibility of MaidenName is appropriate for that Record, else it will be formatted according to the condition of the previous Record viewed.

    The same thing applies to a New Record. If you don't address this, the Visibility of the Field will depend on the Visibility of the last Record viewed. In the code below, in the Form_Current event, I set the Visibility, on a New Record, to True. That's a personal preference, so you can change that to False, if you like.

    Code:
    Private Sub Sex_AfterUpdate()
    If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
     Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
    End If
    End Sub
    
    Private Sub Status_AfterUpdate()
    If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
     Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
    End If
    End Sub
    
    Private Sub Form_Current()
    If Me.NewRecord Then
     Me.MaidenName.Visible = True
    End If
    If Nz(Me.Status, "") <> "" And Nz(Me.Sex, "") <> "" Then
     Me.MaidenName.Visible = Me.Sex = "female" And Me.Status = "married"
    End If
    End Sub


    Also note that you can only do this, in Code, if this is a Single View Form. For a Datasheet View Form or a Continuous View Form, you'll have to use the Enabled Property and Conditional Formatting, but we'll address that only if we need to.

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Pascal Notic is offline Novice
    Windows Vista Access 2007
    Join Date
    Mar 2013
    Location
    Mauritius
    Posts
    2
    Hi June7 and Missinglinq.. I would like to thank you both for the time you took to reply to my question. I was able to use the code of Missinglinq and it worked fine though i would have preferred to use enable instead of visible. Stay blessed.
    Pascal.

    P.S. If in case it's not too much asking and if does not require much of your time I would be grateful if you could let me have the code for the enable too. Thanks

  5. #5
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by Pascal Notic View Post

    ...If in case it's not too much asking and if does not require much of your time I would be grateful if you could let me have the code for the enable too...
    Don't know if I have enough time, and know that you're new at this, and this is kind of complicated, but

    1. Search through the code I gave you
    2. Find Visible
    3. Replace it with Enabled
    4. Repeat Steps 2-3 until done

    Couldn't resist that!

    Glad we could help!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

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

Similar Threads

  1. Password Form With Enable/Disable Shift Key
    By burrina in forum Code Repository
    Replies: 0
    Last Post: 12-24-2012, 10:53 PM
  2. Enable or Disable Field in Forum
    By lolos66666 in forum Forms
    Replies: 5
    Last Post: 03-13-2011, 05:30 PM
  3. Replies: 1
    Last Post: 02-25-2011, 10:03 AM
  4. Function to Enable/Disable Field
    By swalsh84 in forum Programming
    Replies: 5
    Last Post: 11-04-2010, 02:48 PM
  5. Replies: 3
    Last Post: 09-29-2010, 09:31 AM

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