Creating a COM Component to access an MS-Access Database
In this chapter we will create a component that can be used to perform SQL commands on
databases. The component itself does not necessarily have to be limited to MS-Access
databases. You can use an MS-SQL Server database if you'de like. But, for the puspose of
this demonstration, we will be using a MS-Access database to retrieve data from and
display it via our ASP page.
Rather than explain the theory and an understanding of the technologies we will be using
in our tutorial (such as ADO, ODBC, OLE DB, and SQL), I am going to dive right into the
dirty work and get to it - I'll do some explaining some as we move along.
Before we continue, you will need to make sure you have a few things ready:
Type the following code in the code window:
Public Function Execute(strSQL As Variant, strConn As Variant) As ADODB.Recordset
'Only continue with the method if these two variables
'contain text
If Trim(strSQL) <> "" And Trim(strConn) <> "" Then
'Create a new ADO Connection object
Dim objConn As New ADODB.Connection
'Create a connection to the provider in strConn
objConn.Open strConn
'Make the cursor run on the client - this must be
'specified in order to use a disconnected recordset
'because will be not be connected to the data store
'througout our ASP page
objConn.CursorLocation = adUseClient
'Return a Recordset back to the calling ASP object
'based on the strSQL command(s) provided
Set Execute = objConn.Execute(strSQL)
'Disconnect out Execute object from the strConn
'Connection so we don't get an error
Set Execute.ActiveConnection = Nothing
End If
End Function
Save your project into a location of your liking and name it what you will.
The DataAccess.SQL component is now ready to compile. Click on the
File menu then click on Make DataAccess.dll.... Save the DLL somewhere
on the web server where we will be accessing it from our ASP page.
Now we must create an ASP page that will both create a new record/row with data
in the first three columns of Table1 of the Database.mdb database and retrieve that data
to display out to the clients browser. Create an ASP page called Test.asp somwhere
on your Inetpub\wwwroot directory and place either of the following code examples in it:
Using VBScript:
'Instantiate the COM Component DataAccess.SQL (DataAccess.dll)
Set objDS = Server.CreateObject("DataAccess.SQL")
'Create a new SQL Command that creates a new record/row with data in the first three columns
strSQL = "INSERT INTO Table1 (FIELD1,FIELD2,FIELD3) VALUES ('one','two','three')"
'Specifies the DSN data source name to our Database.mdb access database
strConn = "DSN=MSAccess"
'Execute the SQL Method
objDS.Execute strSQL, strConn
'Retrieve a recordset with the data we just added to the database
Set objRS = objDS.Execute("SELECT * FROM Table1", "DSN=MSAccess")
'While the cursor is not at the end of the table display the contents of F1-F3
Do While Not objRS.EOF
Response.Write("FIELD1:" & objRS.Fields.Item("FIELD1").value & " ")
Response.Write("FIELD2:" & objRS.Fields.Item("FIELD2").value & " ")
Response.Write("FIELD3:" & objRS.Fields.Item("FIELD3").value & " ")
'Move the next record/row in Table1
objRS.MoveNext()
'Do it again until the cursor is at the end of Table1
Loop
'Destroy the Recordset object
Set objRS = Nothing
'Destroy the DataAccess.SQL object
Set objDS = Nothing
Using JavaScript:
//Instantiate the COM Component DataAccess.SQL (DataAccess.dll)
var objDS = Server.CreateObject("DataAccess.SQL");
//Create a new SQL Command that creates a new record/row with data in the first three columns
var strSQL = "INSERT INTO Table1 (FIELD1,FIELD2,FIELD3) VALUES ('one','two','three')";
//Specifies the DSN data source name to our Database.mdb access database
var strConn = "DSN=MSAccess";
//Execute the SQL Method
objDS.Execute(strSQL, strConn)
//Retrieve a recordset with the data we just added to the database
var objRS = objDS.Execute("SELECT * FROM Table1", "DSN=MSAccess")
//While the cursor is not at the end of the table display the contents of F1-F3
while (! objRS.EOF) {
Response.Write("FIELD1:" + objRS.Fields.Item("FIELD1").value + " ");
Response.Write("FIELD2:" + objRS.Fields.Item("FIELD2").value + " ");
Response.Write("FIELD3:" + objRS.Fields.Item("FIELD3").value + " ");
//Move the next record/row in Table1
objRS.MoveNext()
//Do it again until the cursor is at the end of Table1
}
//Destroy the Recordset object
var objRS = null;
//Destroy the DataAccess.SQL object
var objDS = null;
Now, run the Test.asp in a browser. The ASP page will do two things:
1) Update the Database.mdb database with new records which populate the FIELD1, FIELD2, and
FIELD3 fields with data and 2) retrieves that same data and displays it to the client's
browser using the Response.Write method.
I have provided, in a ZIP format, all of the source code, the database, and DLL that we
have produced in this tutorial. Click here
to download the database.zip file.