Excel Module from within Access? (1 Viewer)

vjmehra

Registered User.
Local time
Today, 21:37
Joined
Mar 17, 2003
Messages
57
What I want to be able to do is click a button in Access, for this to export some data into an Excel file and then format the data programmatically in Excel. I have created command buttons in Access and Excel which contain the necessary code for each part, however ideally I want the user to be able to click the button in Access and then for the Access code to run then somehow get the function to run in Excel. Does anyone know if this is possibleor even better how to do it?

Cheers,
Vidal
 
K

Kat001

Guest
Hi Vidal

Yes you can do this in Access. You don't need the command button in Excel for this to work.

Put the following code behine the 'On Click' property of a command button on a form in Access.

This will output the results of a query (called "Query1") in Excel format, to the C:\Temp folder.
For this example, the code will only select Cell "B2" and then save the workbook. You will have to delete these 2 lines of code and add you own.

You must ensure that the 'Microsoft Excel Object Library' is referenced for this code to work.
(This can be found under Tools - References from inside the VBA editor)


Private Sub cmd1_Click()
On Error GoTo Err_Handler

DoCmd.OutputTo acOutputQuery, "Query1", "Microsoft Excel (.xls)", "C:\Temp\Test.xls", False

Dim xlApp As Excel.Application, xlSheet As Excel.Worksheet

Set xlApp = CreateObject("Excel.Application")
xlApp.Application.Visible = False

xlApp.Workbooks.Open "C:\Temp\Test.xls"
Set xlSheet = xlApp.ActiveSheet

' Below, you need to add the code to do whatever is required in the Excel sheet

Range("B2").Select
ActiveWorkbook.Save

' Delete the above 2 lines when you have added your code

xlApp.Application.Visible = True

'Clean up here
Set xlSheet = Nothing
Set xlApp = Nothing
End

Exit_cmd1_Click:
End
Exit Sub

Err_Handler:
MsgBox "Error: " & Err.Number & " - " & Err.Description
Resume Exit_cmd1_Click

End Sub

Hope this is what you wanted.
 

vjmehra

Registered User.
Local time
Today, 21:37
Joined
Mar 17, 2003
Messages
57
Excellent that looks like exactky what I'm after, thanks a lot for your help.
 

Users who are viewing this thread

Top Bottom