How to reference Access query defs from Excel? - excel

I have a need to change the SQL of a query saved in Access from Excel and it must use late binding.
Specifically I'm building SQL strings in Excel VBA featuring a number of parameters provided by the user in Excel and querying the database via an ADODB connection. I have constructed core queries in the database which do most of the work so I can keep my SQL strings and the amount of data returned light on the Excel end.
Mostly this works fine but some of those core queries rely on other queries nested several levels deep, and some of the parameters I need to supply are around data in those parent queries. So, I'm thinking I need to temporarily amend the SQL of those parent queries before running my main code (unless anyone has another idea?). I know how to change the SQL for a queryDef in Access, but not how to get to the queryDefs from Excel.
So TL:DR - how can I get a handle on the queryDefs in an Access accdb from Excel?
Thanks.

Here is a late bound example
Dim db As Object
Dim qdef as Object
Set db = CreateObject("DAO.DBEngine.120").OpenDatabase("path to db")
set qdef = db.QueryDefs("query name")

Related

Excel Power Pivot / Data Model - passing a parameter from worksheet

I have a SQL query I am using for a workbook requiring multiple pivot views from the same dataset. Part of the query is looking at a declared value and based on that value it will either return one result or another, that declared value however needs to be a parameter that end users will input into the workbook.
Normally I would use the excel function to pull the data from the SQL server and then pass a parameter in the advanced query option using " Let Parameters = Excel.CurrentWorkbook()".. however the result set it too large and therefore I need to use Power Pivot.
I however for the life of me can find no help on how to pass a parameter from a cell in a worksheet to power pivot - is this possible, is there a VB script I can use to update the query, alternatively what other options are available other than having to get the end users to go into power pivot and edit the query in the properties each time?
As the variable is used in a computation, I am not using the parameter to filter the dataset and that seems to be the only advice I have been able to find. I am new to Power Pivot.
I came right by doing the following:
Add the SQL query in Power Query, as a data connection only. In the advanced query options, I added the required parameters.
I then selected the query and selected the option to load to Data Model.
Seems to be working.

Excel create multiple MS Queries using one data connection between two sheets in the same workbook

In Excel 2021, what exactly is a "data connection", "query" and "domain source name"?
Let's say I have a Workbook "Manahil_Customer_Database.xlsm" in which I have a sheet "sht_Customer_Cities" that has a table "tbl_Customer_Cities". In a new sheet "sht_Report" I want to run two queries using one connection via MS Query. Now when I go through the MS Query route I get one Domain Name Source File "Manahil_Customer_Database.dsn" and one MS Query file "Customer_Countries_Cities.dqy" and one Connection file "Customer_Countries_Cities.odc".
However when I look at the "Queries & Connections" it says 0 Queries and 1 Connection named "Customer_Countries_Cities". I want to be able to establish a single Data Connection via MS Query from the "sht_Report" to the Workbook "Manahil_Customer_Database.xlsm" and than run multiple queries using the same connection.
Power Query replaced MS:Query from Excel 2016 onwards. The objects and panes you are describing relate to Power Query, not MS:Query.
Power Query is far more functional, reliable, flexible and performant than MS:Query.
For example depending on your exact requirement, you might create a base query that gathers all the required data, then refer to that base query in Reference queries that filter the output needed for each destination table.
Here's a starting point for Power Query:
https://support.microsoft.com/en-us/office/about-power-query-in-excel-7104fbee-9e62-4cb9-a02e-5bfb1a6c536a
Power Query is a MS tool that assists you on your ETL tasks.
As read in a previous answer, it is based on M language.
To be able to import / modify / connect your data, the command is:
DATA / GET DATA and select your input
Check this link for a quick introduction:
https://learn.microsoft.com/en-us/power-query/power-query-what-is-power-query
If I understand the situation correctly, you are working internally, within a single excel file. Data connections, queries, and domain sources, are all used to associate externally.
Internally I would think you could use a pivot-table and/or a slicer.
If you provide additional details on what specifically you are trying to do, a better answer could be provided.
Some additional reading below may help further:
Power Query Help
Data Connections
Queries
External Links

Connecting many Excel data tables to Access queries

We need to update about 40 Excel data tables from queries in a single Access database, on demand. Ideally, Data > Refresh All should perform the update.
I’ll want to use VBA to set this up; a master Excel table identifies the data tables and corresponding query names.
What’s a clean way to design this? Should my VBA use ODBC, OLE DB, or ADO to create 40 database connections? (For maintainability, I'd prefer not to have a lot of VBA complexity, since our shop doesn't otherwise use these APIs -- just Excel with simple VBA.)
I’m running MS Office 2013.

Is it possible to use MS Excel as front and back-end for MS Access?

in a few words, I would like to use MS Excel together with MS Access, such that Excel is the front and backend, but Access provides the capability for relations between data and sql queries. Is that possible?
In a few words more, I would like to use MS Excel together with MS Access such that :
the 'raw data' is visible and accessible from Excel ((write, modify, delete data entries!)
I can use SQL queries to query my data via MS Access (and hence dynamically create different views on my data as excel sheets)
I can implement relations between the data via MS Access (for consistency checks, etc)
My own research got me here:
I know how to set up access to use an excel table as its data backend (both, linked to or copied from excel)
I know how to send SQL queries from Excel to an access database and display the results in excel sheets
However, here is what's missing:
in Access if I use tables that are linked to Excel, I cannot create relations between tables (access says it's not possible)
in Access if I use tables that are imported (copied) from Excel, I can create relations between tables, but after creating relations I cannot update my import anymore (access says it's not possible, I'd have to remove the relations before)
In any case I have to trigger updates manually ...
Background:
Excel is currently used for project management. The management process often involves 'exploring the data', e.g. assigning people different projects, adding people, adding projects, ... and then seeing how that affects other things. (Hence) it is required that Excel is kept as the platform. But it would be nice to have database feature like linked tables and queries, as data is getting too complex to manage with excel only tools. Also, this is not a large project, but I'm aiming for an easy and not complex solution.
Basically I want to keep excel and everything, I'd simply like some new features (e.g. relating tables, sql-like queries).
So, does anybody have an idea how to go about this?
Thanks a lot!
I have been using a lot Excel as Front-end and Access as Back-end.
Typically, my solutions involve UserForms or ActiveX Objects in Excel Sheets that manipulate and show data that is stored in an Access database. The Excel file and Access file are completely independent, which allows you to create and alter relationships in Access without restrictions. Just make sure you update your queries in Excel. There are even some cases that I use more than one Workbook to access the same Access database.
I make Excel communicate with Access via Excel VBA, using the ADODB. A quick Google search got me to this tutorial.
Cheers,
Tiago

How to join 2 Excel connections?

I've got 2 connections in a worksheet, querying an.accdb. How can I get an inner join on them within Excel?
Restrictions:
One of the existing connections returns a crosstab query. Crosstab queries cannot be used as subqueries in Access directly within an SQL string (the TRANSFORM keyword cannot be used after the FROM keyword). The crosstab must be saved as a new querydef first, and then the querydef is used as an alias
Both connection strings have WHERE criteria that change dynamically and are generated on-the-fly, hence it's not practical to open up the .accdb and change a crosstab querydef constantly especially in a multiuser environment
Hence I stress I want the join to be done within Excel. Since all the required data gets pulled in and stored locally in the spreadsheet after a Refresh All, surely we have all we need to perform a join in Excel...

Resources