Solved

Access VBA Strategy for Testing Existence of Excel Worksheet

Posted on 2014-01-01
4
1,162 Views
Last Modified: 2014-01-29
Hello ~ Importing data from an Excel worksheet to an Access table using:
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12Xml, "tblRealOne", sFile, True, "Worksheet1$"

NOTE: sFile is a string containing the path and file name of the workbook containing data.

Works perfectly!

However, IF "Worksheet1" is not present, Error #3125 results and the function stops.

Because contents of tblRealOne are deleted BEFORE running the TransferSpreadsheet statement, I need a way to determine if the TransferSpreadsheet will work BEFORE deleting tblRealOne contents.

Right now, I create a tblTest w/ the same TransferSpreadsheet statement to see if it runs, BEFORE deleting existing data and running TransferSpreadsheet on tblRealOne....... (deleting tblTest afterwards).

This strategy works but seems extremely clumsy.

My question: Is there a way to determine if "Worksheet1" worksheet exists prior to running the TransferSpreadsheet statement? - Without opening the spreadsheet file?
OR
Is there a way of testing for an error in the TransferSpreadsheet statement, without actually moving data or creating a table???

I'll be offline shortly but will resume tomorrow.

Thank You!  Jacob
0
Comment
Question by:Chi Is Current
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
als315 earned 300 total points
ID: 39750820
I always prefer link file and import to table with query. In this case you can skip other steps if there is an error during link.
0
 
LVL 6

Assisted Solution

by:ButlerTechnology
ButlerTechnology earned 200 total points
ID: 39750867
The below function will return true if the specified worksheet exists in the workbook.

Public Function WorksheetExists(sPath As String, sSheet As String)
On Error Resume Next
Dim oExcelApp As Object
Dim oWB As Object
Dim oWS As Object
Dim results As Boolean
     
  Set oExcelApp = CreateObject("Excel.Application")
  oExcelApp.Workbooks.Open (sPath)
  Set oWS = oExcelApp.Sheets(sSheet)
  If Err Then
    results = False
  Else
    results = True
  End If

  Set oWS = Nothing
  oExcelApp.Quit
  Set oExcelApp = Nothing
  WorksheetExists = results

End Function

Open in new window

0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 39753161
Thank you both for your replies.

Working with them.


Many thanks ~ Jacob
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 39819981
Thank you for your assistance here.

Since this import function is only used once a month, I decided to stay with discarding the TEST import table, if it exists.  I do appreciate your comments here.

Best Regards, Jacob
0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Disable Outlook Security Warning Window Access 2016 5 51
Access Query on CSV returning number not text 6 37
Signature required on a report 5 33
Access Schema 6 26
This article is a continuation or rather an extension from Cascading Combos (http://www.experts-exchange.com/A_5949.html) and builds on examples developed in detail there. It should be understandable alone, but I recommend reading the previous artic…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

829 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