Results 1 to 15 of 15

Thread: Error trapping

  1. #1

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Error trapping

    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

    Thanks in advance.......

  2. #2
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Error trapping

    Before you add it, use a SELECT statement to search for what you are about to add. If it's found, then shout away.

  3. #3
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Error trapping

    Here is a sample code:
    VB Code:
    1. Private Sub cmdCheckDuplicateUser_Click()
    2.     Dim rs As adodb.Recordset
    3.    
    4.     '--Replace with appropriate field names and table name --
    5.     Set rs = cnn.Execute("SELECT * FROM TableName WHERE UserName='" & txtUserName & "' AND dDate=#" & txtDate & "#")
    6.     If Not rs.EOF Then
    7.         MsgBox "User already exists!", vbExclamation
    8.     Else
    9.         'Unique user
    10.         rs.AddNew
    11.         rs!UserName = txtUserName
    12.         rs!dDate = txtDate
    13.         rs.Update
    14.     End If
    15.     rs.Close
    16.     Set rs = Nothing
    17. 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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  4. #4

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Re: Error trapping

    I'm still using the antique DAO code.....

    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....

  5. #5
    Fanatic Member
    Join Date
    Sep 2002
    Location
    Lexington, SC
    Posts
    586

    Re: Error trapping

    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.

  6. #6
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Error trapping

    Quote Originally Posted by StevenHickerson
    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  7. #7

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Re: Error trapping

    Quote Originally Posted by dglienna
    Before you add it, use a SELECT statement to search for what you are about to add. If it's found, then shout away.
    David, how do you suggest I do this???

  8. #8
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Error trapping

    Quote Originally Posted by stealth black
    David, how do you suggest I do this???
    check pradeep's post #3

  9. #9

    Thread Starter
    Registered User
    Join Date
    Jun 2005
    Posts
    87

    Re: Error trapping

    Quote Originally Posted by kfcSmitty
    check pradeep's post #3
    Isn't that adodb? I'm in DAO

  10. #10
    PowerPoster kfcSmitty's Avatar
    Join Date
    May 2005
    Posts
    2,248

    Re: Error trapping

    yes, but the select statement would be the same, would it not?

  11. #11
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Error trapping

    Quote Originally Posted by Pradeep1210
    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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  12. #12
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Error trapping

    Here's the DAO version:

    VB Code:
    1. Private Sub cmdCheckDuplicateUser_Click()
    2.     Dim rs As Recordset
    3.    
    4.     '--Replace with appropriate field names and table name --
    5.     Set rs = cnn.OpenRecordset("SELECT * FROM TableName WHERE UserName='" & txtUserName & "' AND dDate=#" & txtDate & "#")
    6.     If Not rs.EOF Then
    7.         MsgBox "User already exists!", vbExclamation
    8.     Else
    9.         'Unique user
    10.         rs.AddNew
    11.         rs.Fields("UserName") = txtUserName
    12.         rs.Fields("dDate") = txtDate
    13.         rs.Update
    14.     End If
    15.     rs.Close
    16.     Set rs = Nothing
    17. 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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  13. #13
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Error trapping

    Quote Originally Posted by szlamany
    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

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  14. #14
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Error trapping

    Here is a simple DAO example. They don't check for dups, but show how to find fields.
    Attached Files Attached Files

  15. #15
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Error trapping

    Quote Originally Posted by Pradeep1210
    Won't it raise an error in such a case??
    I guess my post was really directed at StevenHickerson - I just wanted to shed some more light on how BOF and EOF work...

    *** 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".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width