Check for duplicates on loosefocus

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I'm trying to build a form that has a non automatic number as primary key.
However, I would like to set the message of "record cannot be saved" to
activate right after you enter that field, and not needing to wait until you
move to the next record.

Which is the best way to do that?
 
Use the AfterUpdate event of the control to DLookup() the value in the
table, and see if it already exists.

This example assumes a numeric primary key named ID, in Table1:

Private Sub ID_AfterUpdate()
Dim strWhere As String
With Me.ID
If IsNull(.Value) Or (.Value = .OldValue) Then
'do nothing
Else
If Not IsNull(DLookup("ID", "Table1","ID = " & .Value)) Then
MsgBox "Duplicate ID."
End If
End If
End With
End Sub

For further help with DLookup() see:
Getting a value from a table: DLookup()
at:
http://allenbrowne.com/casu-07.html
 
Use DLookup() or DCount() in the BeforeUpdate event of the control. If you have
a duplicate you can Cancel the update and hold the focus in the control.

Hi,

I'm trying to build a form that has a non automatic number as primary key.
However, I would like to set the message of "record cannot be saved" to
activate right after you enter that field, and not needing to wait until you
move to the next record.

Which is the best way to do that?
_______________________________________________
hth - RuralGuy (RG for short)
Please post to the NewsGroup so all may benefit.
 
Great! It works.

Thanks.
--
M


Allen Browne said:
Use the AfterUpdate event of the control to DLookup() the value in the
table, and see if it already exists.

This example assumes a numeric primary key named ID, in Table1:

Private Sub ID_AfterUpdate()
Dim strWhere As String
With Me.ID
If IsNull(.Value) Or (.Value = .OldValue) Then
'do nothing
Else
If Not IsNull(DLookup("ID", "Table1","ID = " & .Value)) Then
MsgBox "Duplicate ID."
End If
End If
End With
End Sub

For further help with DLookup() see:
Getting a value from a table: DLookup()
at:
http://allenbrowne.com/casu-07.html
 
Back
Top