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.
The errors occur here:
How do I keep it from adding that "_1" at the end?
Jon
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