Solved

Visual Basic Error Opening 2nd Recordset on ADODB Connection in Microsoft Access

Posted on 2008-06-11
2
339 Views
Last Modified: 2008-06-11
I am trying to open 2 recordsets on the same ADODB connection in Visual Basic code behind a Microsoft access form.  The access database has tables linked to a SQL Server database.  The first recordset opens correctly, however, I get an ODBC connection failed error message (Run-time error '-2147467259 (80004005)') on the second recordset and I'm not sure why.  Code is attached.  Thanks!
Private Sub btnSave_Click()
    Dim dblocal As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim rstCur As ADODB.Recordset
    Dim stSQL As String
 
    Set dblocal = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    Set rstCur = New ADODB.Recordset
 
 
            stSQL = ""
            stSQL = "SELECT IntSecID FROM tblSecurity WHERE IssuerID = " & txtIssuerID.Value
            rst.Open stSQL, dblocal, adOpenDynamic, adLockOptimistic
            
            If rst.BOF = False Or rst.EOF = False Then
                rst.MoveFirst
            End If
            
            Do While rst.EOF = False
                stSQL = ""
                stSQL = "INSERT INTO tblRatingHistory (StartDate, EndDate, RatingID, RatingSourceID, IntSecID, "
                stSQL = stSQL & "RatingAddedDate, RatingModifiedDate, RatingAddedNTUser, RatingModifiedNTUser) "
                stSQL = stSQL & "VALUES ('" & txtEffDate.Value & "', '9/9/9999', " & cboRating.Value & ", 5, "
                stSQL = stSQL & rst.Fields("IntSecID").Value & ", '" & txtRatingAddedDate.Value & "', '" & txtRatingModifiedDate.Value
                stSQL = stSQL & "', '" & txtRatingAddedNTUser & "', '" & txtRatingModifiedNTUser & "');"
                dblocal.Execute stSQL
                
                '*********************** Update tblRatingCurrent*******************************
                stSQL = ""
                stSQL = "SELECT RatingCurrentID FROM tblRatingCurrent WHERE IntSecID = " & rst.Fields("IntSecID") & " AND RatingTypeID = " & cboRatingType.Value
 
 
 
 
               rstCur.Open stSQL, dblocal, adOpenDynamic, adLockOptimistic    '<---------- Error occurs here
                
                If rstCur.BOF = False Or rstCur.EOF = False Then
                    stSQL = ""
                    stSQL = "UPDATE tblRatingCurrent SET RatingID = " & cboRating.Value & " WHERE RatingCurrentID = " & rstCur.Fields("RatingCurrentID")
                    dblocal.Execute stSQL
                Else
                    stSQL = ""
                    stSQL = "INSERT INTO tblRatingCurrent (IntSecID, RatingTypeID, RatingID) VALUES (" & rst.Fields("IntSecID")
                    stSQL = stSQL & ", " & cboRatingType.Value & ", " & cboRating.Value & ");"
                    dblocal.Execute stSQL
                End If
                
                rstCur.Close
                '******************************************************************************
                
                
                rst.MoveNext
            Loop
            rst.Close

Open in new window

0
Comment
Question by:dskoln
[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
  • 2
2 Comments
 

Author Comment

by:dskoln
ID: 21763164
The same code in the development environment (exact replica of the SQL Server 2000 database, but in SQL Server 2005, same access db and code with access tables linked to dev db instead of the prod db) produces a similar, but slightly different error in the same place.  The error in Dev is:

Run-time error  '-2147467259 (80004005)': Method 'Open' of object '_Recordset' failed
0
 

Accepted Solution

by:
dskoln earned 0 total points
ID: 21764811
found a work around myself that seems to work by associating the 2nd recordset to a different connection variable.

Code is attached below with the changes commented if anyone is interested
Private Sub btnSave_Click()
    Dim dblocal As ADODB.Connection
    Dim dbcur As ADODB.Connection ' <----------- new line
    Dim rst As ADODB.Recordset
    Dim rstCur As ADODB.Recordset
    Dim stSQL As String
 
    Set dblocal = CurrentProject.Connection
    Set rst = New ADODB.Recordset
    Set dbcur = CurrentProject.Connection ' <---------- New line
    Set rstCur = New ADODB.Recordset
 
 
            stSQL = ""
            stSQL = "SELECT IntSecID FROM tblSecurity WHERE IssuerID = " & txtIssuerID.Value
            rst.Open stSQL, dblocal, adOpenDynamic, adLockOptimistic
            
            If rst.BOF = False Or rst.EOF = False Then
                rst.MoveFirst
            End If
            
            Do While rst.EOF = False
                stSQL = ""
                stSQL = "INSERT INTO tblRatingHistory (StartDate, EndDate, RatingID, RatingSourceID, IntSecID, "
                stSQL = stSQL & "RatingAddedDate, RatingModifiedDate, RatingAddedNTUser, RatingModifiedNTUser) "
                stSQL = stSQL & "VALUES ('" & txtEffDate.Value & "', '9/9/9999', " & cboRating.Value & ", 5, "
                stSQL = stSQL & rst.Fields("IntSecID").Value & ", '" & txtRatingAddedDate.Value & "', '" & txtRatingModifiedDate.Value
                stSQL = stSQL & "', '" & txtRatingAddedNTUser & "', '" & txtRatingModifiedNTUser & "');"
                dblocal.Execute stSQL
                
                '*********************** Update tblRatingCurrent*******************************
                stSQL = ""
                stSQL = "SELECT RatingCurrentID FROM tblRatingCurrent WHERE IntSecID = " & rst.Fields("IntSecID") & " AND RatingTypeID = " & cboRatingType.Value
 
 
 
 
               rstCur.Open stSQL, dbcur, adOpenDynamic, adLockOptimistic    '<---------- Updated line, this is where it used to error out
 
 
 
 
 
 
 
                
                If rstCur.BOF = False Or rstCur.EOF = False Then
                    stSQL = ""
                    stSQL = "UPDATE tblRatingCurrent SET RatingID = " & cboRating.Value & " WHERE RatingCurrentID = " & rstCur.Fields("RatingCurrentID")
                    dbcur.Execute stSQL ' <------------- updated line
                Else
                    stSQL = ""
                    stSQL = "INSERT INTO tblRatingCurrent (IntSecID, RatingTypeID, RatingID) VALUES (" & rst.Fields("IntSecID")
                    stSQL = stSQL & ", " & cboRatingType.Value & ", " & cboRating.Value & ");"
                    dbcur.Execute stSQL ' <------------ updated line
                End If
                
                rstCur.Close
                '******************************************************************************
                
                
                rst.MoveNext
            Loop
            rst.Close
 

Open in new window

0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

730 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