|
||||||
Working with Records and Fields in VBScriptHow to Use VBScript to Process the Contents of a Recordset
VBScript (the scripting language for Visual Basic) can make the processing of records and fields in a recordset very easy for the database programmer.
Visual Basic is one of the most commonly used programming languages in the world of software. One reason for its success is that VBScript (the scripting element of Visual Basic) is installed on every Windows computer. Another reason is that VBScript can be used to program many Windows applications - for example Microsoft Access. With VBScript it is very easy to process one a vital element of Microsoft Access - the recordset. A recordset is the data returned from running a SQL query on a database or by reading the contents of a table, and so the first stage is to connect to the database. Using VBScript to Connect to a Microsoft Access DatabaseMaking a connection to a Microsoft Access database is very easy and just requires a couple of inputs:
The connection code is, therefore: dim datasource : datasource = "provider=microsoft.jet.oledb.4.0;" _
& "data source=c:\sales.mdb"
dim conn : set conn = createobject("adodb.connection")
conn.open datasource
If this code is save to a .vbs file and run (by double clicking on the file in Windows Explorer) then nothing obvious will happen - nothing interesting will happen until a recordset has been created. Using VBScript to Create a RecordsetThe processing of recordsets may be used quite a lot in an application, and so it's useful to build the functionality into a subroutine: sub show_results (sql, connection)
The recordset can then be created from the sql and the connection passed to the subroutine: dim rs : set rs = createobject("adodb.recordset") rs.open sql, connection The recordset will now have been loaded into VBScript, and so the next step is to process the contents of the recordset. Using VBScript to Process the Contents of a RecordsetThere are two things that the programmer is unlikely to know about the recordset:
The VBScript must, therefore, be able to cope with these variables and it does this in two ways:
Both tasks are, of course, very easy with VBScript. Using VBScript to Process Records in a RecordsetVBScript must be able to process the recordset record by record (or line by line), and it does this by making use of the recordsets eof (end of fields) property and the movenext method: while not rs.eof
'Process the current record
rs.movenext
wend
Here VBScript will loop through each record in the recordset, and there's an important point to take not of at this point. If the movenext method is missed out an infinite loop will be caused (because the code will not progress past the first record). Now that the recordset can be processed record by record the next stage is to process each of the fields in the record. Using VBScript to Process Fields in a RecordThe fields in a record can be processed by using the for each statement: for each field in rs.fields
'Process the current field
next
These two techniques can now be combined to produce a subroutine that will be able to handle any number of records with any number of fields. A Complete VBScript for Processing a RecordsetThe final subroutine can accept any sql statement with a connection and will display the results of the query: sub show_results (sql, connection)
dim details
while not rs.eof
for each field in rs.fields
details = details & field & " "
next
details = details & vbcr
rs.movenext
wend
msgbox details
set rs = nothing 'Clear up memory used
end sub
The subroutine can now be called from anywhere in the application, for example: dim sql : sql = "select * from customer"
show_results sql, conn
or the table name can be used: sql = "customer" show_results sql, conn The end result, in this example, will be for the contents of the customer table to be displayed to the user.
The copyright of the article Working with Records and Fields in VBScript in Database Programming is owned by Mark Alexander Bain. Permission to republish Working with Records and Fields in VBScript in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||