Solved

Access: Trapping an error to see if Excel is open

Posted on 2004-09-24
6
372 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
6 Comments
 
LVL 11

Assisted Solution

by:Guru Ji
Guru Ji earned 50 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 50

Assisted Solution

by:Steve Bink
Steve Bink earned 150 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 50

Assisted Solution

by:Steve Bink
Steve Bink earned 150 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 300 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

919 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now