{ewc HLP95EN.DLL,DYNALINK,"See Also":"vbconGetDataFromASCIIFileC"}
The data your application needs to interact with may not always be stored in a relational database. It may be stored in delimited or fixed-length fields in an ASCII text file. For example, your application's data source may be data downloaded into a text file from a mainframe computer, a file converted from an unsupported database format or operating system, or an "ad hoc" data file created with a text editor.
By creating a data-aware class and using Microsoft ActiveX Data Objects (ADO), you can use Visual Basic to create applications that interact with data in an ASCII text file. Your application can read data from a text file, update fields, add new records, and write data back to the text file as if the data were stored in a relational database.
This series of topics shows you how to use a data-aware class and
· Creating a data-aware class that reads records from a delimited text filevbconCreateNewDataSource
· Creating a form that lets you view and update data from a data-aware classvbconCreateConnectionToDataSource
· Modifying the form to let you add new recordsvbconBindGridControlToSource
· Modifying the class and form to write records back to the delimited text filevbconFormatData
{ewc HLP95EN.DLL,DYNALINK,"See Also":"vbconCreateNewDataSourceC"}
By creating a data-aware class, you can read data from a
delimited text file into an
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
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 FilevbconGetDataFromASCIIFile.
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 ClassesvbconCreatingData-AwareClasses in the Programmer's Guide.
By reading data from a text file into an
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
Close
End Sub
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 = rsCustomers
End Sub
For More Information For a discussion of data sources, see Creating a Data SourcevbconCreatingDataSource in the Programmer's Guide.
This topic is part of a series that walks you through using a
data-aware class and
|
To |
See |
|
Go to the next step |
Creating a Form that Lets You View and Update Data from a Data-Aware ClassvbconCreateConnectionToDataSource |
|
Start from the beginning |
Interacting with Data in an ASCII Text FilevbconGetDataFromASCIIFile |
{ewc HLP95EN.DLL,DYNALINK,"See Also":"vbconCreateConnectionToDataSourceC"}
Once you've created a class that can act as a data source, you can easily create applications that let you view and update records from the data source. You can use ActiveX Data Objects (ADO) and the BindingsCollection object to bind the data source to controls on the form, and add command buttons to navigate through records.
This topic shows how to create a form that lets you view and edit customer address records from the data-aware class you created in the previous topic.
To create a form that lets you view and update data from a data-aware class
1. Add text box and label controls to a form.
2. Add code to bind the text box controls to the data source.
3. Add command button controls to navigate through records.
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 FilevbconGetDataFromASCIIFile.
The first step in viewing and updating data from a data-aware class is to create an interface for interacting with records from the data source. The easiest way to create an interface is to open a new Standard EXE project, then add TextBox and Label controls to a form.
For example, you can create an interface for viewing customer address information from the Customers.txt file. First, add a "Customer ID:" label to the form, then add a text box control next to the label and set its Name property to txtCustomerID. Repeat the same process for CompanyName, Address, City, Region, PostalCode, and Country controls.
Using a data-aware class, ActiveX Data Objects (ADO), and the BindingCollection object, you can bind controls to a data source when the page loads. You can then edit the data and add code to navigate through the records. For example, you can use an instance of your data-aware class and the BindingCollection object to bind the text box controls on your form to fields from the Customers.txt file.
First, add a reference to the BindingCollection object's type library to your project. To add the reference, select References on the Project menu, then select Microsoft Data Binding Collection in the References dialog box.
Then declare variables for the data-aware class and a BindingCollection object in your form's Declarations section:
Private objDataSource As CustomerDataSource
Private colBind As BindingCollection
To bind the text box controls to fields from the Customers.txt
file when the form loads, add code to the form's Load event procedure. An
instance of the CustomerDataSource class reads records from the text file into
an
Private Sub Form_Load()
Set objDataSource = New CustomerDataSource
Set colBind = New BindingCollection
Set colBind.DataSource = objDataSource
colBind.Add txtCustomerID, "Text", "CustomerID"
colBind.Add txtCompanyName, "Text", "CompanyName"
colBind.Add txtAddress, "Text", "Address"
colBind.Add txtCity, "Text", "City"
colBind.Add txtRegion, "Text", "Region"
colBind.Add txtPostalCode, "Text", "PostalCode"
colBind.Add txtCountry, "Text", "Country"
End Sub
By binding controls on your form to a public recordset in your data-aware class, you can easily create Next and Previous buttons that let you navigate through records. Each command button requires a single line of code.
For example, to create a Next button for the form that displays customer records, add a command button to the form and change its Caption and Name properties to Next. Then add the following line to the command button's Next_Click event procedure:
objDataSource.rsCustomers.MoveNext
The code uses the MoveNext method of the rsCustomers recordset that serves as the data source for the form's controls. It refers to the recordset as a property of the object variable that represents an instance of the CustomerDataSource class.
Similarly, you can create Previous, First, and Last buttons by adding command buttons to the form and changing their Caption and Name properties to Previous, First, and Last, respectively. Then add code to the Click event procedure for each command button that invokes the MovePrevious, MoveFirst, and MoveLast methods.
When you run the form, Visual Basic lets you view and update records from the Customers.txt file and lets you navigate through the recordset.
This topic is part of a series that walks you through using a
data-aware class and
|
To |
See |
|
Go to the next step |
Modifying the Form to Let You Add New RecordsvbconBindGridControlToSource |
|
Start from the beginning |
Interacting with Data in an ASCII Text FilevbconGetDataFromASCIIFile |
{ewc HLP95EN.DLL,DYNALINK,"See Also":"vbconBindGridControlToSourceC"}
In addition to viewing and updating existing records, you may want to add new records to your data source. You can modify your form so that it can serve as a data entry form by creating command buttons that use ActiveX Data Objects (ADO) and the BindingsCollection object to clear the display of data, manage the data binding of controls, and add a new record to the underlying recordset.
This topic shows how to modify the form you created in the previous topic so that it also serves as a data entry form for adding new customer records.
To modify the form to let you add new records
1. Create command buttons for allowing data entry, adding new customer records, and returning to viewing data.
2. Add code to enable data entry.
3. Add code to enable saving the data you enter as a new record.
4. Add code to enable returning the form to viewing data.
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 FilevbconGetDataFromASCIIFile.
The first step in modifying the form is to create the interface for the tasks that you want to accomplish. For example, to allow data entry on the customer address form you created in the previous topic, you could add the following:
· A DataEntry command button to clear the existing data displayed on the form and disable data binding.
· An AddCustomer command button to add new data entered on the form as a new record in the underlying recordset.
· A ViewData command button to re-enable data binding, returning the form to its original state.
First, add a command button to the form and change its Caption and Name properties to DataEntry. Add a second command button to the form and change its Caption and Name properties to AddCustomer. Then add a third command button to the form and change its Caption and Name properties to ViewData.
Because the AddCustomer and ViewData command buttons should only be displayed when the form is being used for data entry, set the Visible property for these controls to False.
You can make a data-bound form also serve as a data entry form by disabling data binding and clearing the existing data displayed on the form. You may also want to show hidden command button controls that apply only to adding new records, and hide command button controls that apply only to viewing existing records.
For example, to enable the DataEntry command button on the customer address form, add the following code to the DataEntry_Click event procedure:
Private Sub DataEntry_Click()
' Disable data binding.
Set colBind = Nothing
' Clear the text box controls.
Me.txtCustomerID = ""
Me.txtCompanyName = ""
Me.txtAddress = ""
Me.txtCity = ""
Me.txtRegion = ""
Me.txtPostalCode = ""
Me.txtCountry = ""
' Hide the command buttons used for viewing
' existing data.
Me.Next.Visible = False
Me.Previous.Visible = False
Me.First.Visible = False
Me.Last.Visible = False
Me.DataEntry.Visible = False
' Show the command buttons used for entering new data.
Me.AddCustomer.Visible = True
Me.ViewData.Visible = True
End Sub
After you've entered record data in a data entry form, you can
use
Private Sub AddCustomer_Click()
' Add the record to the rsCustomers recordset
' in your data-aware class.
With objDataSource.rsCustomers
.AddNew
!CustomerID = Me.txtCustomerID.Text
!CompanyName = Me.txtCompanyName.Text
!Address = Me.txtAddress.Text
!City = Me.txtCity.Text
!Region = Me.txtRegion.Text
!PostalCode = Me.txtPostalCode.Text
!Country = Me.txtCountry.Text
.Update
End With
' Clear the controls for additional data entry,
' if desired.
Me.txtCustomerID = ""
Me.txtCompanyName = ""
Me.txtAddress = ""
Me.txtCity = ""
Me.txtRegion = ""
Me.txtPostalCode = ""
Me.txtCountry = ""
End Sub
When you've finished using your form as a data entry form, you can return it to its original use for viewing and editing existing records by re-enabling data binding. Any new records you've entered will now be displayed when you move through records on the form. You can also hide command button controls that apply only to adding new records, and show hidden command button controls that apply to viewing existing records.
For example, to enable the ViewData command button on the customer address form, add the following code to the ViewData_Click event procedure:
Private Sub ViewData_Click()
' Bind text box controls to the data source
' of your data-aware class.
Set colBind = New BindingCollection
Set colBind.DataSource = objDataSource
colBind.Add txtCustomerID, "Text", "CustomerID"
colBind.Add txtCompanyName, "Text", "CompanyName"
colBind.Add txtAddress, "Text", "Address"
colBind.Add txtCity, "Text", "City"
colBind.Add txtRegion, "Text", "Region"
colBind.Add txtPostalCode, "Text", "PostalCode"
colBind.Add txtCountry, "Text", "Country"
' Show the command buttons used for viewing
' existing data.
Me.Next.Visible = True
Me.Previous.Visible = True
Me.First.Visible = True
Me.Last.Visible = True
Me.DataEntry.Visible = True
' Hide the command buttons used for entering new data.
Me.AddCustomer.Visible = False
Me.ViewData.Visible = False
End Sub
For More Information To read about the BindingCollection object, see Creating Data-Aware ClassesvbconCreatingData-AwareClasses in "Programming with Objects" in the Programmer's Guide.
This topic is part of a series that walks you through using a
data-aware class and
|
To |
See |
|
Go to the next step |
Modifying the Class and Form to Write Records Back to the Delimited Text FilevbconFormatData |
|
Start from the beginning |
Interacting with Data in an ASCII Text FilevbconGetDataFromASCIIFile |
{ewc HLP95EN.DLL,DYNALINK,"See Also":"vbconFormatDataC"}
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 FilevbconGetDataFromASCIIFile.
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
End With
Close
End Sub
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
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 FilevbconGetDataFromASCIIFile.
For More Information For information on data sources, see Creating Data SourcesvbconBuildingDataSources in the Component Tools Guide.
# vbconGetDataFromASCIIFile
T 184017
$ Interacting with Data in an ASCII Text File
A vbconBindGridControlToSourceC;vbconCreateConnectionToDataSourceC;vbconCreateNewDataSourceC;vbconFormatDataC;vbconGetDataFromASCIIFile;
K
# vbconCreateNewDataSource
T 184019
$ Creating a Data-Aware Class that Reads Records from a Delimited Text File
A vbconCreateNewDataSource;vbconGetDataFromASCIIFileC;
K
# vbconCreateConnectionToDataSource
T 184020
$ Creating a Form that Lets You View and Update Data from a Data-Aware Class
A vbconCreateConnectionToDataSource;vbconCreateNewDataSourceC;
K
# vbconBindGridControlToSource
T 184021
$ Modifying the Form to Let You Add New Records
A vbconBindGridControlToSource;vbconCreateConnectionToDataSourceC;
K
# vbconFormatData
T 184022
$ Modifying the Class and Form to Write Records Back to the Delimited Text File
A vbconBindGridControlToSourceC;vbconFormatData;
K