In the company that I work, there is a folder with many access database files that are generated by a system. They have all the same structure but different data. I'm trying to build an Excel tool to compare and analyze some of them using power query (connected to workbook datamodel and use DAX as well). People who will use the system are not very familiar with vba, power queries (me neither), or SQL statements. So, what I thought to do is to code a vba function to allow people to select the files they want to analyze and then, update the workbook power queries with this data. The power queries source data should be SQL statements.
I'm using the "Formula" propertie to make all the changes needed (actually, new data source file, append a column with the filename, and append the result for the other data source files (if more than one is selected). The following code works if I'm importing a table but I'm not able to do it using a SQL statement. I've tried to change some Access.Database parameters like using Query="Select XXX" instead of Item="TableXXX" but it didn't work. I've found some information about using ODBC but I wouldn't like to work with ODBC because, it seems to me, that the tool can result in many connections to handle (open and delete) whenever the user decides to change the data source.
Following there is an example with only the "Formula" propertie for two data source files. I didn't write the loop to build the string strSource because I don't have problems with it. I suppose the problem is to use an SQL statement in Access.Database. Thanks for the help.
strSource = "let
Fonte = Access.Database(File.Contents("PathName\File01Name.mdb")),
_TbEx_1 = Fonte{[Schema="",Item="TableExample"]}[Data],
_TbEx_C1 = Table.AddColumn(_TbEx_1, "Case", each "File01Name", type text),
Fonte_2 = Access.Database(File.Contents("PathName\File02Name.mdb")),
_TbEx_2 = Fonte_2{[Schema="",Item="TableExample"]}[Data],
_TbEx_C2 = Table.AddColumn(_TbEx_2, "Case", each "File02Name", type text),
_TbExComb = Table.Combine({_TbEx_C1 , _TbEx_C2})
in
_TbExComb"
ActiveWorkbook.Queries("PwQry01").Formula = strSource
ThisWorkbook.Connections("Qry-PwtQry01").Refresh
Searching on internet, I've found a way of using SQL statement, but the problem becames to combine the connections of different files on the same power query. Following the code that I've found, but I suppose the "Formula" propertie is closer the final answer.
cnString = "OLEDB;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & _
"PathName\File01Name.mdb;Mode=Read"
ThisWorkbook.Connections.Add2 _
Name:="MyConnect", _
Description:="Testing connection", _
ConnectionString:=cnString, _
CommandText:="SELECT * FROM TableExample", _
lCmdtype:=XlCmdType.xlCmdSql, _
CreateModelConnection:=True, _
ImportRelationships:=True
Being naive, what I'd like to see is something like:
strSource = "let
Fonte = Access.Database(File.Contents("PathName\File01Name.mdb")),
_TbEx_1 = Fonte{[Schema="",Query="SELECT XXXXX"]}[Data],
_TbEx_C1 = Table.AddColumn(_TbEx_1, "Case", each "File01Name", type text),
Fonte_2 = Access.Database(File.Contents("PathName\File02Name.mdb")),
_TbEx_2 = Fonte_2{[Schema="",Query="SELECT XXXX"]}[Data],
_TbEx_C2 = Table.AddColumn(_TbEx_2, "Case", each "File02Name", type text),
_TbExComb = Table.Combine({_TbEx_C1 , _TbEx_C2})
in
_TbExComb"
ActiveWorkbook.Queries("PwQry01").Formula = strSource
ThisWorkbook.Connections("Qry-PwtQry01").Refresh