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
Solved

Access: Trapping an error to see if Excel is open

Posted on 2004-09-24
6
374 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
Familiarize people with the process of utilizing SQL Server stored procedures 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 Micr…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

809 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