I tried the following
serial_lostfocus
txtjunk.SetFocus
txtjunk.Value = DLookup("[random]", "Assignment Table",
"form![txtserial] = form![txtserial]")
As soon as I enter the second serial number, it gives my the first
cases random value, no matter if the serial is a duplicate or not.
Well, a couple of issues here.
I'd STRONGLY suggest using Serial's BeforeUpdate event instead of
LostFocus. BeforeUpdate fires only when the user changes the value in
the control, and it can be cancelled; LostFocus fires whenever they
move out of the control, even if they're just tabbing through it.
Secondly, your search criterion will always be TRUE since it's
comparing the value in the txtserial textbox on the Form to the value
in the txtserial textbox on the Form... it's comparing the control to
itself, so it will naturally always be equal! Instead, compare the
value in the Table to the value in the textbox.
If you just want to warn the user if there's a duplicate, try code
like this. I'm assuming that the field [Serial] is bound to a textbox
named [txtserial], and that it's a Text field (leave off the ' and "'"
if it's numeric).
Private Sub txtserial_BeforeUpdate(Cancel as Integer)
If Me!Serial & "" = "" Then Exit Sub ' ignore blanks
If Not IsNull(DLookUp("[Serial]", "[Assignment Table]", _
"[Assignment Table].[Serial] = '" & Me!txtSerial & "'') Then
MsgBox "This serial number already exists!", vbOkOnly
<do something appropriate>
End If
End Sub