Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / Database Access / April 2007



Tip: Looking for answers? Try searching our database.

read data from excel with unknown sheet name

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jan Lorenz - 26 Apr 2007 13:26 GMT
Hi,

when i read some data from a excel-file via ado, then I can select the sheet
over the sheet name: "SELECT  * FROM [" + SheetName + "]". So I must know
the name of the sheet.
It is possible to read the data, when I don't know the name of the sheet?
For instance: read data from the first sheet in the excel file?

regards
Jan
Paul Clement - 26 Apr 2007 14:47 GMT
¤ Hi,
¤
¤ when i read some data from a excel-file via ado, then I can select the sheet
¤ over the sheet name: "SELECT  * FROM [" + SheetName + "]". So I must know
¤ the name of the sheet.
¤ It is possible to read the data, when I don't know the name of the sheet?
¤ For instance: read data from the first sheet in the excel file?

Well yes you have to know the name of the Sheet. However, it is possible to retrieve the names of
all Excel Worksheets in a Workbook (in alphabetical order).

Dim cnn As New ADODB.Connection
Dim rsSchema As New ADODB.Recordset

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=C:\Test Files\Book20.xls;Extended Properties=Excel 8.0;"

Set rsSchema = cnn.OpenSchema(adSchemaTables, Array(Empty, Empty, Empty, Empty))

While Not rsSchema.EOF
   
   Debug.Print rsSchema.Fields("TABLE_NAME").Value
   rsSchema.MoveNext

Wend
 
rsSchema.Close
cnn.Close

Now if you want the Worksheets in the order that they appear in the Workbook (instead of
alphabetical) you have to use DAO:

Dim xlWB As dao.Database
Dim tbl As dao.TableDef
Dim strFirstSheetName As String

Set xlWB = OpenDatabase("C:\Test Files\Book20.xls", False, True, "Excel 8.0;")

'Below returns name of first Worksheet in the Workbook
'Debug.Print xlWB.TableDefs(0).Name

'All Workbooks by ordinal position
For Each tbl In xlWB.TableDefs
   Debug.Print tbl.Name
Next tbl

xlWB.Close

Paul
~~~~
Microsoft MVP (Visual Basic)
Jan Lorenz - 26 Apr 2007 15:41 GMT
thanks a lot :)

> ¤ Hi,
> ¤
[quoted text clipped - 54 lines]
> ~~~~
> Microsoft MVP (Visual Basic)
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.