ActiveX Data Objects

ActiveX Data Objects

Now that you have seen the examples demonstrating the usage of Recordset objects with the database, let’s concentrate on the issue of connectionless recordsets or rather should I say “Creatable Recordsets”. Below is shown the code that creates a brand new recordset that has no relationship to an OLE DB data source. The code generates a recordset that reads drive information through the FileSystem Scripting Object. So, you will learn not only how to create a new recordset (connectionless recordset) but also, how to use the FileSystem Scripting Object. The code shown below is written in VBScript. The ASP version is also provided with this article. See the related documents.

CODE

'============================================================
'Name: ConnectionlessRS.vbs
'Description: Shows a connection less recordset with it’s own fields added.
'============================================================
'Constants
Const adUseClient = 3
const adCurrency = 6
const adBSTR = 8
'Local Variables
dim fso, rst, drives
'Creates the main objects of the script
set fso = CreateObject("scripting.filesystemobject")
set rst = CreateObject("ADODB.Recordset")
'Gets the collection of available drives
set drives = fso.Drives
'Prepares the Recordset structure: Root, Volume, Type
'FileSystem, FreeSpace
rst.CursorLocation = aduseclient
rst.Fields.Append "Root", adBSTR
rst.Fields.Append "Volume", adBSTR
rst.Fields.Append "Type",adBSTR
rst.Fields.Append "FileSystem",adBSTR
rst.Fields.Append "FreeSpace",adCurrency
rst.Open
'Fills the recordset out with drive information
for each drv in drives
            rst.AddNew
            if drv.isready then
                        rst.Fields("Root").value = drv.DriveLetter
                        rst.Fields("Volume").value = drv.VolumeName
                        rst.fields("Type").value = drv.DriveType
                        rst.Fields("FileSystem").value = drv.FileSystem
                        rst.Fields("FreeSpace").value = drv.FreeSpace/1024
            end if
next
'Displays the recordset
s = ""
rst.movefirst
while not rst.EOF
            for each fld in rst.Fields
                        s = s & Pad(rst.Fields(fld.name),14) & vbtab
            next
            s = s & vbcrlf
            rst.MoveNext
Wend
msgbox s
'============================================================
'Pad(str, numChars)
'Pads the specified string with the specified trailing blanks
'============================================================
Function Pad(str, numChars)
            str = str & space(numChars)
            Pad= Left(str,numchars)
End Function

Two objects are used in this code, one is the FileSystem Scripting Object and the other is the Recordset object. Drives property of the FileSystem Object is used to get the collection of all the drives in your computer. The next step is to create new fields of your recordset object. We have used the client side cursor during the process. Append property of the fields collection is used to add new fields in the recordset. Once the fields are appended, we scroll through the drives collection and add new record against each drive, each record contains information about the individual drive. Lastly, we display the drive information using the msgbox function. Trailing blanks are added to the strings simply to display the information more clearly on the screen. To display the same information in ASP, we use Response.Write method while navigating through the recordset. Also, note that to use the above code in ASP, you will have to create the objects using the “Server.CreateObject()” method.

You might also like...

Comments

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“God could create the world in six days because he didn't have to make it compatible with the previous version.”