Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Duplicate Entry

Status
Not open for further replies.

quaked2023

Technical User
Jan 22, 2004
105
MX
hi!:

I have a question for you, how do you prevent a duplicate entry in a database?, in my aplication i request the user to put a code for every client that he checks in... the code has to be unique so in my access database i indexed with no duplicates the field code, an in my vb aplication i put this code:

Code:
Data1.Recordset.MoveFirst
Do While Data1.Recordset("Code") <> Val(Text1.Text)
    If Data1.Recordset("Code") = Val(Text1.Text) Then
        X = MsgBox("The code has to be unique change it", vbCritical, "Error:")
        Text1.SetFocus
        Exit Sub
    Else
        Data1.Recordset.MoveNext
    End If
Loop

But my code doesn't work i get Run-time Error '3022' that says that the changes i requested will create a duplicate value in the index..... Can you tell me if there is an other way to prevent the user from duplicating an entry?

Thanks in advance

-The Only Privacy Left Is The Inside Of Your Head!- QUAKED2023
 
The Do/While loops exits as soon as it finds an equal value, it doesnot do the check. My example also also handles the case when the RS has no rows, movefirst would give an error.
Code:
If Not (Data1.Recordset.BOF And Data1.Recordset.EOF) Then
    Data1.Recordset.MoveFirst
    Do Until Data1.Recordset.EOF
        If Data1.Recordset("Code") = Val(Text1.Text) Then
            X = MsgBox("The code has to be unique change it", vbCritical, "Error:")
            Text1.SetFocus
            Exit Sub
        Else
            Data1.Recordset.MoveNext
        End If
    Loop
End If
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top