Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ADO Error Messages when using sqloledb (SQL Server 7.0)

Posted on 2000-02-16
6
Medium Priority
?
456 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 6

Expert Comment

by:Marine
ID: 2528818
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
ID: 2529073
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
ID: 2529139
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jadekraf
ID: 2529300
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 70

Accepted Solution

by:
Éric Moreau earned 2000 total points
ID: 2529634
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
ID: 2530888
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…
Suggested Courses

721 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