Access and Excel 2013
Trying to use an Access base as a data source in Excel, it seems like if a query calls a VBA function, Excel doesn't see it.
This query shows up in Excel:
SELECT "StaticValue" AS static_value;
This one doesn't:
SELECT my_function() AS value_from_vba;
The code of the module containing my_function being:
Option Compare Database
Function my_function() As String
my_function = "ValueFromVBA"
End Function
How can I call a VBA function in an Access query and link this query to Excel ?
Hmm perhaps;
Make table and query the table (as stated in comments)
My preferred is just to export the data from access into a new worksheet that you need in excel, then you can do what you want with it if you are only getting values. Then you can go back in and update anything after (in your access tables) with VBA should you need it
I ended up creating a table (SELECT … INTO …) that I refresh on a regular basis. This table is linkable within Excel. It is probably a dirty trick, but it does the job.
Related
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.
I have a named variable in Excel, which not on the workbook itself, which I want to pass to a power query function.
I have been trying to understand how to do so. The only things I found were making use of Excel Table objects.
What if it is not a range at all, just a value, string, whatever which is only stored on name manager?
Do I need to go and put in on the workbook and transform it into a table? I didn't want to have that table editable since I don't need that...
Or can I get access to Excel's Name Manager to fetch a (normal) variable there ?
Thanks !
(In this example, in VBA I would do something like: ThisWorkbook.Names("This_one").Value)
This, from Wyn Hopkins, looks like it might be relevant to your question.
In the current Project i Need to Keep a Excel File which gets Values from a Machine to the Access Database to work with them and Import them in the Data Model.
Problem is some of the Values give invalid results due to the way they are saved. For example the timestamp is saved like
030420 instead of 03:04:20 and Access cant handle that and gives me a #NUMBER
I can not simply Change the datatype in Excel because the whole Excel gets refreshed every hour by a source that i cant influence.
Any help appreciated.
If Erik's proposal does not work, you can
- create a backup copy of your Excel source
- tweak the file: enter text in the first row of the problematic columns
- link the tweaked file into Access
- put back the real file in place.
Now the problematic columns should be read as Text, and you can build a query that solves any issue like conversion, null handling...
Link, don't import, the Excel file, and you have a linked table.
Now, use this linked table as source in a simpel select query where you modify the data and alias the fields as needed. For example:
Select
F1 As SomeName,
F2 As OtherName,
TimeSerial(Mid([F5],1,2),Mid([F5],3,2),Mid([F5],5,2)) As TrueTime
From
LinkedTable
Where
F7 Is Not Null
The use this query for your import.
Consider querying the Excel file instead of using a linked table.
The query can directly query an Excel range:
SELECT * FROM
[Excel 12.0 XML;DATABASE=PathToMyExcel;HDR=Yes;IMEX=1].[MyRange] t
Then, you can use functions like TimeSerial to cast numbers to time values.
So in Excel 2016, they have this neat tool called Power Query, basically a glorified excel table. Every table has steps in it for filtering, removing columns, etc... The first step is the source step, to assign a connection string basically to retrieve data, normally this source just points back to the query which created it.
Anyways, I'm trying in VBA to dynamically change the source of these power queries, anyone have any ideas?
I tried using the whole connections vibe, but was unsuccessful.
You can access the query through ActiveWorkbook.Item. You can then modify the Formula property. You can find the documentation on these objects here.
Please note that the Power Query object model was only added to VBA in Excel 2016 and cannot be accessed in prior versions.
The queries can be accessed via the Queries collection in a Workbook object. The relevant property for the source is Formula.
Example code:
ActiveWorkbook.Queries.Item("MyQuery").Formula = "[Insert actual M formula here]"
Many years later, but I'm adding a different solution for those, like me, still stuck with Excel 2013. As stated by #Alejandro in his response, Power Query was only added to the object model in Excel 2016.
If you are using an older version of Excel, you can use a cell-based solution similar to the one talked about here for relative source paths:
https://techcommunity.microsoft.com/t5/excel/power-query-source-from-relative-paths/m-p/206150
Basically have a cell some where in your workbook that contains your query's source. Name that cell, using standard Excel names. In the example below, I've named the cell SourceFileName, and I'm trying to load an Excel file to PowerQuery. The full name of the source Excel file (including path) is in SourceFileName . You can then access the contents of that cell via Power Query:
<previous M code>
sourceFileName= Excel.CurrentWorkbook(){[Name="SourceFileName"]}[Content]{0}[Column1],
Source = Excel.Workbook(File.Contents(sourceFileName ), null, true),
<rest of M code>
If you make a data connection to another Excel file or an Access table it will import that data into excel as a Table. This is great! But when I do the same with "From Text" and choose a CSV, it loads no problem, but it loads as a RANGE not a TABLE. This is highly frustrating as I NEED it to be in the table format so that I can take advantage of the dynamic column names.
Am I missing a tickbox somewhere? I'm not opposed to using VBA but it really seems odd that Excel can't do CSV to Table so I'm hoping for a native solution.
I should mention that if it's a VBA solution, it CANT break the workbook. So if I'm doing something like:
=SUMIFS(CSVDATA[SalesDollars], CSVDATA[RepName], "BOB")
It will still work after refreshing.
So you can get around the 'data connection' not creating a table by downloading the MS Excel 2010 plugin (from Microsoft) called Power Query. It's free and is a default feature in MS Excel 2013.
It will allow you to choose to create a Power Query Table from External Data >> From File >> From CSV
Upon doing this, it will create a named table for you and a Power Query object attached to the table. You can use the table itself the same way you normally would - with the Columns as references for formulas.
For instance, the default table that I just created using the steps above was auto-named: Table_ExternalData_1
I can then select it using the normal method in formulas:
=Table_ExternalData_1[Column2]
etc...
Hope that helps.