要以程式建立 Access 資料庫,可以使用 ADOX,您必須先在 Delphi 中 Import ADOX type library: Project->Import Type Library,選擇 Microsoft ADO Ex 2.x for DDL and Security (Version 2.x),然後按 "Create Unit"鈕。如果出現下面的錯誤訊息表示類別名稱重複了: "A class named 'xxxxx' is already installed" 你可以在 Class names 清單中直接更改類別名稱。 uses ComObj, ADOX_TLB; ADOXCatalog.CreateNew('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=new.mdb'); 建立 Access 的資料表,取自 Adonis FAQ: var Catalog: ADOX.Catalog; Table: ADOX.Table; Index: ADOX.Index; Catalog := CoCatalog.Create; with Catalog do begin _Set_ActiveConnection('Provider=Microsoft.Jet.OLEDB.4.0;Data Source=database.mdb'); // create table Table := CoTable.Create; with Table do begin Name := 'Contacts'; Columns.Append('Name', adVarWChar, 20); Columns.Append('Phone', adVarWChar, 20); // create index Index := CoIndex.Create; with Index do begin Name := 'IndexName'; PrimaryKey := True; Unique := True; Columns.Append('Name', adVarWChar, 20); end; Indexes.Append(Index, EmptyParam); end; Tables.Append(Table); end; 範例二 (取自 http://afedorov.homepage.com/index.html) : ADOX: Creating new Access Database With the help of ADOX (ADO Extensions for Data Definition and Security) we can create databases on the fly, using simple syntax (comparing with SQL DDL). All we need to do is to create an instance of a Catalog object, that is datastore in ADOX terms and use its Create method to create a new database. Next, we add a table to the Tables collection and required number of Columns (i.e. fields) into the Table. The following example shows how to create a new Access database and a table and two columns into it. It should be mentioned that the current version of ADOX works properly only with Jet OLE DB Provider - other providers have some limitations of functionality. This should be fixed in ADO 2.5 - the version that will come with Windows 2000. Note: Add ADOX_TLB and ComObj units into USES clause. procedure TForm1.Button1Click(Sender: TObject); var Catalog : _Catalog; Table : _Table; BaseName : String; DS : String; begin // Name of the new database file BaseName := 'd:\data\demo.mdb'; // Create a Catalog Object Catalog := CreateCOMObject(StringToGUID('ADOX.Catalog')) as _Catalog; // Set the Connection String DS := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source='+BaseName; // Check if we already have such a file and delete it If FileExists(BaseName) Then DeleteFile(BaseName); // Create new Access database Catalog.Create(DS); // Create a Table Object Table := CreateCOMObject(StringToGUID('ADOX.Table')) as _Table; // Set the name of a table Table.Name := 'Customers'; // Append Table into the base Catalog.Tables.Append(Table); // Now add two columns (fields) into the table // Both are Text fields up to 128 characters Table.Columns.Append('FIRSTNAME', adVarWChar, 128); Table.Columns.Append('LASTNAME', adVarWChar, 128); end; by Alex Fedorov