I am creating a SQL query to get data from an SQL database, but the macro recorder posts the data into a listobject. I want the query to post the data into an array instead of listobject.
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=Query1;Extended Properties=""""" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = xlCmdSql
.CommandText = Array("SELECT * FROM [Query1]")
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.ListObject.DisplayName = "Query1"
.Refresh BackgroundQuery:=False
End With
I've recorded a macro to query a database, and when I record the macro, the query runs properly. However, when I try to run the macro again on the same sheet or on a different one, I get the error:
runtime error 1004, "SQL syntax error"
on the line
.Refresh BackgroundQuery:=False".
Below is the recorded macro.
Sub Macro3()
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Substation Prod;SRVR=SUBP;UID=U326357;", Destination:=Range("A1"))
.CommandText = Array( _
"SELECT FACT_MONTHLY.TIME_STAMP, FACT_MONTHLY.SYSTEM_NUMBER_VAL0, FACT_MONTHLY.SUBSTATION_NUMBER_VAL0, FACT_MONTHLY.MVA_MAX_OUT_VAL0, FACT_MONTHLY.MW_MAX_OUT_VAL0, FACT_MONTHLY.MVAR_MAX_OUT_VAL0, FACT_" _
, _
"MONTHLY.MW_MIN_OUT_VAL0, FACT_MONTHLY.MVAR_MIN_OUT_VAL0, FACT_MONTHLY.PF_MAX_VAL0, FACT_MONTHLY.PF_MIN_VAL0, FACT_MONTHLY.TOP_OIL_TANK_MAX_VAL0, FACT_MONTHLY.LOAD_FACTOR_VAL0, FACT_MONTHLY.PH1_TAP_MAX" _
, _
"_DRAG_VAL0, FACT_MONTHLY.WIND_TEMP_MAX_VAL0, FACT_MONTHLY.LTC_TEMP_MAX_VAL0, FACT_MONTHLY.PH2_TAP_MAX_DRAG_VAL0, FACT_MONTHLY.PH3_TAP_MAX_DRAG_VAL0, FACT_MONTHLY.BOT_OIL_TEMP_MAX_VAL0, FACT_MONTHLY.PH" _
, _
"1_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH2_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH3_TAP_MIN_DRAG_VAL0, FACT_MONTHLY.PH1_LOAD_MAX_KV_VAL0, FACT_MONTHLY.PH2_LOAD_MAX_KV_VAL0, FACT_MONTHLY.PH3_LOAD_MAX_KV_VAL0, FA")
.Name = "Query from Substation Prod"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
I've searched online for ways to fix this issue, but none of the other solutions I've found are working. Any help would be greatly appreciated.
All the previous lines inside the With statement are setting properties.
the
.Refresh BackgroundQuery := False
is a method call.
The refresh is supposed to refresh the results.
The background Query is for when quering SQL data and is optional so I think you can leave it off and just have .Refresh
For further assistance try VBA Excel QueryTables.add .Refresh BackgroundQuery Error
I am trying to write some VBA that will import some data into a query table using a .odc file that is stored in a SharePoint data connection library. I used the macro recorder to record the process where I add the connection, then go to the existing connections and import the data into a table in the current worksheet(which worked when I did it manually).
The recorder spit out the following code (I removed the command text since it contains some sensitive info, but it was a big string of SharePoint related stuff like the list and view GUID):
Sub RecordedImportMacro()
Workbooks("MyWorkbook.xlsm").Connections.AddFromFile _
"http://path/to/my/odcfile/on/sharepoint.odc"
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"OLEDB;Provider=Microsoft.Office.List.OLEDB.2.0;Data Source="""";ApplicationName=Excel;Version=12.0.0.0" _
, Destination:=Range("$A$1")).QueryTable
.CommandType = 5
.CommandText = "some command text here"
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.SourceConnectionFile = "http://path/to/my/odcfile/on/sharepoint.odc"
.ListObject.DisplayName = "My_Table"
End With
End Sub
However, when I run the macro to seemingly perform the exact same task that worked before I get the following error: Run time error 1004. I googled and it didn't really find anything that pertained to my use case
when I debug the following line is highlighted: .CommandType = 5
Any ideas on how to get this working?
I was able to get it working using code found here
Below is the code:
Sub ThisWorks()
With ActiveSheet.QueryTables.Add(Connection:= _
"FINDER;http://path/to/my/odcfile/on/sharepoint.odc" _
, Destination:=Range("$A$1"))
.RefreshStyle = xlInsertDeleteCells
.RowNumbers = False
.FillAdjacentFormulas = False
.RefreshOnFileOpen = True
.HasAutoFormat = True
.BackgroundQuery = True
.TablesOnlyFromHTML = True
.Refresh BackgroundQuery:=False
.SavePassword = False
.SaveData = True
End With
End Sub
I am having a little trouble returning SQL data into an Excel table, instead of dropping the data into the cell I specify (when not using a table) it pushes the whole table to the right.
Does anyone know how I can get it to populate the table as I want to make use of splicers etc. to filter the data. I know I could import the data then turn it into a table but if there's a quicker, simpler way then I'd rather that.
Right now the code I'm using is:
With ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=<dbname>", Destination:=ActiveSheet.Range("A13"))
.CommandText = strSQL
.Name = "Query"
.FieldNames = False
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
ActiveSheet.QueryTables(1).Delete
Many thanks in advance.
Dan
I am writing a tool to import a database query results into Excel in VB.NET
I tried the following code. It is not working.
With objApp.ActiveSheet.QueryTables.Add(Connection:="ODBC;DSN=Build_statistics;", _
Destination:=objApp.Range("G15"))
.CommandText = "SELECT * from mytable"
.Name = "Query"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = Microsoft.Office.Interop.Excel.XlCellInsertionMode.xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
End With
It is working fine in VBA, but not in VB.NET.
Why not load the query results into a DataSet and then import the contents of the DataSet table into Excel?
The line
**.Refresh(BackgroundQuery:=False)** 'I am getting error here
is not correct and should be replaced by
.Refresh BackgroundQuery:=False
.