Home Search Contact us About us
Title Appending to database using ADO and VBScripts
Summary This note shows simple methods to append records to a database using the ADO interface with VBScript.
Contributor John McTainsh
Published 19-Oct-2000
Last updated 19-Oct-2000
Page rating   93% for 3 votes Useless Brilliant

Download demo ASP and MDB files - 12 Kb

Description.

The Microsoft¢ÎActiveX¢ÎData Objects (ADO) object model defines a collection of programmable objects that can be used in any language that supports both COM and Automation to access Databases. In this example we will be using ADO to access a Microsoft Access Database.

Note: I have been working on ADO for 5 days now and this is what I have learnt in this time. Also remember you need Microsoft IIS running on the server since this is all server side processing.

How to use it.

Start as always by inserting <%@ LANGUAGE = VBScript %> at the top of the page. The file I will be working with is called TestADO.mdb. When setting these demos up for yourself de sure to change the DBQ=D:\Inetpub\DevelopmentPrivate\TestADO.mdb; path to your local path.

Append data to the database. (Adding records)

The important code in this block is where the record is Added to the database. Click here to view the results. Note an important feature of ASP that is demonstrated in this sample is its ability to post messages to itself. In this way the form and processing logic can be kept in the same page.
'***************************************************************************
'This is processing of the Form Request
strAuthor   = request.form("Author")
nAge        = Cint( request.form("Age") )
strMessage  = request.form("Message")

IF strMessage = "" THEN
    iLenNote=255
ELSE
    iLenNote = Len(strMessage)
END IF

'Connects to the Access driver and Access database in the Inetpub
'directory where the database is saved
strProvider = "Driver={Microsoft Access Driver (*.mdb)};" &_
              "DBQ=D:\Inetpub\Development\ASP\TestADO.mdb;"
'Creates an instance of an Active Server component
set objConn = server.createobject("ADODB.Connection")

'Opens the connection to the data store
objConn.Open strProvider

'Instantiate Command object and use ActiveConnection property to
'attach connection to Command object
set cm = Server.CreateObject("ADODB.Command")
cm.ActiveConnection = objConn

'Define SQL query
cm.CommandText = "INSERT INTO Comment (Author,Age,Message,LastRefreshed)" &_
                 " VALUES (?,?,?,?)" 
'Define query parameter configuration information for guestbook fields
set objparam=cm.createparameter(, 200, ,  50, strAuthor)
cm.parameters.append objparam
set objparam=cm.createparameter(,   3, ,   4, nAge )
cm.parameters.append objparam
set objparam=cm.createparameter(, 201, , iLenNote, strMessage)
cm.parameters.append objparam
set objparam=cm.createparameter(, 135, ,  50, Now )
cm.parameters.append objparam    
cm.execute
'Say that all went well
Response.Write( "Thank you for your input!" )

What needs further investigation.

We are not doing any error handling. If any one knows how to do this add it to the comments.
Comments Date
Home Search Contact us About us