Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Access: Trapping an error to see if Excel is open

Posted on 2004-09-24
6
Medium Priority
?
378 Views
Last Modified: 2010-07-27
Hello again experts,

Now I'm trying to learn how to trap errors in Access while loading Excel files.  

What happens:
A user types in search criteria in a textbox, it is passed to a query that creates a table, and then the table is passed to Excel, and then Excel is loaded.  If I have already run the code once and the excel file is open, however, I get:
Run-time Error '3010' table already exists ...but everything works fine if the Excel file isn't already loaded.

-------------
DoCmd.SetWarnings False
DoCmd.OpenQuery "qryKSB1SearchMakeTable", acViewNormal, acReadOnly
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblKSB1Search", "\\Jess\DB Project\search_output.xls", False, ""
DoCmd.SetWarnings True
Dim XL As Object
Set XL = CreateObject("Excel.Application")
XL.Workbooks.Open "\\Jess\DB Project\search_output.xls"
XL.Application.Visible = True
-------------

Is there some generic code I can use to see if the Excel file is already loaded?  If so, I'm thinking it either needs to be closed and reopened unless someone has a fancier idea...

Thanks,
Jess
0
Comment
Question by:jesselavaca
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Assisted Solution

by:Guru Ji
Guru Ji earned 200 total points
ID: 12146519
Try to do this

Before you even try to create the same table put this command before it
On Error Resume Next
DoCmd.RunSQL "DROP TABLE tbl1"

OR try this below

If Err.Number = 3010  Then OR If Err.Number = '3010' Then
On Error Resume Next
DoCmd.RunSQL "DROP TABLE tbl1"

let me know if this works, now that error won't trap at all and your code should run smoothly
0
 
LVL 11

Expert Comment

by:Guru Ji
ID: 12146592
Of you can do this

If Err.Number = 3010 then

msgbox "The Excel is open"

End if
0
 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 600 total points
ID: 12146822
The problem you are experiencing is due to Excel having the file open already.  You cannot export to or overwrite a file currently open for modification by any application.  Your only resolution is to ensure no app has the file open when this procedure runs.  There are several ways to accomplish this:

1) You are using the object variable XL to create the Excel application.  If this were a module-level object, or at least a STATIC object in the sub, you can have the code check for its existance and status, and close it if necessary.  Remember that without STATIC, XL will no longer be associated with your open Excel application once the sub ends, meaning you have no control over it outside the current instance of the sub.
2) Try to delete the file before exporting.  If an error is returned indicating anything besides "File not found", notify the user there has been an error and they should make sure Excel is closed before trying again.
3) Trap for the error as write2mohit has suggested (you will need to experiment to find all the errors possible for your setup), and create a "serial" filename.  For example, the first file is search_output.xls.  If that file exists (check using the Dir() method), you can try search_output1.xls.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 51

Assisted Solution

by:Steve Bink
Steve Bink earned 600 total points
ID: 12146830
One last idea:

You can have your Excel object open the file as read-only.  This may bypass Access' need to have modification rights to the file before exporting, but I think one of the other options will be better for you.
0
 
LVL 6

Accepted Solution

by:
Plamodo earned 1200 total points
ID: 12147198
Routinet's last response is exactly how I got around the same problem.  I open the excel object as read-only, and from there, any number of users can open the same excel object over and over without error.

xlObject.Workbooks.Open afilename, False, True, ,"password"

The 'True' is for readonly.. the 'False' is for update links or not.

I originally side-stepped this problem by seeing if Excel was open *at all* .. and if it was, I'd display a message to close Excel first - but after a while it became obvious people needed to have multiple spreadsheets, word files, etc open.  But, in case you want to do that, here's the code I used:

Function isMSExcelRunning() As Boolean
    Dim myExcelapp As Excel.Application

    On Error GoTo HandleErr

    Set myExcelapp = GetObject(, "Excel.Application")

    isMSExcelRunning = True
    Exit Function

HandleErr:
    Select Case Err.Number
        Case 429 'ActiveX component can't create object
            isMSExcelRunning = False
            Exit Function
        Case 4601 'Active X cannot create object
            isMSExcelRunning = False
            Exit Function
                   
        Case Else
            isMSExcelRunning = False


    End Select
End Function


So before you load the excel file, you can have an 'if' statement like this:

if isMSExcelRunning = true then
    'open excel, etc
else
    'do something else
endif


I find the read-only option to be *much* more useful on the whole... and dynamic to all user's different needs.
0
 
LVL 1

Author Comment

by:jesselavaca
ID: 12161694
Sorry for the delayed response, this bit of work was put on the back burner temporarily...

but I went for the Read-Only option, it met the need of letting the workbook be overwritten by running the search/output more than once while the workbook was open...and as a side benefit doesn't allow the user to alter the workbook and forces them to save output themselves locally.  

Great job again, experts!

-Jess
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question