Open form, Edit button, etc

  • Thread starter Thread starter Aniko
  • Start date Start date
A

Aniko

Hi,
When I open a form I would like the record to be locked, so
users cannot accidentally amend any fields of the record. I
would like to add a button called Edit, which would make
the record editable and the Save button, which could be
used to save the record only if the user is happy with it.
(I did find the Save button, but not sure how to stop the
Access saving automatically.)
Could someone please help me to solve this problem?

Thank you,
Aniko
 
1. Set the AllowAdditions property of your form to No.
You probably want to set AllowDeletions to No as well.

2. Create a command button with these properties:
Name cmdLock
Caption Un&Lock
On Click [Event Procedure]

3. Click the Build button (...) beside the On Click property.
Access opens the code window.
Set up the event procedure like this:

Private Sub cmdLock_Click()
Dim bLock as Boolean

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If
bLock = Not Me.AllowAdditions
Me.AllowAdditions = bLock
Me.AllowDeletions = bLock
Me.cmdLock.Caption = IIf(bLock, "Un&Lock", "&Lock")
End Sub

The command button toggles the locking/unlocking of the form.
 
you can add the following code to the form's Current event, as

Private Sub Form_Current()

Me.AllowEdits = Me.NewRecord

End Sub

each time you move from one record to another, the record will be locked -
unless it is a new record. so the user can add a new record without
interference, but is stopped from editing an existing record. Note: the
user can also delete an existing record entirely, without interference.

add a command button for editing and add the following code. i called the
command button cmdEdit, and put the code on the Click event, as

Private Sub cmdEdit_Click()

Me.AllowEdits = True

End Sub

to stop automatic saving of an edited record, add the following code to the
form's BeforeUpdate event, as

Private Sub Form_BeforeUpdate(Cancel As Integer)

If Me.Dirty And Not Me.NewRecord Then
If MsgBox("Do you want to save your changes?", _
vbOKCancel + vbDefaultButton2) = vbCancel Then
Cancel = True
Me.Undo
MsgBox "Changes were not saved."
End If
End If

End Sub

if the record is a new record, it will save automatically. if an existing
record has been changed, the "Do you want to save?" message box pops up. if
the user clicks the OK button (default button is set to Cancel), then the
record is saved and nothing else happens. if the user clicks the Cancel
button, the changes are "undone" and the "changes not saved" message box
pops up.
if the user changes a record and then closes the form before those changes
have been saved or discarded, and chooses to Undo the changes, an error will
be generated. to handle the error, add the following code to the form's
OnError event, as

Private Sub Form_Error(DataErr As Integer, Response As Integer)

If DataErr = 2169 Then
Response = acDataErrContinue
End If

End Sub

suggest you read up on the various form events and properties that are used
in the above code, so you'll understand how they work.

hth
 
Thank you Allen,
I also set the AllowEdits to NO and added an extra line to
the Event Procedure Me.AllowEdits = bLock.

Could you please also help me with the savind issue?
How do I stop Access autosaving the records?
How do I stop and warn the user when clicking on any of the
navigation buttons to save the record first.
Regards,
Aniko
-----Original Message-----
1. Set the AllowAdditions property of your form to No.
You probably want to set AllowDeletions to No as well.

2. Create a command button with these properties:
Name cmdLock
Caption Un&Lock
On Click [Event Procedure]

3. Click the Build button (...) beside the On Click property.
Access opens the code window.
Set up the event procedure like this:

Private Sub cmdLock_Click()
Dim bLock as Boolean

If Me.Dirty Then 'Save any edits.
Me.Dirty = False
End If
bLock = Not Me.AllowAdditions
Me.AllowAdditions = bLock
Me.AllowDeletions = bLock
Me.cmdLock.Caption = IIf(bLock, "Un&Lock", "&Lock")
End Sub

The command button toggles the locking/unlocking of the form.

--
Allen Browne - Microsoft MVP. Perth, Western Australia.

Reply to group, rather than allenbrowne at mvps dot org.

Aniko said:
When I open a form I would like the record to be locked, so
users cannot accidentally amend any fields of the record. I
would like to add a button called Edit, which would make
the record editable and the Save button, which could be
used to save the record only if the user is happy with it.
(I did find the Save button, but not sure how to stop the
Access saving automatically.)
Could someone please help me to solve this problem?

Thank you,
Aniko


.
 
Use the BeforeUpdate event of the Form. That's the *only* way to catch all
the various ways when a record may be saved, e.g. moving to another record,
or closing a form, or requerying, or filtering, or reordering, or ...

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save changes?", vbOkCancel) <> vbOk Then
Cancel = True
'Me.Undo
End If
End Sub
 
Hi Tina,

I have implemented all your suggestions, but I just
realised that some part of the form is not editable.

I have a main form and on it option buttons to select the
corresponding tab controls. The tab controls hold sub
forms. And none of them are able to be amended.

Any help would be much appreciated.

Kind regards,
Aniko
 
are you able to add subform records when adding a *new* mainform record?
and are you able to edit subform records after up clicking the Edit button?
were you able to edit subform records in new and/or existing mainform
records before adding the code i posted?
 
Hi,
I went back to a copy of the database before the codes were
added and yes I am able to edit the sub-form of existing
records.
However, I have an error message when adding a new record,
which was rectified by Dirk, that I should have a default
value for the option group 1+ the number of tabs
corresponding to the option group.

Now I am unable to edit existing record's sub-forms after
clicking the Edit button, and unable to enter anything to
the sub-form when adding new records, as the sub-form is no
longer visible in a new record.

Thanks for your help,
Aniko
 
well, the code i gave you was pretty limited really, shouldn't have had such
a massive effect. if you want me to take a look at your db (the "after code
was added" copy), post back with your email address (beware the spammers)
and i'll email you.
 
Hi Tina,
I rolled back to an earlier version of the database and
realised that the Edit button only activates the main form
fields, but not the subforms'. In other words, when opening
the main form the record is locked for editing but only the
main form part. The subform part can still be edited.
Do I need to add your codes to all the subforms as well?
And how am I to change the Edit button onCLick event to
unlock the subforms as well as the main form?

Thanks for your time,
Aniko
 
with the additional info you've posted, i'll go ahead and test out a
solution on the db you sent me. i'll email you, and post the answer here
too, if possible.
 
no, don't add the Current event code, etc, to the subform(s). the easiest
way that i can think of to keep the subforms "in sync" with the main form re
controlling editing, is to put the following code in the main form's subform
control Enter event, as

Private Sub MySubformControl_Enter()

Me!MySubformControl.Form.AllowEdits = Me.AllowEdits

End Sub

each time you enter the subform from the main form, AllowEdits in the
subform will be set to match the current state already in effect in the main
form.
see the email i sent you re other issues.

hth
 
Back
Top