Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

[b]Cancel Current Record on close if no PK[/b]

Status
Not open for further replies.

MikeMcKeown

Programmer
Apr 1, 2003
69
GB
I have a problem preventing the same error message appearing twice.

If you try to enter a null primary key and then close the form, you get error 515. Which I have successfully trapped in the below code.

If dataerr = 515 Then
If MsgBox("Are you sure you want to exit and not save the current record as there is no PK" vbOKCancel) = vbOK Then
response = acDataErrContinue
Cancel = False

Else
MsgBox ("CANCEL")
Cancel = True
End If
End If

This works fine - however when they have selected Ok not to save it they get an Access Warning. Saying that the record cannot be saved - click Yes to close it and no to return to the form.

Bascially the user has to enter the same information twice. Is there anyway to prevent this message from appearing.

I have tried:-

DoCmd.SetWarning False
Me.Undo

In the code above however the message still appears, is there a way of preventing appearing?
 
Why not do this ... put this code in the form's Before Update event ...

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
    If IsNull(Me.RecordID) Then
        MsgBox "Primary Key Missing", vbExclamation, "Demo Database"
        DoCmd.CancelEvent
        Me.RecordID.SetFocus
    End If
End Sub

In this example, RecordID is the primary key field. The code:

-- detects a null key field
-- Displays the warning message of your choice
-- cancels the update (save) event
-- moves the focus back to the RecordID field, so the user can complete this.

Note: the 'way out' for a user who doesn't want to save is now to press [Esc] to cancel the changes, then close the form.

I hope that thsi idea is useful.


Bob Stubbs
 
Using the forms on error event isn't validation, it is just a mean to for instance substitute the default message boxes with you own, or for instance drop of a message completely. For proper validation, using the before update event, as mentioned by BobStubbs is probably the best.

To just drop of this message, I think this is DataErr 2169, try just adding this to your on error code

[tt]if dataerr=2169 then
response=acdataerrcontinue
end if[/tt]

Roy-Vidar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top