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 adovbs.inc 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
  %> 

You might also like...

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

0 Comments

Recent Comments

Robby Robson 15/08/2017 00:43
In response to Routing in Razor Pages
Mike: great stuff. Now that .Core Standard 2.0 is formally out, how soon will you rewrite your book...

Satyabrata Mohapatra 28/07/2017 08:59
In response to Sending Email in Razor Pages
Bit off topic, but congratulation sir for your MVP award. You deserve it !!!...

Satyabrata Mohapatra 23/07/2017 16:43
In response to Razor Pages - The Elevator Pitch
@Dale Severin You can continue to build apps using asp.net web pages....

Satyabrata Mohapatra 23/07/2017 16:40
In response to Sending Email in Razor Pages
Thanks for sharing...learned a lot...

Gfw 22/07/2017 11:53
In response to Sending Email in Razor Pages
Question... Does System.Net.Mail support SSL?...

Dale Severin 20/07/2017 03:38
In response to Razor Pages - The Elevator Pitch
I work with razor web pages extensively. I appreciate the rapid development it permits me to I am as...

Obinna Okafor 14/07/2017 01:19
In response to Routing in Razor Pages
Thank you, Mike. Good post....

Satyabrata Mohapatra 11/07/2017 16:02
In response to Routing in Razor Pages
Very powerful routing system!!...

Cyrus 05/07/2017 03:41
In response to Razor Pages - Getting Started With The Preview
How can I trim packages and services as much as possible to use just razor pages? I don’t want to to...

Harris Boyce 04/07/2017 04:17
In response to Razor Pages - The Elevator Pitch
As a developer of a couple "trivial" web pages applications used by non-profits that wouldn't have I...