Query a Table in another database Syntax Variations (1 Viewer)

sxschech

Registered User.
Local time
Today, 16:51
Joined
Mar 2, 2010
Messages
791
This is more about opinions rather than a specific question.

When I happen to decide to view data in another Access file, I sometimes use and have recommended to others the following syntax:

Code:
SELECT *
FROM tblTableName IN 'C:\Users\Documents\AccessFiles\AnotherFile.accdb';

Yesterday while searching the web on an unrelated topic, after I was finished, decided to click on some of the other articles (such as this one) https://www.databasejournal.com/fea....php/3647466/Queries-On-Steroids--Part-II.htm and came upon a different syntax to achieve the same result:
Code:
SELECT *
FROM [C:\Users\Documents\AccessFiles\AnotherFile.accdb].tblTableName;

Seems like it might be better when constructing it as a query string since it doesn't use quote marks and there is no confusion about IN which is also used in queries for criteria instead of OR. Other than that are they functionally equivalent or is the original method preferred as that is how Access constructs it if using the graphical interface?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 16:51
Joined
Oct 29, 2018
Messages
21,358
Hi. Basically, when you connect to a table, Access needs to know where it is and how to connect to it. This is the function of a connection string. Those two methods you showed were just different ways of passing the connection string to Access, so it know where to find the table you're trying to query. So, yes, I believe they are functionally equivalent. There are probably other ways to do the same thing besides the one you posted.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 19:51
Joined
Feb 19, 2002
Messages
42,981
I have seen this syntax but have never had a need to implement it. I use linked tables and that means that I can use saved querydefs. If I need to point to a different BE, I relink the tables.

Is there some reason that you prefer to do this rather than use linked tables and querydefs which don't have to be changed when the BE changes?
 

Micron

AWF VIP
Local time
Today, 19:51
Joined
Oct 20, 2018
Messages
3,476
This is more about opinions
Great! Then I can't be wrong.

Can't think of a reason why either of these is preferable to linking BE tables. Can you pass a BE password using this string? If not, 1 down vote.

If network paths change (anyone who has worked long enough in a large company environment knows this happens) would I rather batch re-link BE tables or edit every sql statement where this technique is used? 1 down vote.

And then have to release an updated FE version? 1 down vote.
Can't think of an up vote reason.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 18:51
Joined
Feb 28, 2001
Messages
27,001
My question is whether the "other table in another file" is more of an ad-hoc or one-off type of operation, or whether you are more likely to visit this same table but just not very often. If this is a one-off case, I'm not sure that it matters. If this is a "repeated but infrequent" case then I would go for a linked table.

I agree that it might be easier to build the required string in your second example because of no need for quotes. However, to be honest, that is a minuscule reason to choose one vs. another.
 

June7

AWF VIP
Local time
Today, 15:51
Joined
Mar 9, 2014
Messages
5,423
I agree, to read data just set links to tables. However, I have a process to export data to a db and email it to another office on a periodic basis. The IN approach is what I happened to find at the time. Example of code:

Code:
Public Sub ConstructionExtract()
'exports data to ConstructionExtract Access file
'copies file to zip folder
'opens Outlook and attaches file to msg and sends
Dim strZip As String
Dim strExtract As String
strZip = gstrBasePath & "Editing\ConstructionExtract.zip"
strExtract = gstrBasePath & "Editing\ConstructionExtract.accdb"
'delete records from ConstructionExtract tables
CurrentDb.Execute "DELETE FROM Bituminous IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM BituminousMD IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM Concrete IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM Emulsion IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM PGAsphalt IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM SoilsAgg IN '" & strExtract & "'"
CurrentDb.Execute "DELETE FROM SampleInfo IN '" & strExtract & "'"
'insert records into ConstructionExtract tables
CurrentDb.Execute "INSERT INTO Bituminous IN '" & strExtract & "' SELECT * FROM ConstructionBIT;"
CurrentDb.Execute "INSERT INTO BituminousMD IN '" & strExtract & "' SELECT * FROM ConstructionBMD;"
CurrentDb.Execute "INSERT INTO Concrete IN '" & strExtract & "' SELECT * FROM ConstructionCONC;"
CurrentDb.Execute "INSERT INTO Emulsion IN '" & strExtract & "' SELECT * FROM ConstructionEMUL;"
CurrentDb.Execute "INSERT INTO PGAsphalt IN '" & strExtract & "' SELECT * FROM ConstructionPG;"
CurrentDb.Execute "INSERT INTO SoilsAgg IN '" & strExtract & "' SELECT * FROM ConstructionSA;"
CurrentDb.Execute "INSERT INTO SampleInfo IN '" & strExtract & "' SELECT * FROM ConstructionSampleInfo;"
'create empty zip folder
'found this on web, no idea what the Print line does but if it isn't there, this won't work
Open strZip For Output As #1
Print #1, "PK" & Chr$(5) & Chr$(6) & String(18, 0)
Close #1
'copy file into zip folder
Dim objApp As Object
Set objApp = CreateObject("Shell.Application")
'variable for source file doesn't seem to work in this line
'also double parens not in original example code but won't work without
objApp.NameSpace((strZip)).CopyHere gstrBasePath & "Editing\ConstructionExtract.accdb"
'open Outlook, attach zip folder, send e-mail
Dim appOutLook As Outlook.Application
Dim MailOutLook As Outlook.MailItem
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
With MailOutLook
    .BodyFormat = olFormatRichText
    .To = "email here"
    ''.cc = ""
    ''.bcc = ""
    .Subject = "Central Materials Laboratory Data"
    .HTMLBody = "Construction data extract: " & Now
    .Attachments.add (strZip)
    .DeleteAfterSubmit = True 'to not retain in sent folder
    .Display
    ''.Send
End With
'delete zip folder
Kill strZip
CurrentDb.Execute "UPDATE Updates SET ConstructionExtract=#" & Date & "#"
End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom