Solved

Access: Trapping an error to see if Excel is open

Posted on 2004-09-24
6
370 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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

707 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

16 Experts available now in Live!

Get 1:1 Help Now