Controlling Excel from Access

DeutchBose

Board Regular
Joined
Mar 22, 2004
Messages
83
I know there has to be a way to do this:

From a Microsoft Access Module or Macro, I want to go into an excel spreadsheet and refresh a query.

Any ideas on where I would start?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Something like this, perhaps?

Code:
    Set xlApp = CreateObject("Excel.Application")
    Set xlWorkbook = xlApp.workbooks.Open("filename.xls)
    Set xlSheet = xlWorkbook.Sheets("MySheet")

    xlSheet.QueryTables("myQueryTable").Refresh

The cool thing is, once you have xlApp defined, all of the objects, methods, properties, etc. associated with Excel become available to you.


You can also, in the VBE for Access, go to Tools --- References and check the Microsoft Excel libraries. Then, you don't need the createobject piece. But, if you are using your database amongst various users who have various versions of Excel, you could run into problems (i.e. Excel 9.0 or 10.0 library?) So, I chose to do it this way.

HTH,
Mike.
 
Upvote 0
You can also, in the VBE for Access, go to Tools --- References and check the Microsoft Excel libraries. Then, you don't need the createobject piece.
I don't think that is quite right.

The advantage of checking the libraries is that you will have access to the dropdowns that list properties and methods for Excel objects.
 
Upvote 0
If you have the reference checked, you can create a new instance just by using:

Dim xlApp as New Excel.Application

as in:

Code:
Sub test()
    Dim xlApp As New Excel.Application
    xlApp.Visible = True
    xlApp.Workbooks.Add
End Sub

Again, I don't recommend using it for any situation where there is a potential difference in versions, and this is why I didn't provide the details in my earlier post. But it does work, and I have used it in the past for other solutions.

Please, Norie, don't think me rude. I just want to provide thorough information.

Mike.
 
Upvote 0

Forum statistics

Threads
1,221,834
Messages
6,162,268
Members
451,758
Latest member
lmcquade91

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top