How To Pass a Variant Array Populated with a RecordSet From a VB Component to an ASP PageBy Doug Dean
|There's an on-line
discussion forum on this article, so if you have any questions or comments, be sure to visit
the forum and ask away!
[Visit the Forum]
This article serves as a quick "How To" example for sending a Variant array containing the contents of a RecordSet from a VB server-side component to an asp file. The VB component will populate the Variant array via the ADO
GetRows method. (For more information on using
sure to read: Using
GetRows to Display "Skinny" Tables,
Optimizing Database Display with
and Paging Records with
The supporting database file, text, and code, can be downloaded here.
If you are unfamiliar with writing a server-side VB dll, you can read some introductory articles at
The sample code simply exemplifies passing a Variant array filled with a RecordSet via a method.
Recommended code procedures like transactions, error handling, etc., have all been eliminated for
This example will include a VB server-side dll (ActiveX dll) consisting of one class (
and one method (
MethodName). The VB project is named
THE VB COMPONENT - How2Project3.vbp
The Function Definition
Two parameters will be passed to our component; a connection string (
and an sql statement (
strSQL) - both of which are of type string.
We want to return a Variant array, so we declared the method as type Variant. Although the default type
of a VB method is Variant, declaring it so clarifies the code. It's best to declare the parameters
ByVal for Internet connections.
The connection string value will need to contain the Data Source Name (DSN) that should first be established with ODBC, although any valid database connection string will work here. ADO will also need to be referenced in the VB project (see an introductory VB component article on how to do this).
Connecting The Database
After instantiating the ADODB RecordSet, Command, and Connection objects, the connection string is used to open the database connection and then the appropriate values are assigned to the Command object before opening the RecordSet. There are numerous ways to obtain a RecordSet via ADO. This is one such way.
Populating The Variant Array via
GetRows Method returns a Variant whose value is a two-dimensional array. We'll assign
the returned RecordSet field values directly to our methods' variable name. How to access each of the
record field values stored within the Variant array will be covered in the asp file code section.
We then close up and set our objects to
Now compile, or run, the VB component so the asp file can use it as an object (production servers will need to have the compiled dll registered on the system - again, see introductory articles on how this is accomplished).
Now that we've examined the VB component in depth, it's time to turn our attention to the ASP page. In Part 2 we'll look at this ASP page and how, exactly, it will obtain and access the contents of the variant array!