1
votes

I currently have a form that is locked to prevent accidental editing. However, the form contains information such as customer contact information that might need to be edited. I currently have a command button that users can click that closes the form and reopens it in "edit" mode. What I would like to happen is to have all other command buttons on the page disappear when the form reloads and a "save" button appear. Once the save button is clicked I want the form to go back to non-editable and all the command buttons to reappear.

I currently have something similar set up on another form in the database (see code below) but that is for adding a new record. How do I set it up for an existing record? Is there a more elegant way to do it besides closing and reopening the form? I've searched for quite a while and can't seem to find anything to this end. Please let me know if you need more background information about how the database is structured or more specifics with what I am trying to do. Thank you for your help!

*

Private Sub add_Click()
On Error Resume Next
DoCmd.GoToRecord , , acNewRec
End Sub
Private Sub Form_Current()
   If Me.NewRecord Then
    Me.recordcounter.Caption = "New Record"
    Me.next.Visible = False
    Me.previous.Visible = False
    Me.first.Visible = False
    Me.last.Visible = False
    Me.add.Visible = False
    Me.save.Visible = True
   Else
    Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount
    Me.next.Visible = True
    Me.previous.Visible = True
    Me.first.Visible = True
    Me.last.Visible = True
    Me.add.Visible = True
    Me.save.Visible = False
    End If
 End Sub
Private Sub save_Click()
DoCmd.save
    Me.previous.Visible = True
    Me.first.Visible = True
    Me.recordcounter.Caption = "Record " & Me.CurrentRecord & " of " & Me.Recordset.RecordCount + 1
    Me.previous.SetFocus
    Me.save.Visible = False
End Sub

*

1

1 Answers

1
votes

Instead of closing and reopening the form in "edit" mode, you could just set the properties of the fields you want to be able to change, i.e. .locked = false and/or .enabled = true directly in the OnClick event of your "edit" button.

In the OnClick event of your "save" button you do it just the other way round.

For example:

Private Sub EditButton_Click()
    Field1.Enabled = True
End Sub

Private Sub SaveButton_Click()
    Field1.Enabled = False
End Sub

Now, when you click the edit button, your field1 will be enabled for input and after clicking your save button, your field1 will be disabled (i.e. greyed and not allowing any input or focus setting) again.

If you prefer to just "lock" the fields (i.e. let them have the focus and don't grey them but prevent any input), just use the .Locked field property.