Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Data Classes

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I don't get Data Classes.&nbsp;&nbsp;What are the good for?<br>I've read the info in my MSDN help, but they don't (what a suprise) explain what they are good for.<br><br>If someone uses them, could you please tell me how and for what?<br><br>Thanks a bunch.
 
Bigfoot,<br><br>Found this article in the VB6 Help which seems quite good.<br><br>I haven't used Data Classes myself - but the main advantage seems to be that you can create a data source - like a data control - that you can then bind controls like text boxes or list boxes to.<br><br><font color=blue><b><br>Creating a Data-Aware Class that Reads Records from a Delimited Text File</b><br><br><br>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.<br><br>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.<br><br>To create a data-aware class that reads data from a delimited text file <br><br>Create a class that acts as a data source.<br><br><br>Add code to read data from the text file into an ADO recordset.<br><br><br>Set the data source for the class. <br><br><br>Note&nbsp;&nbsp;&nbsp;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 <b>Interacting with Data in an ASCII Text File.</b><br><br>Create a Class that Acts as a Data Source<br>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.<br><br>For example, to create a CustomerDataSource class that can act as a data source, set the following properties:<br><br>Property Setting <br>Name CustomerDataSource <br>DataSourceBehavior vbDataSource <br><br><br>For More Information&nbsp;&nbsp;&nbsp;Data-aware classes are covered in depth in Creating Data-Aware Classes in the Programmer's Guide.<br><br>Add Code to Read Data from the Text File into an ADO Recordset<br>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.<br><br>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:<br><br>Public rsCustomers As ADODB.Recordset<br><br>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.<br><br>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:<br><br>Private Sub Class_Initialize()<br><br>&nbsp;&nbsp;&nbsp;Dim fld As ADODB.Field<br>&nbsp;&nbsp;&nbsp;Dim strRow As String<br>&nbsp;&nbsp;&nbsp;Dim strField As String<br>&nbsp;&nbsp;&nbsp;Dim intPos As Integer<br><br>&nbsp;&nbsp;&nbsp;Set rsCustomers = New ADODB.Recordset<br><br>&nbsp;&nbsp;&nbsp;With rsCustomers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Set CustomerID as the primary key.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;CustomerID&quot;, adChar, 5, adFldRowID<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;CompanyName&quot;, adChar, 40, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;ContactName&quot;, adChar, 30, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;ContactTitle&quot;, adChar, 30, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;Address&quot;, adChar, 60, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;City&quot;, adChar, 15, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;Region&quot;, adChar, 15, adFldMayBeNull<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;PostalCode&quot;, adChar, 10, adFldMayBeNull<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;Country&quot;, adChar, 15, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;Phone&quot;, adChar, 24, adFldUpdatable<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields.Append &quot;Fax&quot;, adChar, 24, adFldMayBeNull<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Use Keyset cursor type to allow updating records.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.CursorType = adOpenKeyset<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.LockType = adLockOptimistic<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Open<br>&nbsp;&nbsp;&nbsp;End With<br><br>&nbsp;&nbsp;&nbsp;Open &quot;Customers.txt&quot; For Input As #1<br><br>&nbsp;&nbsp;&nbsp;Do Until EOF(1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line Input #1, strRow<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;With rsCustomers<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.AddNew<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;For Each fld In .Fields<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' If a tab delimiter is found, field text is to the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' left of the delimiter.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If InStr(strRow, Chr(9)) &lt;&gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Move position to tab delimiter.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intPos = InStr(strRow, Chr(9))<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Assign field text to strField variable.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strField = Left(strRow, intPos - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' If a tab delimiter isn't found, field text is the<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' last field in the row.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strField = strRow<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Strip off quotation marks.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;If Left(strField, 1) = Chr(34) Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strField = Left(strField, Len(strField) - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strField = Right(strField, Len(strField) - 1)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End If<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fld.Value = strField<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' Strip off field value text from text row.<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strRow = Right(strRow, Len(strRow) - intPos)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;intPos = 0<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Next<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Update<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.MoveFirst<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;End With<br>&nbsp;&nbsp;&nbsp;Loop<br>&nbsp;&nbsp;&nbsp;Close<br><br>End Sub<br><br>Set the Data Source for the Class<br>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.<br><br>For example, to set the rsCustomers recordset as the data source for the CustomerDataSource class, add the following to the Class_GetDataMember event procedure:<br><br>Private Sub Class_GetDataMember(DataMember As String, Data As Object)&nbsp;&nbsp;&nbsp;Set Data = rsCustomersEnd Sub<br>For More Information&nbsp;&nbsp;&nbsp;For a discussion of data sources, see Creating a Data Source in the Programmer's Guide.<br><br>Step by Step<br>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.<br><br>To See <br>Go to the next step Creating a Form that Lets You View and Update Data from a Data-Aware Class <br>Start from the beginning Interacting with Data in an ASCII Text File <br></font> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
...as I duck and run...&nbsp;&nbsp;Mike, you might try posting the link to the site instead of the article for a couple of reasons.&nbsp;&nbsp;First, Copyright's and second the page may print better from the authors site since this site typically has wide columns.<br><br>My 2 cents... <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
 
Thanks Mike,&nbsp;&nbsp;I'll play around with it... In fact, I have a use for it.<br><br>Thanks again...<br><br>Gary
 
Steve,<br><br>The article is attributed &quot;from the VB6 help&quot; and it was taken from the CD rather than a site - I'm not aware it is available online.<br> <p>Mike<br><a href=mailto:michael.j.lacey@ntlworld.com>michael.j.lacey@ntlworld.com</a><br><a href= Cargill's Corporate Web Site</a><br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top