ADOX, MS Access and ASP

4.5 (2 votes)

You don't need MS Access installed on your computer to create a database file for your ASP web site. Using ADOX and some data definition language allows you to produce an mdb file programmatically.

ADO (ActiveX Data Objects) are used primarily by ASP programmers for manipulating data - that is, retrieving it from data stores to display on web pages, and allowing users to insert or modify data in data stores.

ADO can also be used as a method of passing DDL (Data Defintion Language) commands to create, drop and alter tables and their fields within databases, but what about creating a new Access database, for example, or other schema based activities?

ADOX (ADO Extensions) is the tool for this. The ADOX object model starts with the Catalog object, which is the top-level object. The Catalog object contains a number of objects as properties, each of which are collections. (For example, the Tables property of the Catalog object is a collection of Table objects.) These Catalog object properties include:

  • Tables
  • Groups
  • Users
  • Procedures
  • Views

The Catalog object represents the top level object, schema information about a data store. Here's how to use it to create a new Access .mdb file and add a table with a primary key.

  'Const adVarWChar = 202
  'Const adInteger = 3
  'The above are from and are the ADO Constants 
  'for the two datatypes used in this example.
  Dim objADOXDatabase
  'Instantiate a Catalog object
  Set objADOXDatabase = CreateObject("ADOX.Catalog")
  'Use the Create method, passing the Datasource Provider 
  'and full path and file name as parameters
  objADOXDatabase.Create "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  "Data Source=PathToDatabasecustomers.mdb"
  'The database now exists...
  'Create a database table and name it
  Dim objFirstTable 
  Set objFirstTable = CreateObject("ADOX.Table")
  objFirstTable.Name = "Customers"
  'CustomerID will be of datatype adInteger, for which the ADO Constant is 3
  objFirstTable.Columns.Append "CustomerID", 3
  'CustomerName is of datatype adVarWChar, for which the ADO Constant is 202. 
  'The field size is 30 characters
  objFirstTable.Columns.Append "CustomerName", 202, 30
  'Create a key, passing first the name of the key, 
  'then the type (1 = primary key), 
  'then the column on which it is created
  objFirstTable.Keys.Append "PK_CustomerID", 1, "CustomerID"
  'Add the database table to the database 
  objADOXDatabase.Tables.Append objFirstTable
  response.write "Database created"
  'Clean up...
  Set objFirstTable = Nothing
  Set objADOXDatabase = Nothing

Date Posted:
Last Updated:
Posted by:
Total Views to date: 9295