W
Walter Bennett
Hello All!!!
Im using an unbound form to add records to a table using
recordsets in VB...
I want to learn how to check for duplicate entries using
code before I add the recordset...
More specifically, I want to check the data entered into
the "drvssn" field of the unbound form against the data
stored in the table to make sure the data being entered
into the unbound form will not generate a duplicate
entry...
The name of the unbound form im using to validate and
enter data into the "tblDrivers" table is: "frmDrivers"
Using DCount was suggested, but when I add this code, I
get a runtime error 3420 Object Invalid or no longer set.
The debugger references my line of code:
rctDriverAdd.AddNew
When I remove the code for 9.) Validate for duplicate SSN
using DCount*** everything works OK... NO error...
Any ideas anyone?? Please feel free to modify my code as
you see fit...
Thx., & Rgds.,
Walter
My code reads as follows:
Private Sub Command29_Click()
Dim db As Database
Dim rctDriverAdd As Recordset
'*** Create a pointer to the database and a pointer to
the table ***
Set db = CurrentDb()
Set rctDriverAdd = db.OpenRecordset("tblDrivers",
dbOpenTable)
' ***Validation Sequence***
'8.) Validate SSN
If IsNull(drvssn) Or Len(drvssn) = 0 Then
MsgBox "Please enter the S.S.N."
drvssn.SetFocus
rctDriverAdd.Close
db.Close
Exit Sub
End If
'9.) Validate for duplicate SSN using DCount***
If DCount("*", "tbldrivers", "drvssn = """ &
Me.drvssn & """") > 0 Then
' Duplicate record found***
MsgBox "The S.S.N. you have entered is in already used"
drvssn.SetFocus
rctDriverAdd.Close
db.Close
Else
' Do nothing, record is not a duplicate***
Exit Sub
End If
rctDriverAdd.AddNew
rctDriverAdd!drvssn = drvssn
DoCmd.Close
rctDriverAdd.Close
db.Close
End Sub
Im using an unbound form to add records to a table using
recordsets in VB...
I want to learn how to check for duplicate entries using
code before I add the recordset...
More specifically, I want to check the data entered into
the "drvssn" field of the unbound form against the data
stored in the table to make sure the data being entered
into the unbound form will not generate a duplicate
entry...
The name of the unbound form im using to validate and
enter data into the "tblDrivers" table is: "frmDrivers"
Using DCount was suggested, but when I add this code, I
get a runtime error 3420 Object Invalid or no longer set.
The debugger references my line of code:
rctDriverAdd.AddNew
When I remove the code for 9.) Validate for duplicate SSN
using DCount*** everything works OK... NO error...
Any ideas anyone?? Please feel free to modify my code as
you see fit...
Thx., & Rgds.,
Walter
My code reads as follows:
Private Sub Command29_Click()
Dim db As Database
Dim rctDriverAdd As Recordset
'*** Create a pointer to the database and a pointer to
the table ***
Set db = CurrentDb()
Set rctDriverAdd = db.OpenRecordset("tblDrivers",
dbOpenTable)
' ***Validation Sequence***
'8.) Validate SSN
If IsNull(drvssn) Or Len(drvssn) = 0 Then
MsgBox "Please enter the S.S.N."
drvssn.SetFocus
rctDriverAdd.Close
db.Close
Exit Sub
End If
'9.) Validate for duplicate SSN using DCount***
If DCount("*", "tbldrivers", "drvssn = """ &
Me.drvssn & """") > 0 Then
' Duplicate record found***
MsgBox "The S.S.N. you have entered is in already used"
drvssn.SetFocus
rctDriverAdd.Close
db.Close
Else
' Do nothing, record is not a duplicate***
Exit Sub
End If
rctDriverAdd.AddNew
rctDriverAdd!drvssn = drvssn
DoCmd.Close
rctDriverAdd.Close
db.Close
End Sub