Share via


Visual Basic Concepts

Modifying the Class and Form to Write Records Back to the Delimited Text File

After you've updated records or added new records, you can write the changes back to the delimited text file that serves as the data source for your data-aware class. You can add a public method to the class that writes records to a file, then invoke the method in your applications.

This topic shows how to modify the CustomerDataSource class to provide a public method to write records back to the Customers.txt file, and how to invoke the method on your customer address form.

To modify the class and form to write all records back to the delimited text file

  1. Create a public method in the class that writes records to a file.

  2. Create a command button on your form that writes records to a file.

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 Public Method in the Class that Writes Records to a File

By adding Sub procedures to your class, you can provide public methods to applications that use your class as a data source. For example, you can create a public method in your CustomerDataSource class that writes current records from the rsCustomers recordset to the Customers.txt delimited text file. The text file will then include any changes or additions you've made to the recordset.

To create a public WriteToFile method, add the following code to the CustomerDataSource class:

Public Sub WriteToFile()

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

   Open "Customers.txt" For Output As #1

   With rsCustomers

      .MoveFirst
      Do While Not .EOF
         For Each fld In .Fields
            ' If a field has a value, add quotation marks.
            If Len(fld.Value) > 0 Then
               strField = Chr(34) & fld.Value & Chr(34)
            Else
               strField = ""
            End If
            ' Add the field value and a tab delimeter
            ' to the output string.
            strRow = strRow & strField & Chr(9)
         Next
         ' Strip off the end tab character.
         strRow = Left(strRow, Len(strRow) - 1)
         ' Print the output string.
         Print #1, strRow
         strRow = ""
      .MoveNext
      Loop
   End With
   Close

End Sub

Create a Command Button on Your Form that Writes Records to a File

Once you've created a public method in your data-aware class, you can use it in any application that requires the same functionality. For example, by creating a public method for writing records to a delimited text file, you can easily create a WriteToFile button on your customer address form. The command button requires a single line of code.

To create a WriteToFile button, add a command button to the form and change its Caption and Name properties to WriteToFile. Then add the following line to the command button's WriteToFile_Click event procedure:

objDataSource.WriteToFile

The code uses the WriteToFile method you created for the CustomerDataSource class.

You may want to hide the WriteToFile button while users enter data on the form. To hide the command button during data entry, add the following code to the DataEntry_Click event procedure:

Me.WriteToFile.Visible = False

And if you hide the WriteToFile button while users enter data, make it visible again while users view data. To do so, add the following code to the ViewData_Click event procedure:

Me.WriteToFile.Visible = True

Step by Step

This topic concludes a series that walks you through creating a simple database application that interacts with data in a tab-delimited text file. To start from the beginning, see Interacting with Data in an ASCII Text File.

For More Information   For information on data sources, see Creating Data Sources in the Component Tools Guide.