ADO OpenSchema with Excel and Duplicate table names

Posted on 2003-03-25
Medium Priority
Last Modified: 2012-06-21
I'm currently using ado to get the list of worksheets in a workbook and populating it in a listbox.

The code is as follows:

Dim cn as Connection
Dim rs as RecordSet

   Set cn = New ADODB.Connection
   cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyFile.XLS" & _
                   & ";Extended Properties=Excel 8.0"
   Set rs = cn.OpenSchema(adSchemaTables, Array(Empty,Empty, Empty, "Table"))


   While Not rs.EOF
       List1.AddItem rs.fields("TABLE_NAME").Value
   Set rs = Nothing
   Set cn = Nothing

The problem is that with some excel books i something like the following results:


I looked at the workbook itself and these sheets are only in there once so my questions are as follows:

1)  How do I only retrieve the list of actual worksheets in a workbook?

2)  Where are the duplicate sheet names coming from?  What caused this?

I know that I can of course that I can check to see if the sheet name exists before adding it to the list, but I just sort of curious as to what is going on.
Question by:leavinmate
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
  • 2

Expert Comment

ID: 8204415
Its not using ADO but here is a way to get list of all the sheets in a list box.

Refernece the Excel Library

Dim AppExcel As Excel.Application
Dim wSheet As Worksheet
Set AppExcel = CreateObject("Excel.Application")
AppExcel.Visible = False
'Open file
Workbooks.Open FileName:= "C:\Myfile.xls"

For Each wSheet In ActiveWorkbook.Worksheets
   List1.AddItem wSheet.Name

Set wSheet = Nothing
Set AppExcel = Nothing

B)  I am not sure on this but they aren't duplicte sheets, what your code is doing is getting a list of all table names in Workbook.   Excel has hidden stuff that it stores besides what you can see.  
Like when I ran your code I have Sheet1$
So all my Print_Area information is stored under that table name.

Author Comment

ID: 8204510
And is there any way to query via ADO to not show this extra stuff?  I would think that you would be able to exclude these.

Also, what exactly is the extra stuff?  Is there any documentation on this somewhere?

I don't want to use the Excel object as I can't guarantee that every machine that this app will run  on will have Excel installed.  (And I don't want to create an installation package for this small utility.)


Accepted Solution

YeaYuh earned 140 total points
ID: 8209769
You can just put in some checking, I have been messing around with just getting the sheets and I don't think there is a way to get them with out the others.

As far as I can figure out is its a way to reference stuff in Excel, but I haven't really found any information about it on the web.

Dim cn As Connection
Dim rs As Recordset
Dim dollarInt As Long
Dim lengthInt As Long
  Set cn = New ADODB.Connection
  cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyFile.xls;Extended Properties=Excel 8.0"
  Set rs = cn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, "Table"))

  While Not rs.EOF
      dollarInt = InStr(rs.Fields("TABLE_NAME").Value, "$")
      lengthInt = Len(rs.Fields("TABLE_NAME").Value)
      If (dollarInt + 1) = lengthInt Then
         List1.AddItem rs.Fields("TABLE_NAME").Value
      End If
  Set rs = Nothing
  Set cn = Nothing

Author Comment

ID: 8215088
Thanks YeaYuh.  That's pretty much what I wound up doing....except I using the following check:

While Not rs.EOF
  If Right$(rs.Fields("TABLE_NAME").Value, 1) = "$" Then
     List1.Additem rs.Fields("TABLE_NAME").Value
  End If

I gave you the points though.

Thanks for the assistance.

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

764 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