0

Hi,
I would like to perform a validation check on a field (Employee/Staff Number) there should not be duplicate entries in this field so i would like for a check to be performed when the next field is clicked then a msg displayed saying "Staff Number already exists"
Any sample code would be appreciated

Regards

3
Contributors
2
Replies
3
Views
8 Years
Discussion Span
Last Post by omoridi
0

Hi RassKass,

What type of Database you are using? If you are using MS Access as your database, then here is my simple help.

Private Sub txtEmployeeNumber_LostFocus()
Dim CON As New Connection
Dim CMD As ADODB.Command
Dim RST As New Recordset
       
    Dim conString As String
    
        conString = "Provider=Microsoft.Jet.OLEDB.4.0;" _
                    & "Data Source=" _
                    & App.Path & "\Database.mdb"
    Set CON = New ADODB.Connection
    With CON
        .ConnectionString = conString
        .Open
    End With

Dim strSQL As String
    
    strSQL = "SELECT * FROM tblEmployee WHERE EmployeeNumber LIKE '" & txtEmployeeNumber.Text & "'"
    RST.Open strSQL, CON, adOpenForwardOnly, adLockOptimistic
If RST.EOF = False Then
'Add your Codes here if your database does not contain that Employee Number.
Else
MsgBox "Staff/Employee Number already exists",vbOKOnly,"Duplicate Number"
txtEmployeeNumber = ""
txtEmployeeNumber.SetFocus
End If
End Sub

Hope This Helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.