Hi,
I've been part of Tek-Tips for quite a while and I think I may have posted only a few times in the past. Most of the things I learn are from reading answers after searching for them. However, I couldn't find an ansewr for this one.
I have the following code that's only partially modified from its original state. Its from Derek Smyth's Software Development Blog at:
I've searched all over the net as good as I could and all the examples I find are like this code where it will show the worksheet names as a messagebox. Messageboxes are nice, but for my ideas, I would like to be able to just append the worksheet names via an Access query into a table. I will also accept just doing away with the "MsgBox adoTable.Name" and replacing it with something that adds it into a table.
Does anyone know how I could write this as a query, SQL statement, or modifying the module? Again, I only need the worksheet names and I can't figure out the code to do this. I want to use the table as a source for a listbox and allow the user an easier way to upload from a spreadsheet into the same Access table (rather than going through the Import dialog boxes, the code would be a Transferspreadsheet once it can read the listbox's values).
I will gladly appreciate any help you can offer. And I did try emailing from Derek Smyth's blog, but its been more than a few days and my email was never returned.
Hre's the code I have so far...
Public Sub GetWorkbooksSchema()
'The following is code found on Derek Smyth's Development Blog
'Find it at 'the name of the workbook, the workbook must be saved prior to accessing it using ADO
Dim sWorkbook As String
sWorkbook = "E:\NameofExcelWorkbook.xls"
'connection object needed to connect to Excel/Access
Dim adoConnection As New ADODB.Connection
'connection string to Excel/Access
adoConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sWorkbook & _
";Extended Properties=""Excel 8.0;HDR=Yes;"""
'open the connection
adoConnection.Open
'treat the workbook as a database catalog, the catalog at the connection
Dim adoWbkAsDatabase As New ADOX.Catalog
adoWbkAsDatabase.ActiveConnection = adoConnection
'get the catalogs tables, which in this case is the worksheets
Dim adoTables As ADOX.Tables
Set adoTables = adoWbkAsDatabase.Tables
'for each table/sheet in the catalog/workbook display the name
Dim adoTable As ADOX.Table
For Each adoTable In adoTables
MsgBox adoTable.Name
Next
'close the connection
adoConnection.Close
'tidy up
Set adoConnection = Nothing
Set adoWbkAsDatabase = Nothing
Set adoTables = Nothing
Set adoTable = Nothing
End Sub
Thanks for your time,
Scootman
I've been part of Tek-Tips for quite a while and I think I may have posted only a few times in the past. Most of the things I learn are from reading answers after searching for them. However, I couldn't find an ansewr for this one.
I have the following code that's only partially modified from its original state. Its from Derek Smyth's Software Development Blog at:
I've searched all over the net as good as I could and all the examples I find are like this code where it will show the worksheet names as a messagebox. Messageboxes are nice, but for my ideas, I would like to be able to just append the worksheet names via an Access query into a table. I will also accept just doing away with the "MsgBox adoTable.Name" and replacing it with something that adds it into a table.
Does anyone know how I could write this as a query, SQL statement, or modifying the module? Again, I only need the worksheet names and I can't figure out the code to do this. I want to use the table as a source for a listbox and allow the user an easier way to upload from a spreadsheet into the same Access table (rather than going through the Import dialog boxes, the code would be a Transferspreadsheet once it can read the listbox's values).
I will gladly appreciate any help you can offer. And I did try emailing from Derek Smyth's blog, but its been more than a few days and my email was never returned.
Hre's the code I have so far...
Public Sub GetWorkbooksSchema()
'The following is code found on Derek Smyth's Development Blog
'Find it at 'the name of the workbook, the workbook must be saved prior to accessing it using ADO
Dim sWorkbook As String
sWorkbook = "E:\NameofExcelWorkbook.xls"
'connection object needed to connect to Excel/Access
Dim adoConnection As New ADODB.Connection
'connection string to Excel/Access
adoConnection.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sWorkbook & _
";Extended Properties=""Excel 8.0;HDR=Yes;"""
'open the connection
adoConnection.Open
'treat the workbook as a database catalog, the catalog at the connection
Dim adoWbkAsDatabase As New ADOX.Catalog
adoWbkAsDatabase.ActiveConnection = adoConnection
'get the catalogs tables, which in this case is the worksheets
Dim adoTables As ADOX.Tables
Set adoTables = adoWbkAsDatabase.Tables
'for each table/sheet in the catalog/workbook display the name
Dim adoTable As ADOX.Table
For Each adoTable In adoTables
MsgBox adoTable.Name
Next
'close the connection
adoConnection.Close
'tidy up
Set adoConnection = Nothing
Set adoWbkAsDatabase = Nothing
Set adoTables = Nothing
Set adoTable = Nothing
End Sub
Thanks for your time,
Scootman