I need to update a Power Query using VBA or be able to define the query so that the code is linked to a named range in the same workbook.
I write dynamic SQL scripts (based on user input) in a worksheet, then in a single cell (range name = “sqlQuery”) I use the Textjoin function to compile the code into that cell. Most are select queries but some are update queries to an ERP database running on Pervasive.
Update Example:
Update v_Inventory_mst3 set STANDARD_COST_QTY = '0' where Part = '0-0.250A' ;
Update v_Inventory_mst3 set STANDARD_COST_QTY = '0' where Part = '000M-SS' ;
select now() ;
The name of the Power Query is: Query1
I can do this successfully but manually and the query definition looks like this:
= Odbc.Query("dsn=global_fah", "Update v_Inventory_mst3 set STANDARD_COST_QTY = '0'
where Part = '0-0.250A' ; Update v_Inventory_mst3 set STANDARD_COST_QTY = '0'
where Part = '000M-SS' ; select now() ; ")
The results are that the values get updated in the database as specified and the value in the query results is the last time I ran it.
If I can link the defined name range into the query definition then presumably all I would need to do is refresh it to process the next set of updates.
If I can’t link the defined name into the query definition but I can run a VBA script to update it from the current value in “sqlQuery” then all I would need to do is execute the macro. This is the type of process I currently use for select queries, from a (non-Power Query) TableQuery. This meth does not work for multiline update scripts to a Pervasive db.
VBA sample for a select query:
Public Sub QTY_VAR()
' Copy Content which contains a textjoin formual formual compiling many lines of code
Dim sqlCode As String
sqlCode = Range("sqlQVAR").Value
' Select Report which is a Query Table (not created through Power Query)
Application.Goto Reference:="rptQVAR"
With Selection.ListObject.QueryTable
.CommandType = xlCmdSql
.CommandText = sqlCode
.Refresh BackgroundQuery:=False
End With
DoEvents
If Err <> 0 Then MsgBox Err.Description
Application.CutCopyMode = False
End Sub
I write dynamic SQL scripts (based on user input) in a worksheet, then in a single cell (range name = “sqlQuery”) I use the Textjoin function to compile the code into that cell. Most are select queries but some are update queries to an ERP database running on Pervasive.
Update Example:
Update v_Inventory_mst3 set STANDARD_COST_QTY = '0' where Part = '0-0.250A' ;
Update v_Inventory_mst3 set STANDARD_COST_QTY = '0' where Part = '000M-SS' ;
select now() ;
The name of the Power Query is: Query1
I can do this successfully but manually and the query definition looks like this:
= Odbc.Query("dsn=global_fah", "Update v_Inventory_mst3 set STANDARD_COST_QTY = '0'
where Part = '0-0.250A' ; Update v_Inventory_mst3 set STANDARD_COST_QTY = '0'
where Part = '000M-SS' ; select now() ; ")
The results are that the values get updated in the database as specified and the value in the query results is the last time I ran it.
If I can link the defined name range into the query definition then presumably all I would need to do is refresh it to process the next set of updates.
If I can’t link the defined name into the query definition but I can run a VBA script to update it from the current value in “sqlQuery” then all I would need to do is execute the macro. This is the type of process I currently use for select queries, from a (non-Power Query) TableQuery. This meth does not work for multiline update scripts to a Pervasive db.
VBA sample for a select query:
Public Sub QTY_VAR()
' Copy Content which contains a textjoin formual formual compiling many lines of code
Dim sqlCode As String
sqlCode = Range("sqlQVAR").Value
' Select Report which is a Query Table (not created through Power Query)
Application.Goto Reference:="rptQVAR"
With Selection.ListObject.QueryTable
.CommandType = xlCmdSql
.CommandText = sqlCode
.Refresh BackgroundQuery:=False
End With
DoEvents
If Err <> 0 Then MsgBox Err.Description
Application.CutCopyMode = False
End Sub