CampSoup1988
CampSoup1988

Reputation: 139

Change current read-only form record to be editable

I am using Microsoft Access 2013 to create a database where I have a form that the default view is read-only to prevent accidental editing. I am currently trying to include a button to enable editing for the current record only. I tried using DoCmd.OpenForm to open the record in editable mode (since I am using that command elsewhere to open specific records), but it seems like it can not open a record within the same form with that command.

I thank you in advance for any advice on how to solve this issue.

Upvotes: 1

Views: 5872

Answers (2)

KenSquare
KenSquare

Reputation: 300

I learned this from the first VBA book I ever read (Access 97 VBA for Dummies, I think), and I have never seen anyone use the Tag property since, which has no other purpose than for you to find something to use it for. Enter "Lockable" in the tag property for any control (text box, combobox etc.) that you want to protect. Add a button named btnEdit to your form.

When you enter a record, the Form Current event will unlock the controls if it is a new record, and lock the controls if it is an existing record.

The button will then unlock to allow edits, and re-lock once the record is exited. I've found this to be very effective in preventing inadvertent edits.

 Private Sub Form_Current()

 Dim ctlCurr As Control
    'Lock the record if it is not new. Prevents inadvertent edits.
    If Me.NewRecord = False Then
        For Each ctlCurr In Me.Controls
            If ctlCurr.Tag = "Lockable" Then
            ctlCurr.Locked = True
            End If
            btnEdit.Enabled = True
        Next
    End If
    'Unlock a new record for editing.
    If Me.NewRecord = True Then
        For Each ctlCurr In Me.Controls
            If ctlCurr.Tag = "Lockable" Then
            ctlCurr.Locked = False
            End If
        Next
        btnEdit.Enabled = False
    End If  
End Sub

Private Sub btnEdit_Click()
      Dim ctlCurr As Control
        'Unlocks a record for editing.  Requires the operator to make the decision to edit.
        If Me.NewRecord = False Then
            For Each ctlCurr In Me.Controls
                If ctlCurr.Tag = "Lockable" Then
                ctlCurr.Locked = False
                End If
            Next
        End If
    End Sub

Upvotes: 1

Gustav
Gustav

Reputation: 55951

You cannot edit anything else than the current record.

Use this code line:

Me.AllowEditions = True

But it doesn't make much sense as you could just open the form this way.

Upvotes: 1

Related Questions