Share via


Visual Basic Concepts

Creating a Data-Aware Class that Reads Records from a Delimited Text File

By creating a data-aware class, you can read data from a delimited text file into an ADO recordset and use the features of ADO to manipulate the data. You can then use the class as a data source in your application, binding controls on a form to fields in the recordset.

This topic shows how to create a data-aware class that reads data in a tab-delimited text file and provides methods for navigating through the data.

To create a data-aware class that reads data from a delimited text file

  1. Create a class that acts as a data source.

  2. Add code to read data from the text file into an ADO recordset.

  3. Set the data source for the class.

Note   This topic is part of a series that walks you through creating a simple database application that interacts with data in a tab-delimited text file. It begins with the topic Interacting with Data in an ASCII Text File.

Create a Class that Acts as a Data Source

You can create a class that acts as a data source by inserting a class module in your project and specifying its data source behavior. First, insert a class module in your project by selecting Add Class Module from the Project menu. Then set the Name and DataSourceBehavior properties for the class.

For example, to create a CustomerDataSource class that can act as a data source, set the following properties:

Property Setting
Name CustomerDataSource
DataSourceBehavior vbDataSource

For More Information   Data-aware classes are covered in depth in Creating Data-Aware Classes in the Programmer's Guide.

Add Code to Read Data from the Text File into an ADO Recordset

By reading data from a text file into an ADO recordset, you can use the features of ADO to manipulate the data. First, add a reference to the ADO object library by selecting References on the Project menu, then selecting Microsoft ActiveX Data Objects 2.0 Library in the References dialog box.

Then declare a Recordset object variable in the Declarations section for the class. For example, to declare a Recordset object variable for working with customer records from the Customers.txt file, add the following to the Declarations section:

Public rsCustomers As ADODB.Recordset

By declaring the variable as a public variable, you can use the built-in methods of the Recordset object in applications that use the data-aware class.

Finally, add code to the Class_Initialize event procedure for the class to read data from the text file. For example, add the following code to the Class_Initialize event procedure for the CustomerDataSource class to read data from the Customers.txt file into a recordset:

Private Sub Class_Initialize()

   Dim fld As ADODB.Field
   Dim strRow As String
   Dim strField As String
   Dim intPos As Integer

   Set rsCustomers = New ADODB.Recordset

   With rsCustomers
      ' Set CustomerID as the primary key.
      .Fields.Append "CustomerID", adChar, 5, adFldRowID
      .Fields.Append "CompanyName", adChar, 40, adFldUpdatable
      .Fields.Append "ContactName", adChar, 30, adFldUpdatable
      .Fields.Append "ContactTitle", adChar, 30, adFldUpdatable
      .Fields.Append "Address", adChar, 60, adFldUpdatable
      .Fields.Append "City", adChar, 15, adFldUpdatable
      .Fields.Append "Region", adChar, 15, adFldMayBeNull
      .Fields.Append "PostalCode", adChar, 10, adFldMayBeNull
      .Fields.Append "Country", adChar, 15, adFldUpdatable
      .Fields.Append "Phone", adChar, 24, adFldUpdatable
      .Fields.Append "Fax", adChar, 24, adFldMayBeNull
      ' Use Keyset cursor type to allow updating records.
      .CursorType = adOpenKeyset
      .LockType = adLockOptimistic
      .Open
   End With

   Open "Customers.txt" For Input As #1

   Do Until EOF(1)
      Line Input #1, strRow
      With rsCustomers
         .AddNew
         For Each fld In .Fields
            ' If a tab delimiter is found, field text is to the
            ' left of the delimiter.
            If InStr(strRow, Chr(9)) <> 0 Then
               ' Move position to tab delimiter.
               intPos = InStr(strRow, Chr(9))
               ' Assign field text to strField variable.
               strField = Left(strRow, intPos - 1)
            Else
               ' If a tab delimiter isn't found, field text is the
               ' last field in the row.
               strField = strRow
            End If

            ' Strip off quotation marks.
            If Left(strField, 1) = Chr(34) Then
               strField = Left(strField, Len(strField) - 1)
               strField = Right(strField, Len(strField) - 1)
            End If

            fld.Value = strField

            ' Strip off field value text from text row.
            strRow = Right(strRow, Len(strRow) - intPos)
            intPos = 0

         Next
         .Update
         .MoveFirst
      End With
   Loop
   Close

End Sub

Set the Data Source for the Class

When you specify a class as a data source by setting its DataSourceBehavior to vbDataSource, Visual Basic automatically adds a GetDataMember event to the class. The Class_GetDataMember event procedure is where you set the data source for the class by assigning it to the Data object for the class.

For example, to set the rsCustomers recordset as the data source for the CustomerDataSource class, add the following to the Class_GetDataMember event procedure:

Private Sub Class_GetDataMember(DataMember As String, Data As Object)   Set Data = rsCustomersEnd Sub

For More Information   For a discussion of data sources, see Creating a Data Source in the Programmer's Guide.

Step by Step

This topic is part of a series that walks you through using a data-aware class and ADO to create a simple database application that interacts with data in a tab-delimited text file.

To See
Go to the next step Creating a Form that Lets You View and Update Data from a Data-Aware Class
Start from the beginning Interacting with Data in an ASCII Text File