Command Button Runtime error

G

Guest

I have a form called fmhostrec which has a command button to allow edits.
The name of the command button is cmdlock which has the on click event below.

Private Sub cmdlock_Click()
Dim bLock As Boolean
DoCmd.GoToControl "HostRecName"
If Me.Dirty Then
Me.Dirty = False (RUNTIME ERROR)
End If
bLock = Not Me.AllowAdditions
Me.AllowAdditions = bLock
Me.AllowDeletions = bLock
Me.AllowEdits = bLock
Me.cmdlock.Caption = IIf(bLock, "&Lock", "Un&Lock")
End Sub

I also have on the form the following event

Private Sub Form_BeforeUpdate(Cancel As Integer)
If MsgBox("Save changes?", vbOKCancel) <> vbOK Then
Cancel = True
Me.Undo
MsgBox "Changes were not saved."
End If
End Sub

Everything works fine on this event except on thing. When I click the
cmdlock command button and then make changes, and then do not save the
changes I get a runtime error.

See above where runtime error points to. I got this code from looking at
previous messages from this board. I understand the Beforeupdate event. I
do not understand part of the cmdlock_Click() event so it is making it
difficult for me to troubleshoot. The bit I do not understand is block = not
Me.AllowAdditions.

Any help would be appreciated to debug this runtime error.

Thanks

Ronnie
 
G

Graham Mandeno

Hi Ronnie

When you attempt to save the current record (Me.Dirty = False) the form's
BeforeUpdate event fires. If you set Cancel = True in the Form_BeforeUpdate
procedure, then the save operation cannot complete and so it returns an
error.

You can either (a) trap the error and ignore it if it has a particular error
code (2011 I think) or (b) since you are undoing the record anyway, don't
cancel the update (in other words, delete the Cancel = True line).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top