Fabricating a Recordset

In previous chapters, you worked with recordsets that were created from data that came from a Microsoft Access database, a text or dBASE file, an Excel spreadsheet, or a Word document. You may have also practiced working with a recordset generated from an SQL Server database. In each of these circumstances, to get the necessary data, you needed to establish a connection to the appropriate data source. In other words, you worked with recordsets that had a live connection to the data source. These connected recordsets obtained their structure and data from a query to a data source to which they were connected. But what if you need to create a recordset with data that does not come from a data source? As you may recall from the introduction to ADO in Chapter 10, the ADO Object Model allows you to work with both relational and non-relational data stores.

To store non-relational data in an ADO recordset, you can create your recordset from scratch. This recordset will be defined programmatically in memory and will not be connected to any data source. For example, you can easily fabricate a custom recordset that holds non-relational data, such as the information about the files located in one of your hard drive's directories.

Part II

When you create your own recordset from scratch, you define the types of fields in the recordset and then populate the recordset with the information you want. The fields are defined using the Fields collection's Append method. You must specify the field name, the data type, and the length. The syntax for the Append method looks like this:

Fields.Append Name, DataType[, FieldSize], [Attribute]

Recall that arguments in square brackets are optional. FieldSize specifies the size in characters or bytes. Attribute specifies characteristics such as whether the field enables Null values or whether it is a primary key or an identity column.

Once you have defined the structure of your recordset, you should open it and proceed to populate it with the data. You can add data to your custom recordset in the same way you add data to a connected recordset: by using the Recordset object's AddNew method.

The procedure in Hands-On 16-1 demonstrates how to create an empty recordset containing three fields (Name, Size, and Modified) and then populate it with files located in a user-specified file folder.

0 0

Post a comment