Run-time error -2147467259 (80004005) in SQL string for connection

James Snyder

Well-known Member
Joined
Jan 11, 2013
Messages
618
I am getting run-time error -12147467259 on the sheet name used in the connection query. I have used a query to open a connection before and am puzzled as to why it doesn't work now.

Declarations and the call:
Code:
Public Sub Main()
    
    Dim fileName As String                  ' Reuseable parameter
    Dim obstInFile As String                ' Source spreadsheet for Obstructed
    Dim obstConn As ADODB.Connection        ' ADO connection for obstructed
    Dim obstRS As ADODB.Recordset           ' ADO recordset for obstructed
    Dim obsSQL As String                    ' String to pass to set up the recordset

<snip>

    obsSQL = "SELECT * FROM [Obstructed$] GROUP BY [Item No.] ORDER BY [PSID]"
    funcReturn = MakeConnection(obstConn, obstRS, obsSQL, obstInFile)
    If funcReturn <> "Success" Then
        errString = "Obstructed Excel: Unable to create obstructed recordset"
        failReturn = ProblemReport(errString, sendDate)
        GoTo ExitPoint
    End If

The function:
Code:
Private Function MakeConnection(ByRef newConn As ADODB.Connection, _
    ByRef newRS As ADODB.Recordset, _
    ByRef sqlStr As String, _
    ByRef sourceWkSht As String) As String
    
    ' Declarations
    Dim connStr As String
        
    ' ADO ACCESS TO SPREADSHEET
    ' Create the ADO objects to be used
    Set newConn = New ADODB.Connection
    Set newRS = New ADODB.Recordset
    
    ' Open the connection
    connStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & sourceWkSht _
        & ";Extended Properties='Excel 12.0;HDR=YES;IMEX=1';"
    newConn.Open connStr
    
    ' Open the spreadsheet
    With newRS
        .CursorLocation = adUseClient
        .CursorType = adOpenForwardOnly
        .Open sqlStr, newConn
    End With
    Set newConn.ActiveConnection = Nothing  ' Sets recordset to disconnected recordset
    
    MakeConnection = "Failed to create recordset"
End Function

The error gets generated on the [Obstructed$] sheet name when .Open sqlStr, newConn is called.
 
Good questions. It is an .xlsx file when I receive it. I must have been wrong about the output being .xls. Opening and saving had no effect.
CORRECTION:
The file is exported as .xls, but it prompts you to save it. Once I save it using Excel 2010, it is an .xlsx.
 
Last edited:
Upvote 0

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Could you create a censored copy of the actual file that I can test and then upload it somewhere?
 
Upvote 0
Just an update:
I reordered the Tool>References to match the previous working demo. I added the lock parameter of the recordset which I had left off. No change.
Code:
    With newRS
        .CursorLocation = adUseClient
        .CursorType = adOpenForwardOnly
        [COLOR=#008000].LockType = adLockReadOnly[/COLOR]
        .Open sqlStr, newConn
    End With

Next step that I can think of is to start a new workbook, delete the old, and paste in the macro code from a text file.
 
Upvote 0
I'm confused - the file you shared with me does not have a sheet called Obstructed in it (that's the file name, not the name of a worksheet).
 
Upvote 0
Rory, you hit the nail on the head. Instead of using test data run from the test report renamed to "Obstructed", I had renamed a output of the production report of the same data to 'Obstructed' and hadn't caught the sheet still being the original production report name. Although I haven't had a chance to test it, you found the solution. A 'second set of eyes'...
 
Upvote 0
Testing shows that it wasn't the source of the problem. Best guess is that I overwrote the file with the properly named tag. I'll keep working on it.
03.08.2013-13.14.31 - Bermex's library

Improved query for setting up the recordset:
Code:
    obsSQL = "SELECT [PSID], " _
            & "[Leak Found], " _
            & "[Atmospheric Corrosion Found], " _
            & "[Meter Reading], " _
            & "[Meter Location], " _
            & "[Item No.], " _
            & "[User], " _
            & "[Item Date], " _
            & "COUNT([PSID]) " _
            & "FROM [Obstructed$] " _
            & "GROUP BY [Item No.] " _
            & "ORDER BY [PSID]"
    funcReturn = MakeConnection(obstConn, obstRS, obsSQL, obstInFile)
 
Upvote 0
Things I have tried today:
• Took my computer back to a restore point 3 weeks ago (3 hours reinstalling apps, re-entering passwords, etc) - no change
• Reran the demo to prove that it still worked, then changed the query - and it failed

Old query:
Code:
sqlString = "SELECT * FROM [(D) COMPLETED INSPECTION - COMM$]"

New query that failed - note that it is running against the same file in the same location:
Code:
    sqlString = "SELECT [PSID], " _
        & "[Leaks Found], " _
        & "[Atmospheric Corrosion Found], " _
        & "[Meter Reading], " _
        & "[Meter Location], " _
        & "[Item No.], " _
        & "[User], " _
        & "[Item Date] " _
        & "FROM [(D) COMPLETED INSPECTION - COMM$]"

There were no other code changes. I had listed the fields individually because I was going to use a GROUP BY and a ORDER BY. I am done for the day (mentally) and will play with the query in the demo on Monday. Maybe I can get away without the GROUP BY and ORDER BY if I figure out how to use queries against the recordset. The "*" works well because I am using every column. Hat off to Rory who helped even when my brain was scrambled with a migraine.
 
Upvote 0

Forum statistics

Threads
1,223,246
Messages
6,170,988
Members
452,373
Latest member
TimReeks

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top