Run query on database open

hiwelcome

Registered User.
Local time
Yesterday, 21:50
Joined
Aug 14, 2015
Messages
47
Hi,

Is there a way to have Access run a query automatically when the database is opened? Background: we have a shared split database that we'd like to give users the capability to work off-network with. There are tables in the back end with employee numbers and such that will be updated from time to time and I'd like the front end try to update its local employee table from the back end table when a network connection is present.
 
You could put code or a macro behind a form's 'On Open' event, running the query. Then you set the form to open when the database opens (under File - Options - Current Database). If you don't want the user to see a particular form when the database opens, set the form to open and then close. Opening will be enough to kick off the query.
 
1. create a public function that will test if there is network connection.
if so run a query that will update the local tables.
to detect if there is connection, try opening one of your linked table, ie:

dim rs as dao.recordset
on error resume next
set rs = dbengine(0)(0).openrecordset("linktablename")
if err.number = 0
'there is connection so run your query.
.. [code to run the query here]
end if
set rs = nothing


2. create an Autoexec macro and call the function that youve created on step 1.
 
Thank you both.

arnelgp, I am pretty new to coding. I have some simple code in my forms tied to buttons, after-update, etc. But I am unsure if you mean something different by a "public" function. Do I use "Insert Module" for this and put the code you wrote there? Or somewhere else?
 
Nevermind, I combined your two suggestions and put that code snippet into the On Open event of a form. Thanks again to both of you.
 

Users who are viewing this thread

Back
Top Bottom