Solved

ADO Error Messages when using sqloledb (SQL Server 7.0)

Posted on 2000-02-16
6
451 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
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
Independent Software Vendors: 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 500 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA filters 2 70
Excel VBA, find a string in a column, update a cell 7 102
Hide vba in gp 7 113
checkbox to hide entire section 10 58
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

685 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