Connection/Table name keeps changing

JEQ214

New Member
Joined
Jul 14, 2011
Messages
6
Hello,

I'm working in Excel 2007 and I have a command button that changes the criteria of a sql from an Access DB based on some checkboxes. I can't run it more than once or it will change the Connection and Table names from "Top15Office" to "Top15Office_1" and give me a "Subscript out of Range" error.

Code:
        sConn = "DSN=MS Access Database;DBQ=\\nv-mc044100-dmr\4100\CRE.mdb;DefaultDir=\\nv-mc044100-dmr\4100\;"
        sConn = sConn & "DriverId=25;FIL=MS Access;MaxBufferSize=2048;PageTimeout=5;"
 
        'Office
        Set oCn = New ADODB.Connection
        oCn.ConnectionString = sConn
        oCn.Open
 
        sSQL = "SELECT Top 15 qryComl_Conc_Top15_Office.NAME, qryComl_Conc_Top15_Office.`ACCT#`, " & _
        "qryComl_Conc_Top15_Office.`BANK COMMIT`, qryComl_Conc_Top15_Office.PD_LGD_LEG " & _
        "FROM `\\nv-mc044100-dmr\4100\CRE.mdb`.qryComl_Conc_Top15_Office qryComl_Conc_Top15_Office " & _
        "WHERE (qryComl_Conc_Top15_Office.BUS_LINE In (" & Criteria & ")) " & _
        "ORDER BY qryComl_Conc_Top15_Office.`BANK COMMIT` DESC"
 
        Set oRS = New ADODB.Recordset
        oRS.Source = sSQL
        oRS.ActiveConnection = oCn
        oRS.Open
 
        ActiveWorkbook.Connections("Top15Office").Delete
        Sheet25.QueryTables("Top15Office").Delete
        Range("A8:D23").ClearContents
        Set oQt = Worksheets(6).QueryTables.Add(Connection:=oRS, Destination:=Range("A8:D23"))
            oQt.Name = "Top15Office"
            oQt.PreserveFormatting = True
            oQt.RefreshOnFileOpen = False
            oQt.RefreshStyle = xlOverwriteCells
            oQt.AdjustColumnWidth = False
            oQt.Refresh
 
        ActiveWorkbook.Connections("Connection").Name = "Top15Office"

The errors occur here:

Code:
        ActiveWorkbook.Connections("Top15Office").Delete
        Sheet25.QueryTables("Top15Office").Delete

How do I keep it from adding that "_1" at the end?

Jon
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Jon

Why are you adding a query table?

You've already established a connection to the database and opened a recordset.

Why not just copy the records from that recordset to the worksheet?

If you do need to add the query table then when it comes to delete it refer to it by it's index.

Assuming there is only one query table that should be 1.

You can use the same idea for the connection.
 
Upvote 0
Why not just copy the records from that recordset to the worksheet?

How do I do that? Sorry, my vba isn't that great. It took me awhile researching bits of this code and applying it to my own.

If you do need to add the query table then when it comes to delete it refer to it by it's index.

Assuming there is only one query table that should be 1.

There are multiple query tables. How do I know which one to reference? Is there a function to get the index of a query table?

Thanks for your help

Jon
 
Upvote 0
Jon

I was just posting about the query table and was going to post about how you can get the data without one.

I'll just post how to get the data without one and if you want I'll come back to the query table.

This is how you could put the data from the recordset on a worksheet, it assumes you already opened the recordset.
Code:
Dim rngDst As Range
Dim fld As ADODB.Field
Dim I As Long
 
' code to get recordset
 
 
   Set rngDst = Worksheets(6).Range("A8")
 
   For Each fld In oRS.Fields
            rngDst.Offset(,I).Value = fld.Value
            I=I+1
   Next fld
 
   rngDst.Offset(1).CopyFromRecordSet oRS
One of the things I was going to suggest for the query table probem was to not try and delete the connection.
 
Upvote 0
Thanks Norie! That worked and it was much faster, but there were some formatting problems. I applied the code to two of my tables which are next to each other; A8:D23 and G8:J23.

-row 8 no longer has field names. it copies the first record
-the header for the 2nd table shifted over 4 cells to K8:N8, but resumes the rest of the recordset in G9:J23. 2nd table also replaced headers with a copy of the 1st record.

:confused:

This works much better than what I originally had, btw. Thanks
 
Upvote 0
Just out of curiosity how did you fix the original problem?
 
Upvote 0
It wasn't really resolved. I took your advice and just copied the recordset to the worksheet instead of deleting/adding the querytables/connections, which was causing the problem.
 
Upvote 0

Forum statistics

Threads
1,223,237
Messages
6,170,928
Members
452,366
Latest member
TePunaBloke

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