Unable to refresh Query table using Excel VBA

margarita13

New Member
Joined
Aug 3, 2011
Messages
2
Hello there,

I have recorded some code which creates a table selecting certain rows based on an SQL query (which I plan to make a dynamic string later on). This is a query table within Excel 2010 which imports data from an Access database. When I run the code I get the error message 'Run time error 1004': A table cannot overlap a range that contains a PivotTable report, query results, protected cells or another table.'

Code:
Sub Macro1()
'
' Macro1 Macro
'

'
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:=Array(Array( _
"ODBC;DSN=MS Access Database;DBQ=I:\SAM\ANALYSIS\Database2.accdb;DefaultDir=I:\SAM\ANALYSIS;DriverId=25;FIL=MS Ac" _
), Array("cess;MaxBufferSize=2048;PageTimeout=5;")), Destination:=Range( _
"$AD$1")).QueryTable
.CommandText = Array( _
"SELECT *" & Chr(13) & "" & Chr(10) & "FROM `I:\SAM\ANALYSIS\Database2.accdb`.Sheet1 Sheet1" & Chr(13) & "" & Chr(10) & "WHERE (Sheet1.Samp_Spreadshee" _
, "t_Dates>{ts '2011-04-16 00:00:00'})" & Chr(13) & "" & Chr(10) & "ORDER BY Sheet1.ID")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Table_Query_from_MS_Access_Database_1"
.Refresh BackgroundQuery:=False
End With
ActiveWorkbook.Save
End Sub


How do I edit this code so I can keep running it to refresh the table? I want to change the SQL SELECT statement occasionally by changing a cell containing the SQL query and that is why I want to refresh this table using this code. Please help me amend this code and Thanks in advance.
 
Last edited by a moderator:

Some videos you may like

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
36,073
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
Welcome to the forum.

That code adds a new table each time, rather than just refreshing the existing one. A simple fix would be to delete the existing one first:

Code:
On Error Resume Next
Range("$AD$1").Listobject.Delete
On Error goto 0
 

Watch MrExcel Video

Forum statistics

Threads
1,122,632
Messages
5,597,287
Members
414,134
Latest member
Tiyas44

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
Top