Solved

ADO Error Messages when using sqloledb (SQL Server 7.0)

Posted on 2000-02-16
6
454 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

617 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