Results 1 to 6 of 6
  1. #1
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581

    Locking a form

    I'm trying to lock a form. I have this programmed into it:
    Private Sub Form_Current()
    Dim Ctl As Control

    For Each Ctl In Me
    If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then
    Ctl.Locked = True


    Ctl.BackColor = RGB(200, 200, 200)
    End If
    Next

    End Sub

    It works great to lock the form as soon as it is opened. I have a yes/no field (FormLocked) that I want to control if it locks or not. How I'm sure that it will require an if statement. I'm having a problem writing the if statement to say that if I have FormLocked as true, I want the code to run, but if it FormLocked is False, I want the form not locked.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Consider:

    Ctl.Locked = Me!FormLocked

    You aren't locking the form, you are locking controls. The result may be the same but if you want to prevent all data entry/edit, could set the form AllowEdits, AllowAdditions, AllowDeletions properties to No.

    Is the form in Single View?

    Instead of VBA, consider using Conditional Formatting to set control Enabled and BackColor properties. This really is the only way to accomplish what you want for Continuous or Datasheet view form.
    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
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    I'm fine with just locking the controls. I'm new at VBA. It's a little confusing, but it seems to be a really good way to get things done. There is no way in VBA to say that if a field [FormsLocked] is true, to run the code that I wrote already? And if the field [FormLocked] is false to not run that code? I thought VBA would do that.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Yes.
    Code:
    If Me!FormLocked Then
       For Each Ctl In Me
            If Ctl.ControlType = acTextBox Or Ctl.ControlType = acComboBox Then
                Ctl.Locked = True
                Ctl.BackColor = RGB(200, 200, 200)
            End If
        Next
    End If
    Please put code between CODE tags to retain indentation and readability.
    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.

  5. #5
    UT227 is offline Expert
    Windows 7 32bit Access 2013 32bit
    Join Date
    Feb 2016
    Posts
    581
    That gave me other errors. It seemed to conflict with the other codes in the database.

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    What errors? What other codes?

    By CODE tags I meant in the forum post.
    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. Locking a record on a form
    By Sturgis76 in forum Programming
    Replies: 3
    Last Post: 03-11-2017, 09:27 PM
  2. Form locking other users out of DB
    By crowegreg in forum Forms
    Replies: 3
    Last Post: 03-15-2014, 10:49 AM
  3. Locking all controls in the form
    By undee69 in forum Forms
    Replies: 2
    Last Post: 12-10-2012, 01:52 AM
  4. Locking a tab in a tabbed form
    By focosi in forum Forms
    Replies: 7
    Last Post: 09-21-2011, 04:25 PM
  5. Locking Down Form
    By jo15765 in forum Forms
    Replies: 5
    Last Post: 11-16-2010, 04:44 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