Solved

Access VBA Strategy for Testing Existence of Excel Worksheet

Posted on 2014-01-01
4
1,110 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 39

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

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

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Familiarize people with the process of utilizing SQL Server views 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 Access…
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…

920 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