Excel Crashes when opened from Access

P

Phil Stanton

I am using the following code (Office 2000) and opening Excel from a command
button on a form in am Access Database

Private Sub Storage_Click()

On Error GoTo Err_Storage_Click

Dim ExcelApp As Object
Dim ExcelWasNotRunning As Boolean ' Flag for final release
Dim MyDb As Database

On Error Resume Next
Set ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = True

If Err.Number <> 0 Then ExcelWasNotRunning = True
Err.Clear ' Clear Err object in case error occurred.

' Check for Microsoft Excel. If Microsoft Excel is running,
' enter it into the Running Object table.
DetectExcel

' Set the object variable to reference the file you want to see.
Set ExcelApp = GetObject("C:\Documents and Settings\Phil\My
Documents\Access\MDB\WFYC\Storage.XLS")

' Show Microsoft Excel through its Application property. Then
' show the actual window containing the file using the Windows
' collection of the ExcelApp object reference.
ExcelApp.Application.Visible = True
ExcelApp.Parent.Windows(1).Visible = True

Exit_Storage_Click:
Exit Sub

Err_Storage_Click:
MsgBox Err.Description
Resume Exit_Storage_Click

End Sub

Sub DetectExcel()
' Procedure detects a running Excel and registers it.
Const WM_USER = 1024
Dim hWnd As Long
' If Excel is running this API call returns its handle.
hWnd = FindWindow("XLMAIN", 0)
If hWnd = 0 Then ' 0 means Excel not running.
Exit Sub
Else
' Excel is running so use the SendMessage API
' function to enter it in the Running Object Table.
SendMessage hWnd, WM_USER + 18, 0, 0
End If

End Sub

This opens up the Excel application OK (It is using data in the database) If
I click on pretty much anything in the Excel application it immediately
crashes. If I click on something in Access immediately after Excel opens,
then return to Excel - I get a message that the ODBC Microsoft Access Driver
Login Failed and the data fails to update.
If I open Excel with the Access DB closed, everything works perfectly.
If, with the database open, I manually start the Excel program, again
everything is OK

Would appreciate advice

Thanks

Phil
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top