?
Solved

ADO OpenSchema with Excel and Duplicate table names

Posted on 2003-03-25
4
Medium Priority
?
2,635 Views
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"))

   List1.Clear

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

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

Sheet1$
Sheet1$_
Sheet2$
Sheet2$_

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.
0
Comment
Question by:leavinmate
  • 2
  • 2
4 Comments
 
LVL 3

Expert Comment

by:YeaYuh
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
Next

Workbooks.Close
Set wSheet = Nothing
AppExcel.Quit
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$
                                 Sheet1$Print_Area
So all my Print_Area information is stored under that table name.
0
 
LVL 1

Author Comment

by:leavinmate
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.)

0
 
LVL 3

Accepted Solution

by:
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"))
 
  List1.Clear

  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
      rs.MoveNext
  Wend
 
  rs.Close
  Set rs = Nothing
  cn.Close
  Set cn = Nothing
 
0
 
LVL 1

Author Comment

by:leavinmate
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
WEnd

I gave you the points though.

Thanks for the assistance.
0

Featured Post

Industry Leaders: 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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month8 days, 21 hours left to enroll

621 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