Opening Excel from Access

danno_mbk

Board Regular
Joined
Sep 6, 2004
Messages
102
Sorry guys I am new to Access, and dont know it as much as Excel.

Is it possible to open a specific Excel workbook in from Access 97 ?

I have a macro running in acess that needs to export to excel to do some calculations, which will automatically run using a macro that starts when teh work book is openend do the do then save and close

Access will then import the file back in and use that and carry on with its own macro..

can anyone help cause there are 3 of us here that are quite confused !

thanks

Danno
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Danno

Could you not do the required calculations in Access?
 
Upvote 0
we have tried , but its doing calculations on various lines, access doesnt like that but excel does it with a little formula really quickly.

We just wanted Access to export a table in excel format, have it open excel and the specific file (once opened the macro will run automatically).

I think that is as far as Access goes, we will then have to run another macro in Access to finish to process off. ??

But if anyone has any ideas as how to run excel within Access it would be helpfull, cause we are seriously stuck !
thanks
 
Upvote 0
Norries right. Could you not do the calculations in Access? In case you can't, use this code behind a command button.
I use this code in my application with no problems. Not sure where I got it from, so can't credit the author.

Private Sub ExportToExcel_Click()
On Error GoTo Err_cmdExportToExcel_Click


Dim stDocName As String
DoCmd.SetWarnings False

stDocName = "Your Query name"
DoCmd.OpenQuery stDocName, acNormal, acEdit
DoCmd.SetWarnings True

'Copy records to rows
'in an existing Excel Workbook and worksheet
'
Dim objXL As Excel.Application
Dim objWkb As Excel.Workbook
Dim objSht As Excel.Worksheet
Dim db As Database
Dim rs As Recordset
Dim intLastCol As Integer

Const conSHT_NAME = "Sheet Name"
Const conWKB_NAME = "Path to Excel.xls"

Set db = CurrentDb
Set objXL = New Excel.Application
Set rs = db.OpenRecordset("Table Name", dbOpenSnapshot)
With objXL
.Visible = True
Set objWkb = .Workbooks.Open(conWKB_NAME)
On Error Resume Next
Set objSht = objWkb.Worksheets(conSHT_NAME)
If Not Err.Number = 0 Then
Set objSht = objWkb.Worksheets.Add
objSht.Name = conSHT_NAME
End If
Err.Clear
On Error GoTo 0
intLastCol = objSht.UsedRange.Columns.Count
With objSht
.Range("A1").CopyFromRecordset rs
End With
End With

Set objSht = Nothing
Set objWkb = Nothing
Set objXL = Nothing
Set rs = Nothing
Set db = Nothing


Exit_cmdExportToExcel_Click:
Exit Sub

Err_cmdExportToExcel_Click:
MsgBox Err.Description
Resume Exit_cmdExportToExcel_Click
End Sub
 
Upvote 0
Danno

What calculations are you actually doing?

If it's on particular 'lines' you could create queries that only return those required.

Tanis

Could you not just use the TransferSpreadsheet method to export a table to Excel?
 
Upvote 0
Thanks for that, I will try the code. We couldnt figure out how to make access do what we wanted, but as I know excel pretty well, we used that instead,.. access is not my strong point !
thanks or your help
 
Upvote 0
I suppose you could. Does the TransferSpreadsheet method give you as much control?
 
Upvote 0
What control does the posted code give?

As far as I can see it exports to a new worksheet in an existing workbook.

That's basically what TransferSpreadsheet does.
 
Upvote 0
Hi Norrie. Yeah, the TransferSpreadsheet method does almost what I want, but I do need to specify a specific worksheet. I don't think the TransferSpreadsheet method does that, or am I wrong?
 
Upvote 0
CopyFromRecordset lets you be a little more specific about where you want to put things than TransferSpreadsheet.

Like you, Norie, I'm thinking they could easily have done the calculations inside Access.

Just in case you guys are interested at all in how to do it in Access -- check out Crosstab queries. You can also create your own summation queries providing averages/min/max values depending on what you need exactly.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,854
Messages
6,162,448
Members
451,765
Latest member
craigvan888

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