Excel - Refresh an Access Query using VBA code in Excel

Asked By Peter G on 26-Oct-09 12:25 PM
I have linked data in an excel workbook from a tabel in an Access database
The table has been created from a Make tabel query in the same database. For
the data in the Access table to be up to date I need to run the Access query.
Can somone suggest a suitable VBA code for me to run in Excel that will
connect to the Access database and run (refresh) the query.

Patrick Molloy replied on 26-Oct-09 12:44 PM
set a reference to the Microsoft Active data Obkject 2.6 Library

Dim con As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim SQL As String

MyFile = "Risk.mdb"
SQL = "qsecurities"  ' this is th ename of the query
con.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & MyFile
rst.Open SQL, con, adOpenStatic
Peter G replied on 27-Oct-09 07:59 AM
Set the  reference to the Microsoft ActiveX data as you suggested, entered
the path and name of my mdb-file and also the name of the query. Ran the code
but no readcion in my Access database.

-Does the code you have suggested run the query in the backgound?
-The Access make table query requires some interaction confirming that data
is to be overwritten, a new fil will be created etc, does your code
automatically conform all the questions?
Is it necessary to "close down" Access after runnuing the query?

Patrick Molloy replied on 27-Oct-09 09:43 AM
Hi ... this does not open the access database as if you were using Microsift
Access. Its opens a connection to the database that allows to you to run TSQL
statements - and these can be the usual --- data retrieval, inserting amendig
etc, or commands that do things likeadding tables, or addign columns to
tables, and so on. It cannot be interactive as the database would normally be
in a server room and not in your own pc.
As it only opens a connection, there is no Access Application to close. Note
though that if you do have Access open with this database, then you might
inadvertently put a lock on the table if you have it open, Locks prevent two
or more users amendign the same record in a table. Locks also occur if, as
can easily happen with Access, the op edits a table. Another application,
like Excel, running sql against the database will not be able to read the
table while its being edited...and it will wait for euther a specified tome
out, or a default time out, before returning the code to the applicatios
Peter G replied on 27-Oct-09 11:51 AM
This is a little strange! Thanks for your explanations regarding
interactivity, locking etc.
I have still not had any luck with the code. I have inserted a breakpoint in
the code and checked that the values of the variables (path, sql text) are
correct. I have tried running the code with Access running and also with it
closed down. Can you give me some suggestions how to try to trace what I am
doing incorrectly?

Peter G replied on 27-Oct-09 01:08 PM
Still trying to get this coed working and have arrived at the following,
which perhaps helps

1. The "Open.con" line needed a semi-colon at the end of code line.
2. If I replace the varialble sql with "SELECT Levfakt.Levnummer,
Levfakt.Fakturadat INTO AAA_Tabell FROM Levfakt" instead of the name of the
Access Query the code will actually create a table and fill it with data,
however the second time I run the code I get an error advising that the table
alread exists.

Any suggestions regarding the Query name issue?