+ The before insert is a form level event:
- Private Sub
Form
_BeforeInsert(Cancel As Integer)
+ Just thought of a snag here, if the primary key is an autonumber field, then the Before_Insert event will
trigger before we get the data entered into the control.
- The autonumber field is going to increment as soon as the insert event succeeds - and will trigger the event before you have a chance to enter anything into the other fields. AFAIK, there's no way to stop this outside of the form_before_insert event - and that's the snag.
+ Let's go back to your code in the control's before_update event. You have about 95% of it done.
- Private Sub UNITID_BeforeUpdate(Cancel As Integer)
-
Dim zStr As String
-
On Error GoTo zerror
-
zStr = "UNITID='" & Me!UNITID & "'"
-
'
-
If (DLookup("UNITID", "BARCODESUB", zStr) & "") = "" Then
-
MsgBox "This Serial# does not exist in proir production manifest!"
-
Me.Undo
-
Cancel = True
-
End If
-
zerrrtrn
-
Exit Sub
-
zerror:
-
MsgBox Prompt:="ErrS: " & Err.Source & vbCrLf & "ErrN: " & Err.Number & vbCrLf & "ErrD: " & Err.Description
-
Resume zerrrtrn
-
End Sub
This should stop any record inserts for the missing serial number. The above is a melding of code I already use in a production database (I check to see if the item has already been entered into the tracking database) and your code. This stops the insert; however, keep in mind that the autonumber
will increment and is one reason why the autonumber shouldn't be used for anything meaningful outside of being a unique record identifier.
I also suggest that if you haven't done so, you set the
option explicit
as shown in the
trouble shooting section of
> Before Posting (VBA or SQL) Code:
<alt><F11> to open the VBA editor...
Menu>Tools>Options>
>Editor tab, UNCHECK "auto Syntax Check" you're vba code will still highlight if in error, just no annoying message boxes while you type!
>> Check mark the "Require Variable Declaration" -
you will need to explictly declare variables used in the code.
*Everything else on this tab check mark*, I prefer the tab step to be either 2 or 4 spaces.
>The other tabs' default values are fine for most people.
This will help you with trouble shooting in the future :)