Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Access: Trapping an error to see if Excel is open

Posted on 2004-09-24
6
Medium Priority
?
379 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 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
Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

 
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

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Sometimes MS breaks things just for fun... In Access 2003, only the maximum allowable SQL string length could cause problems as you built a recordset. Now, when using string data in a WHERE clause, the 'identifier' maximum is 128 characters. So, …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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