Results 1 to 10 of 10
  1. #1
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172

    Locking/Unlocking a Control

    Hi,



    I want to lock a control unless the date in another control has been populated. I thought I'd do this in the 'BeforeUpdate' event, however I'm getting an error. My code is:

    Private Sub Field_1_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.Field_2) Then
    Me.Field_1.Locked = True
    MsgBox "MyMsg"
    Else
    Me.Field_1.Locked = False
    End If
    End Sub

    But, I'm getting "Run-time Error '2166' You can't lock a control while it has unsaved changes" when I try to update Field_1 after updating Field_2.

    This error makes sense to me, but any ideas around how else I can get this to work would be appreciated.

    Thanks - Kirsti

  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
    Since you're testing field 2, I'd do this in the after update event of that control.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Ahh, that makes sense. I'll give it a try. Thanks.

  4. #4
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Ok, that didn't work.

    I'm trying to prevent data entry into Field_1 unless Field_2 has been populated.

  5. #5
    ssanfu is offline Master of Nothing
    Windows 2K Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    It is very confusing looking at your example because you didn't use good field names.

    If you have set the form default view to Continuous Forms, depending on the current record value for Field_2, every Field_1 control will change Locked status.

    It might be better to use conditional formatting. See attached mdb.....

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Quote Originally Posted by Kirsti View Post
    Ok, that didn't work.
    It would be more helpful if you posted exactly what code "didn't work", and detail what "didn't work" means.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Sorry Paul,

    It 'didn't work' because my code is:

    Private Sub Field_2_AfterUpdate
    If IsNull(Me.Field_2) Then
    Me.Field_1.Locked = True
    MsgBox "MyMsg"
    Else
    Me.Field_1.Locked = False
    End If
    End Sub

    Obviously if it's on the AfterUpdate event, then Field2 will never be Null, which is why I tried to put it on the BeforeUpdate of Field1 to start with.

    So what I want, it to prevent users updating Field1 if Field2 has not been updated.

    Steve - thanks I will look at your example.

  8. #8
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    Ok, thanks. After a weekend away from thinking about this, I have it working - using your idea Paul, and not over complicating it!

    I changed the Locked property of Field1 to Yes, and then used this code:

    Private Sub Field2_AfterUpdate()
    Me.Field1.Locked = False
    End Sub

    Thanks again for your help.

    Kirsti

  9. #9
    Kirsti is offline Competent Performer
    Windows 7 32bit Access 2003
    Join Date
    Jan 2012
    Location
    New Zealand
    Posts
    172
    And, just in case anyone is interested - to get my messagebox working, I used the following code:

    Private Sub Field1_KeyDown(KeyCode As Integer, Shift As Integer)
    If IsNull(Me.Field2) Then
    MsgBox "MyMsg"
    Else
    Me.Field1.Locked = False
    End If
    End Sub

    And now I have exactly what I wanted.

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,642
    Glad you got it sorted out. I would probably have used the current event to lock field 1 if field 2 is Null, and the after update event of field 2 to unlock it as appropriate.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Unlocking an Access database?
    By kp123 in forum Access
    Replies: 6
    Last Post: 02-07-2012, 08:48 AM
  2. Replies: 3
    Last Post: 07-15-2011, 08:31 AM
  3. locking it down
    By BAX5127 in forum Access
    Replies: 0
    Last Post: 03-22-2011, 06:51 PM
  4. Replies: 1
    Last Post: 11-25-2010, 11:00 AM
  5. Locking fields
    By Icewolf0927 in forum Forms
    Replies: 3
    Last Post: 09-23-2010, 12:01 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