Solved

Access VBA Strategy for Testing Existence of Excel Worksheet

Posted on 2014-01-01
4
1,188 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
[X]
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
  • 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
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…
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 …

749 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