Share via

Locked Forms

Anonymous
2017-10-16T21:00:19+00:00

I am fairly new to Access and am nearing completion of my first database. I was looking for a way to lock the data entry form so that users do not accidentally change data if they are moving between records. I have created a command button and inserted the vba code below. This code does lock the records upon opening the form, and it does allow me to edit the form when the command button is clicked, and switch back to being locked when clicked again. It is also creating two issues:

  1. I have two sub forms that are not allowing changes even when the edit button has been pressed. The subforms each have one search combo box, which may be part of the problem. They are each based on a table joining two other tables to offer a drop down menu.
  2. The user must click the command button again before moving to the next record, or it will not lock the next record upon opening.

Private Sub Form_Current()

If Me.NewRecord Then

    With Me

        .cmdEdit.Caption = "Edit"

        .cmdEdit.ForeColor = 0

        .cmdEdit.FontBold = False

        .AllowEdits = True

        .cmdEdit.Enabled = False

    End With

Else

    With Me

        .AllowEdits = False

        .cmdEdit.Caption = "Edit"

        .cmdEdit.ForeColor = 0

        .cmdEdit.FontBold = False

        .cmdEdit.Enabled = True

    End With

End If

End Sub

Private Sub cmdEdit_Click()

Dim cap As String

cap = Me.cmdEdit.Caption

Select Case cap

    Case "Edit"

        With Me

            .AllowEdits = True

            .cmdEdit.Caption = "Lock"

            .cmdEdit.ForeColor = 128

            .cmdEdit.FontBold = True

            .Refresh

        End With

    Case "Lock"

        With Me

            .AllowEdits = False

            .cmdEdit.Caption = "Edit"

            .cmdEdit.ForeColor = 0

            .cmdEdit.FontBold = False

            .Refresh

        End With

End Select

End Sub

Any assistance is much appreciated!

<The thread has been moved to the correct category by forum moderator>

Microsoft 365 and Office | Access | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
Answer accepted by question author
  1. Anonymous
    2017-10-17T21:49:48+00:00

    My guess would be that you've inadvertently tagged a control such as a label which doesn't support these properties.  Only bound controls such as text boxes, combo boxes etc, or a subform control (as with the sfcSubjects subform control in my demo) should be tagged.

    The Enabled or Locked properties won't show up in the list of properties when an object variable is used in code like this as the editor has no way of knowing what type of control is being referenced, and consequently can only list those properties common to all types of control.

    0 comments No comments
Answer accepted by question author
  1. Anonymous
    2017-10-17T17:27:37+00:00

    You might like to take a look at LockForm.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.  

    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.

    This little demo file illustrates how to lock/disable bound controls in a form/subform while still allowing an unbound navigational control, in my case a multi-select list box, to be operational.  Setting a form's AllowEdits property to False would prevent the use of such controls as well as the bound controls.  It works by firstly setting the Tag property of the bound controls you wish to lock/disable to a suitable string expression.  Code then loops through the form's Controls collection and locks/disables or unlocks/enables the tagged controls as appropriate.  It does this in the form's Current event procedure, and a button's Click event procedure, in the latter case toggling between locked/disabled and unlocked/enabled.

    0 comments No comments

5 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-10-17T14:34:30+00:00

    No problem!

    I would put it under *.AllowEdits = True*.

    0 comments No comments
  2. Anonymous
    2017-10-17T14:10:03+00:00

    Thanks for the tip Gina! I am not great with VBA (as in I got this code from a previously asked question). Where in the code should I insert the line Me.sbfKeyWords.Requery? And how should I include my second subform (the two subforms are independent of each other)? Thank you!!

    0 comments No comments
  3. Anonymous
    2017-10-17T02:33:02+00:00

    Hmm, for the first issue, I would suggest adding a Me.YourSubformName.Requery so it will lock\unlock accordingly because without it it basically stays with the first *order* it got.  For the second issue not so easy to resolve as it is expected behavior when putting an event in the On_Current event of a Form.  You would be better off using the On_Open event where the action happens once.  However, this presents a problem if you want records to lock back up at a certain point you will only have the command button to do so.

    0 comments No comments