Solved

ADO Error Messages when using sqloledb (SQL Server 7.0)

Posted on 2000-02-16
6
448 Views
Last Modified: 2013-12-25
Why can't I access the SQL error messages in the connection error object.  It always returns "Errors Occured" and the Native Error = 0.  I can't imagine that this is supposed to work this way being that all the products are Microsoft products.

I am using ADO2.1, visual basic 6.0 sp3, and SQL Server 7.0 sp1.

The code for a test table is
CREATE TABLE Test(
      Test_Identity       int IDENTITY(1,1) NOT NULL,
      Test_Value      tinyint      NOT NULL

      CONSTRAINT Test_PKC PRIMARY KEY CLUSTERED
            (Test_Value),

      CONSTRAINT Test_U1 UNIQUE NONCLUSTERED
            (Test_Identity)
)
GO      

This is the code in vb.
Private Sub Command1_Click()


    Dim cn              As ADODB.Connection
    Dim rs              As ADODB.Recordset
    Dim sSQLCommand     As String
    Dim sConnectionString   As String
    Dim I               As Integer
   
    Set cn = New ADODB.Connection
    Set rs = New ADODB.Recordset

    On Error GoTo ErrorHandler
   
    sConnectionString = "Provider=sqloledb;" _
                        & "Server=jadekra_office;" _
                        & "UID=IntelliLog_VB;" _
                        & "PWD=vb$app;" _
                        & "Database=IntelliLog;" _
                        & "App=Test;"
   
    cn.ConnectionString = sConnectionString
    cn.Open
   
    sSQLCommand = ""
    sSQLCommand = sSQLCommand & "SELECT * "
    sSQLCommand = sSQLCommand & " FROM Test"
   
    rs.Open sSQLCommand, cn, adOpenKeyset, adLockOptimistic, adCmdText

    ' Add a record
    rs.AddNew
    rs.Fields("Test_Value").Value = 1
    rs.Update
   
    ' Try to Add a duplicate record to generate the error
    rs.AddNew
    rs.Fields("Test_Value").Value = 1
    rs.Update
   
   
    If rs.State = adStateOpen Then
        rs.Close
    End If

CleanExit:

    Set rs = Nothing
    Set cn = Nothing


Exit Sub
   
ErrorHandler:
        MsgBox Err.Description
        For I = 0 To cn.Errors.Count - 1
            MsgBox cn.Errors(I).Description
            MsgBox cn.Errors(I).NativeError
        Next
        cn.Errors.Clear
        Resume CleanExit

End Sub

I expect to see the error Violation of Primary key constraint.  
0
Comment
Question by:jadekraf
6 Comments
 
LVL 6

Expert Comment

by:Marine
Comment Utility
Your connection string doesn't look right to me.
sConnectionString = "Provider=sqloledb;" _                  & "Server=jadekra_office;" _                       & "UID=IntelliLog_VB;" _              & "PWD=vb$app;" _
& "Database=IntelliLog;" _
& "App=Test;"
     
SEe where you have the semilcolmnut? Try placing your samicolumn outsite the quotes. I know this may not be related to your primary constrait vaiolation problem but htats how i do my my connection strings.    
0
 
LVL 2

Expert Comment

by:p_biggelaar
Comment Utility
Just to make sure: you tested your code and everything works fine indeed up to the moment where you want to add the new key field, containing the duplicate value?
0
 
LVL 2

Expert Comment

by:p_biggelaar
Comment Utility
Oh and I agree with Marine that there might be something wrong with your connection string. Try to get something like this (By the way: I don't know why you specify the application?):

Provider=SQLOLEDB.1;Persist Security Info=False;User ID=IntelliLog_VB;Password=vb$app;Initial Catalog=IntelliLog;Data Source=jadekra_office
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:jadekraf
Comment Utility
There is nothing wrong with the connection string.  I tested the procedure before I posted it to make sure it would work.  If you bothered to test it first instead of guessing at the problem you would see that it works.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
Set your CursorLocation to the Client instead of Server (default value). You can set it this way:

rs.CursorLocation = adUseClient
rs.Open sSQLCommand, cn, adOpenKeyset, adLockOptimistic, adCmdText

Using this, you will obtain this error: -2147217900:Violation of PRIMARY KEY constraint 'Test_PKC'. Cannot insert duplicate key in object 'Test'.

Instead of the infamous:
-2147217887:Errors occurred
0
 

Author Comment

by:jadekraf
Comment Utility
You da man.  I've searched everywhere but here for the answer to that one.  If it was in help I couldn't find it.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now