Linked queries in Access

Back to the (Microsoft) Office...Today's cunning tip is that of linking queries between two Access databases.

It is easy to link tables in Access; that is to say that your Access application can use, edit, add and delete data that physically exists in a different Access application's tables. This allows you to have multiple independent front-ends for the same background data for instance, gain the ability to develop new forms and reports whilst other people are using existing ones, or have an application that uses data from two different databases. Do this via the "Link table" option you get, when you create a new table (Click the Insert menu, then Table). However at first sight it isn't possible to do this for queries.

You can import queries, sure (File menu, then Get External Data, then Import), but this creates a separate copy of the query in the second database in your database. This is a waste of space and more importantly risks confusion desynchronisation if one of the queries is changed and the supposed replica is not. It is the equivalent of an imported table, not a linked one.

When you know how, it is actually easy to create the equivalent of a directly linked query without knowing any advanced jibberish. There is a query property called "Source database" which allows you to get information from another database's query for your own query. So to make the equivalent of a linked query, you want to link to that database and command Access to give you all the data that query generates. Do it like this:

  1. In the database you're currently working in, select the Insert menu, then Query. Click OK to go into design view. When it shows you the tables and queries in the current database, just click Close because you're only interested in queries in another database.
  2. In the resulting blank query screen, if you cannot already see the Query Properties window, go into the View menu and select Query Properties. One of the properties is called "Source database" and is almost certain to say (Current) at present. Simply type the path to the database you wish to get a linked query from, as in the example pictured on the below.
  3. The "Source database" propertyThe "Source database" property

  4. Chose the Query menu, and select Show Table. The resulting screen will give a list of all the tables and queries in the remote database.
  5. Select the query you want to link to, and click Add.
  6. Now drag the * from the query now shown on the query design screen into the first Field row to ensure that all the data is displayed as below. You can test it by running the query via the Query menu by choosing Run.
  7. Selecting to show all the fieldsSelecting to show all the fields

  8. Save this query, probably using the same name as the query in the remote database to help you remember what it is.

Voila, a linked query that updates as soon as the query in the other database is altered.

SQLGeekness: This sort of query can also be written dynamically in VBA etc. via SQL. You can use the "IN" keyword to let the computer know what database to search for. As an example, the equivalent to the query pictured above can be written as:
SELECT Query1.* FROM Query1 IN 'C:\Documents and Settings\Adam\Desktop\db1.mdb';


Comments

Performance degradation?

What is the performance degradation of this method (e.g. when the database with the query in on a LAN) compared to having a local copy of the query. If there is a performance degradation is way (using VBA) of copy the query from the remote to the local database automatically (i.e. something that can be done during db startup)

Hi, I don't have any facts

Hi,

I don't have any facts and figures to tell on that I'm afraid, but in my experience it is definitely slightly slower to open for the first time or to put into design/SQL view. However after its initial opening things seem to be largely back up to speed - I guess there is some caching (but it's only a guess, I don't know much at all about internal Access workings!). I've never tried it on a database used by huge numbers of users, but on a db used by say 10 users that is maybeb 200-300 mb big the difference is probably at most say 3 seconds upon first open (which is a lot when you're waiting for it), and probably less than 1 second if anything upon subsequent uses. However example was comparing two databases that both operate from the same LAN - I guess depending on the speed of your LAN you might likely find just running a single non-linked database to be a slower option than a local copy running right off your hard disk serving just you.

You could certainly copy things over on startup - on the form Load event for instance if you're using forms. If it's just the data you want, you could do a Make Table query using the linking method which would then make a local (static) copy of the data the remote query would produce, which would update itself when ever you restarted the database - or you could tie it to a button for manual updates.

If you need to copy the actual query objects (querydef in VBA) over, then check out the docmd.transferdatabase command, using the acQuery object type.

Hope that helps!

linking 2 queries in one database

Hey
I know I am not suppose to post a question here, but the information above gives information on how to link queries between 2 databases. I would like to know if its possible to link 2 queries in the same database. And if its possible then please tell me how?. I really need to link 2 queries in the same database, but if its not possible then please tell any alternative solution.

Please answer fast.

Thanks

Hey, if I'm not

Hey, if I'm not misunderstanding, yes, you can link queries in the same database. Just start a new, 3rd, query in the query designer, and where it asks you which tables to include, you can just choose the tab for queries. Pick the 2 queries you want to combine and put in any appropriate links. Good luck!