Cancel Insert

  • Thread starter Thread starter Jim Shaw
  • Start date Start date
J

Jim Shaw

BlankContext: Access 2002

In the "Before Insert" event logic, I'm checking the validity of the record
being added. I.E., does it conform to the business rules of the
application. If the record fails the validity check, I'd like to prevent
the record from being added. I've tried the "cancel = true" statement, but
the record is still being added. How do I do this?

Thanks
Jim
 
The BeforeInsert event fires before there is any data in the form, so you
cannot use that to validate the new record.

Use the BeforeUpdate event procedure of the form. If you have some
conditions that apply only for a new insert, then test the NewRecord
property of the form:

Private Sub Form_BeforeUpdate(Cancel As Integer)
If Me.NewRecord Then
'...
End If
End Sub
 
The BeforeInsert event for the form fires as soon as the user makes the very
first entry into the record. To do what you're trying to do, try the form's
BeforeUpdate event instead. If you only want to make the check for new
records, you can wrap the check in an if statement.

Example:
If Me.NewRecord Then
'do the checking here
End If

If you want to check for edited or new records, you don't need the If
statement, just the checks.
 
Back
Top