Solved

Access VBA Strategy for Testing Existence of Excel Worksheet

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

708 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

12 Experts available now in Live!

Get 1:1 Help Now