0

I am trying to setup an access database (Queries.accdb) that will be used to query multiple access databases as table sources. I am able to do it by hardcoding, but I am getting syntax errors when I try to use relative paths. I would like to be able to move the Queries.accdb around.

Current file structure:

  • C:\Temp\PB\28\A.accdb
  • C:\Temp\PB\28\B.accdb
  • C:\Temp\PB\28\Queries.accdb
  • C:\Temp\PB\29\A.accdb
  • C:\Temp\PB\29\B.accdb
  • C:\Temp\PB\29\Queries.accdb

Current working query:

SELECT * 
FROM  [;database=C:\Temp\PB\28\A.accdb].[Catalog] AS a 
INNER JOIN [;database=C:\Temp\PB\28\B.accdb].[Catalog] AS b 
ON a.componentid = b.externalid;

When I try different variations (.\ ~\, etc.) to remove the hardcoded path in front of the DB file, it automatically adds the "default database folder" setup in Access. (example: "C:\" would change the following query to "C:\A.accdb")

SELECT * 
FROM  [;database=A.accdb].[Catalog] AS a 
INNER JOIN [;database=B.accdb].[Catalog] AS b 
ON a.componentid = b.externalid;

I created a VBA function to return both the full "from" bracket (including database file) and also one just returning the path, but I cannot figure out the correct syntax to use VBA functions in the from clause.

"[;database=" + Application.CurrentProject.Path + "\A.accdb]"
"Application.CurrentProject.Path"

1 Answer 1

1

I cannot figure out the correct syntax to use VBA functions in the from clause.

That's because you can't. The path must be resolved before calling the query.

So you have to build the finished SQL string in VBA, then call the query.

That said, wouldn't it be much simpler to just link the tables needed for the current operation, assigning them names as CatalogA and CatalogB?

The (re)linking is easily done in VBA and would free you completely from tampering with the queries.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.