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

Recommended Answers

All 2 Replies

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

Something like that or also you can check rst.recordcount>0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.