Article ID: 303814 - Last Review: July 13, 2004 - Revision: 2.2 How To Use ADOX with Excel Data from Visual Basic or VBAThis article was previously published under Q303814 On This PageSUMMARY
The ActiveX Data Objects Extensions for Data Definition Language and Security (ADOX) gives ActiveX Data Objects (ADO) developers the capabilities that are present in Data Access Objects (DAO) to manipulate databases, tables, fields and other database objects programmatically. Since the release of Microsoft OLE DB Provider for Jet version 4.0, which supports the ISAM drivers that the Jet engine uses with desktop databases other than Microsoft Access, ADO developers have been able to use both ADO and ADOX with Microsoft Excel workbooks and worksheets. However, because an Excel worksheet is not an ordinary database, ADO has some limitations when it is used with Excel. In particular, many ADOX methods either do not work or give unexpected results when they are used with Excel. This article documents many of the limitations of ADOX and Excel. For additional information about the use of ADO with Excel, click the article number below to view the article in the Microsoft Knowledge Base: 257819
(http://support.microsoft.com/kb/257819/EN-US/
)
How To Use ADO with Excel Data from Visual Basic or VBA
This article is divided into the following sections:
MORE INFORMATIONADOX CatalogsIn the ADOX object model, the Catalog object represents a database or, in the case of Excel, a workbook.
Creating a CatalogYou cannot use the Create method of the ADOX Catalog object to create a new Excel workbook. If you try to do so, you receive the following error message:
Operation is not supported for this type of object.
ADOX TablesThe ADOX Table object corresponds to either of the Excel objects (that is, a worksheet or a range) that can serve as a container for data.Creating a TableYou can use ADOX to create new Tables in Excel. Because you cannot create a Table without also creating Columns, the code sample appears in the "ADOX Columns" section.
Deleting a TableYou cannot use ADOX to delete a table (worksheet) or named range in Excel, whether or not ADOX created it.
ADOX ColumnsADOX columns correspond to the columns or fields in a database and neatly correspond to the columns of an Excel worksheet.ADO Data Types Used with ExcelADO recognizes six data types in an Excel datasource that you can use to create columns:
Columns in General
Creating Columns When Creating a TableCreating columns works best when it is done at the same time that a new table is created using ADOX.The following code creates a new table (worksheet) that contains two columns (one numeric and one text): Creating Columns in an Existing WorksheetAppending columns to a pre-existing, empty worksheet generates unusual results. When the first ADOX column is appended to the table's Columns collection, ADOX first creates a column header named F1 in Column A of the worksheet and then creates the new ADOX column header in Column B of the worksheet. Thus, ADOX code that appends two new columns results in three column headers. If you try to delete this unwanted, first column from the Columns collection before you append the first ADOX column, it appears to succeed, but the unwanted column is created nonetheless.Deleting ColumnsYou cannot delete a column from an ADOX table in Excel.
Changing the Data Type of an Existing ColumnAfter a column has been appended to a table, if you try to set its .Type property to a different data type, you receive the following error message:
Operation is not allowed in this context.
Adding Data to Excel Tables and ColumnsWhen you are testing ADO code with Excel in the Visual Basic design environment, the first time that you run a recordset operation, you may receive the following error message:
Selected collating sequence not supported by the operating system.
246167
(http://support.microsoft.com/kb/246167/EN-US/
)
PRB: Collating Sequence Error Opening ADODB Recordset the First Time Against an Excel XLS
Adding Data to Excel Columns That Are Created by ADOXWhen you use ADO to insert or modify data in the tables and columns that you have created in Excel using ADOX, ADO respects the data types that you specified for those columns, although it is not clear where this information is stored.
Adding Data to Ordinary Excel ColumnsWhen you use ADO to insert or modify data in Excel worksheets or ranges that are not created or modified using ADOX, no restrictions are enforced on the type of data that you can insert.
Using SQL CREATE, ALTER, and DROP StatementsYou can run Microsoft SQL Data Definition Language (DDL) statements such as CREATE, ALTER, and DROP against Excel database objects over an ADO Connection to yield the same results as the corresponding ADOX methods that are described above.Conclusion
| Article Translations
|
Back to the top
