Trying to prevent duplicates from being entered into database.... How do I have the app search the db for a username AND date match, so I can yell at the user through an error message? LOL
'--Replace with appropriate field names and table name --
Set rs = cnn.Execute("SELECT * FROM TableName WHERE UserName='" & txtUserName & "' AND dDate=#" & txtDate & "#")
If Not rs.EOF Then
MsgBox "User already exists!", vbExclamation
Else
'Unique user
rs.AddNew
rs!UserName = txtUserName
rs!dDate = txtDate
rs.Update
End If
rs.Close
Set rs = Nothing
End Sub
Pradeep
Last edited by Pradeep1210; Jul 18th, 2005 at 12:46 PM.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
What I meant was that I would like the error to pop up when the username and date combination matches the same combo in the db. There is a set number of reps, and I got the app automatically pulling their name from the machine. There should never be two entries with the same date for a given user....
Pradeep are you sure that code works correctly? I recently experienced some issues with my database code where the EOF was true when nothing was returned in the recordset. In fact I had to trap the situation by testing if both BOF and EOF were true at the same time, if they were that meant the recordset was empty.
I may have missed something as I'm still rather new with databases and recordsets, but that is the only way I have gotten it to work.
Pradeep are you sure that code works correctly? I recently experienced some issues with my database code where the EOF was true when nothing was returned in the recordset. In fact I had to trap the situation by testing if both BOF and EOF were true at the same time, if they were that meant the recordset was empty.
I may have missed something as I'm still rather new with databases and recordsets, but that is the only way I have gotten it to work.
Whenever a recordset is opened, it is on the first record by default. So when you have just opened a recordset (and haven't moved yet) and you get the EOF=True, it mean's that there are no records in the recordset. Checking for BOF is useless in such a case.
EDIT: Ohh.. sorry. I have edited the code. There was a typo and it was doing the opposite. (I missed the NOT !)
Pradeep
Last edited by Pradeep1210; Jul 18th, 2005 at 12:48 PM.
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Whenever a recordset is opened, it is on the first record by default. So when you have just opened a recordset (and haven't moved yet) and you get the EOF=True, it mean's that there are no records in the recordset. Checking for BOF is useless in such a case.
Just to add to that - BOF checking is not required after you load a RS from a database - EOF checking is enough. But if there is a chance that the SQL is bad, and no recordset is returned (as opposed to an empty recordset), then you should check the .STATE of the RECORDSET first.
*** Read the sticky in the DB forum about how to get your question answered quickly!! ***
Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".
'--Replace with appropriate field names and table name --
Set rs = cnn.OpenRecordset("SELECT * FROM TableName WHERE UserName='" & txtUserName & "' AND dDate=#" & txtDate & "#")
If Not rs.EOF Then
MsgBox "User already exists!", vbExclamation
Else
'Unique user
rs.AddNew
rs.Fields("UserName") = txtUserName
rs.Fields("dDate") = txtDate
rs.Update
End If
rs.Close
Set rs = Nothing
End Sub
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering
Just to add to that - BOF checking is not required after you load a RS from a database - EOF checking is enough. But if there is a chance that the SQL is bad, and no recordset is returned (as opposed to an empty recordset), then you should check the .STATE of the RECORDSET first.
Won't it raise an error in such a case??
Pradeep, Microsoft MVP (Visual Basic) Please appreciate posts that have helped you by clicking icon on the left of the post.
"A problem well stated is a problem half solved." — Charles F. Kettering