Updating an Access Query definition (SQL) and reflecting in dependent queries - vba

I am still relatively new to Access and the functionality, but bottom line is I have a query (called ScenarioQuery), which pulls data from a table ScenarioDetails and creates columns which are equivalent to fiscal periods, so each column will be a calculated column like
IIf(MonthNumber = 1, [Value], 0) AS [S2020Jan]
What I am trying to do is use VBA to dynamically update the year as part of the column aliases as each "scenario" will contain a different year range.
I have successfully set up the macros to update the years dynamically through using arrays and recordsets, however, I have linked (dependent) queries which reference the field names in their own result sets and these don't update automatically when a query def is updated (at least as far as I know)
Is there a way to accomplish this dependency chain update? Or would I either have to write further macros to update these queries / alter the objects to pull from another source ?

Use the QueryDefs collection, like so
For Each q In CurrentDb.QueryDefs
q.SQL = Replace(q.SQL, "2020", "2021")
Next q
https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/querydefs-collection-dao

Related

Change Column location of SQL statement in Excel

Within excel I have connected my Microsoft SQL Server database to it, to display results. The system I have set in place is built off of a form. If a user chooses option 1, the query results will show:
Select person, car, house from mytable1
If the user chooses option 2, the query result will show:
Select job, person, land, truck from mytable2
The very first select statement will give me a table in the column order in which I would like it. However, as a user uses the form again, it will re-query it to use which ever select statement is requested. When the re-query happens the column order which items are shown are in different areas. Even if the select statment is stated within the same order. Is there a way in which I could order the columns in a specific order?
I've attempted to unchecked "Preserve Column Sort" within the Data Range Properties, but ends up leaving empty columns. I.E. : Column1, Column2, Column3, etc.
You may already know this, but since Excel allows you to move the columns in a table / ListObject around, it seeks to preserve any changes you make. So, if you run a query:
select one, two, three
And then move the column "three" in front of "one," when you re-run your query, it will keep them in that order in the ListObject, even though the query said otherwise.
This also means if you add a column, no matter where you add it, it will go to the end when MS Query renders the output.
select four, one, two three
("four" goes to the end in Excel, even though it was listed first in SQL)
In your example, the column "person" was common across the two queries, so Excel (MS Query) would move it to the first position and put all other columns after that.
When Excel deletes the old columns, it leaves a tracer behind -- you may notice the columns that follow your table aren't the normal size; they are the size of the fields that were deleted. I call them "ghosts."
This is a serious hack, but the only way I know of to alleviate this problem is to run a bogus query (ie select 1), delete the ghosts -- remove the entire columns, and then run your second query. Here is some ugly code I use in VBA to do this:
Dim lo As ListObject
Set lo = Sheets("Sheet1").ListObjects("Table_ExternalData_1")
Range(lo.HeaderRowRange.Offset(0, lo.HeaderRowRange.Columns.Count), _
Range("XFD1")).EntireColumn.Delete
Yes, this deletes every column after the table, which means if there is useful data above or below the table in columns after the table, those are wiped out.
Maybe there is a better way -- I'm curious to see if you get any other solutions.

Dynamic Parameter in Power Pivot Query

We are using Excel 2013 and Power Pivot to build modules that consist of several Pivot tables that are all pulling data from the same Power Pivot table, which queries our T-SQL data warehouse.
In an effort to simplify and fully automate this module, we wanted to create a text field that would allow a user to enter a value (a client ID# for example), and then have that value be used as a parameter in the Power Pivot query.
Is it possible to pass a Parameter in the Power Pivot query, which is housed in a text field outside of the query?
You can also pass a slicer or combobox selection to a cell. Define a name for that cell. Put that cell (and others if you have multiple text variables to use) in a table. For convenience, I usually name this table "Parameters". You can then 'read in' the parameters to your query and drop them in your query statements.
The code at the top of your query to read these parameters in might look like...
let
Parameter_Table = Excel.CurrentWorkbook(){[Name="Parameter"]}[Content],
XXX_Value = Parameter_Table{1}[Value],
YYY_Value = Parameter_Table{2}[Value],
ZZZ_Value = Parameter_Table{3}[Value],
Followed by your query wherein instead of searching for, say a manually typed in customer called "BigDataCo", you would replace "BigDataCo" with XXX_Value.
Refreshing the link each time a different customer is selected will indeed be a very slow approach, but this has worked for me.
Rather than pass a parameter to the data source SQL query, why not utilize a pivot table filter or slicer to do allow the users to dynamically filter the data? This is much faster than refreshing the data from the source.
If for some reason you need to pass this directly to the source query, you'll have to do some VBA work.

MS Access Delete query based on combobox

I have a table with a bunch of different fields. One is named period.
The period is not part of the raw data but I run a query when I import new data to the database that gives each record a period.
Now I need a delete query that will delete all the records that have the same period as what is selected in a combobox.
The values in the combobox come from a calendar table that contain all the possible values that could be in that period column at any time.
This is the basic query i thought would solve this issue but it tells me it is going to delete 0 rows every time I run it:
DELETE *
FROM PlanTemp
WHERE PlanTemp.period = Forms![Plan Form]!Combo163;
If you don't need the key field, just remove it.
Look at the "PROPERTIES" section and look at the column names.
Ether remove it there, or from your QUERY source.
You can also look at the Data section of the properties, and change your BOUND column, to Column 2... or whatever holds the data you want to use.

Google Spreadsheet: SQL query for automatic update

Suppose I have one master sheet with several columns and rows which keeps updating whenever. Now I need to use some of the data from the master sheet as reference, to create analytic report in another sheet. How can I use a SQL query to reference the appropriate data from the original sheet? And by doing so, what happens when the original sheet is updated with more records? Will they be automatically included as well in the second sheet?
I'm just a beginner, but if anyone has any clue in this matter, or if there's any alternative method that could be implemented, please let me know! I'd be very grateful for any help!
I think you're looking for Google Query Language.
The QUERY function is a built-in function that allows you to perform a query over an array of values using the Google Visualization API Query Language.
Query String - A query string for applying data operations. The query operates on column IDs directly from the input range and uses a subset of the SQL language. For example, "select E," "select A , B," "sum(B),C group by C," "select D where D < 'Nick' ."
Documentation

Access sharepoint list data in SQL

I am new to SharePoint development. I have a list, this list has a column that is called Todaysdate. This column needs to be updated daily to today's actual date and since it contains ~20,000 rows I am NOT going to update it manually everyday. Because it's used in a calculation row.
My question is can I just use SQL and update the rows in the UserData table that correspond to the datetime column that I need?
I can query the list of rows by something similar to
Select * from UserData where tp_ListID = 'GUID'
but the data contained in the column datetime3 is not just the Todaysdate information. How do I return just the Todaysdate info?
You really should not query let alone update the SharePoint content database directly using SQL. It is totally unsupported, so if you break something you are left alone, and the database schema may change with future service packs / releases.
Also as noesgard mentioned it in his comment you do not need it to use today's date in a calculated field, see this blog entry on how you can do that.