Results 1 to 16 of 16

Thread: Catch SqlServer Errors in VB6

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Question Catch SqlServer Errors in VB6

    Hey there, vb6 automatically open a message box when executing a Store Procedure (recordset.open etc..) but how i catch that error and create a custom one because i cant catch the error with the err.number(-2147217873).

    I'm currently using this method to catch unique Key constraint
    Code:
            set rsC = rs.clone
            rsC.Requery
            rsC.Filter = "CDMId =" & cboCDMID & " And " & "InsId =" & cboInsID
    
            If rsC.RecordCount > 0 Then
            Msg "CDMID '" & cboCDMID & "', InsID '" & cboInsID & "'  is already registered", ""
                cboCDMID.SetFocus
                Exit Sub
                Else
                    rsN.Open nSelect, CnString, adOpenDynamic, adLockOptimistic
                    FColorW
                    cboCDMID.SetFocus                
           End If
    The problem is that the requery is slow with large data.

    How to catch in VB6 an create a custom/friendly Msbox error from a SqlServer/Store Procedure? Thanks

  2. #2
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Catch SqlServer Errors in VB6

    I think you need to try and catch it in the stored procedure not VB. Take a look at this link. About three pages down it's catching a constraint error.

    http://www.simple-talk.com/sql/t-sql...ing-workbench/

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Catch SqlServer Errors in VB6

    Does "i cant catch the error" mean that an error handler doesn't help, or that you don't know how to make one?

    If you don't know how, see the article about it in the "Dealing with Errors" section of our Classic VB FAQs (in the FAQ forum, which is shown near the top of our home page)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    Quote Originally Posted by TysonLPrice
    I think you need to try and catch it in the stored procedure not VB. Take a look at this link. About three pages down it's catching a constraint error.

    http://www.simple-talk.com/sql/t-sql...ing-workbench/

    I have a begin transaction, try/catch in the store procedure but when there is a
    contrainst etc.. only show sysytem message not a custom one.

  5. #5
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Catch SqlServer Errors in VB6

    Quote Originally Posted by CVDpr
    I have a begin transaction, try/catch in the store procedure but when there is a
    contrainst etc.. only show sysytem message not a custom one.
    Try looking at the SQL RaiseError statement...

    http://weblogs.asp.net/guys/archive/...07/226399.aspx

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    Now i can catch the constraint with this
    Code:
    On Error GoTo errHandler
    
    errHandler:
    If adRecIntegrityViolation Or adErrIntegrityViolation Then
            Msg "Ya existe un record similar", ""
            Exit Sub
    End If
    is this is ok or it just luck? is both of this are the same thing? "adRecIntegrityViolation Or adErrIntegrityViolation "

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Catch SqlServer Errors in VB6

    That's just luck I'm afraid - you aren't checking the error code at all (what you are accidentally doing there is actually a Bit manipulation and conversion to boolean, which will always be true).

    It should be like this:
    Code:
    If Err.Number = adRecIntegrityViolation Or Err.Number = adErrIntegrityViolation Then

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    Quote Originally Posted by si_the_geek
    It should be like this:
    Code:
    If Err.Number = adRecIntegrityViolation Or Err.Number = adErrIntegrityViolation Then
    Now it doesnot work. is till showing this message

    Violation of UNIQUE KEY constraint 'IX_AppInformationOfBillingByInsuranceAndServiceArea_1'. Cannot insert duplicate key in object 'AppInformationOfBillingByInsuranceAndServiceArea'.

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Catch SqlServer Errors in VB6

    Can you show us a screenshot of the error?

    (you can upload it here, by going to "Post Reply" then "Manage Attachments")

  10. #10
    Wall Poster TysonLPrice's Avatar
    Join Date
    Sep 2002
    Location
    Columbus, Ohio
    Posts
    3,969

    Re: Catch SqlServer Errors in VB6

    Have you considered the business logic? I don't know what your app is doing but it seems you should be able to handle not allowing duplicates. Why would someone be picking the DB keys?

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    ok..
    Attached Images Attached Images  
    Last edited by CVDpr; Sep 10th, 2008 at 10:22 AM.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    Quote Originally Posted by TysonLPrice
    Have you considered the business logic? I don't know what your app is doing but it seems you should be able to handle not allowing duplicates. Why would someone be picking the DB keys?
    how to check for duplicate without picking the db?

  13. #13
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Catch SqlServer Errors in VB6

    That looks like an error within your application, but being handled by a different part of the program, because your current error handler doesn't deal with it (so the error gets raised to the caller).

    I should have thought of this before:
    Code:
    errHandler:
    If Err.Number = adRecIntegrityViolation Or Err.Number = adErrIntegrityViolation Then
            Msg "Ya existe un record similar", ""
    Else
            Msg "Error " & Err.Number & ": " & Err.Description
    End If
    Once you know the actual error number, you can use that in the If statement.

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    Quote Originally Posted by si_the_geek
    Once you know the actual error number, you can use that in the If statement.
    The error number is -2147217873
    i cant use this number, doesnot work and vb6 always show this number when dont know a vb number

  15. #15
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Catch SqlServer Errors in VB6

    What do you mean by that?

    I have used numbers similar to that in my error handlers lots of times (eg: If Err.Number = -2147217873 Then ), and it has always acted correctly.

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Jun 2008
    Posts
    111

    Re: Catch SqlServer Errors in VB6

    done thanks.
    Last edited by CVDpr; Sep 10th, 2008 at 11:12 AM.

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