Checking for a duplicated customername in access forms!

M

mf_sina

Hi all!

How can i prompt user with a messagebox that the customername entered is
duplicated?

Textbox name: txt_customer_name
Form name: Frm_Customers

Best wishes
 
A

Allen Browne

You can use the AfterUpdate event of the text box to notify the user if
there is another record with the same name.

This example assumes a table named Table1, with a primary key field named
ID:

Private Sub txt_customer_name_AfterUpdate()
Dim strWhere As String
Dim varResult As Variant

With Me.txt_customer_name
If IsNull(.Value) Or (.Value = .OldValue) Then
'do nothing
Else
strWhere = "[customername] = """ & .Value & """"
varResult = DLookup("ID", "Table1", strWhere)
If Not IsNull(varResult) Then
MsgBox "Record " & varResult & " has the same name."
End If
End If
End With
End Sub
 
Top