Solved

Access VBA Strategy for Testing Existence of Excel Worksheet

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
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 …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

624 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