Class TDataRow
Class TDataRow simplifies the scatter/gather process of the field values of a single record of a table for purposes of editing/appending. This class can read all or specified field values from a single row of DBF table,ADO RecordSet, TDatabase object or XBrowse and save the values after editing to the same record.
After we specify the datasource (dbf/ors,etc) in the New() method, we can use the class the sameway for all kinds of datasources.
Datas and Methods:
DATAS:
aData : MultiDimentional Array of FieldName, FieldValue Read by Method New()
lReadOnly : Logical default .t.
RecNo : Record No. ( 0 in case of Blank/New )
bSave : Optional codeblock to override default Save() method
bEdit : Optional codeblock to override default Edit() method
bOnSave : Optional cpdeblock. Executes at the end of Save() method
lValidData : ReadOnly Logical.
METHODS:
New( [cAlias|oRecSet|oDbf|oBrw( Default Alias() )], [cFieldList (Def AllFields)], [lBlank (Def .f. )] )
Edit( [lReadOnly] ) : Default Edit dialog provided.
Undo() : Restores all values to original values when first read
Save() : saves modified values to the data source
Modified( [nPos (def All) ) : Returns .t. if specified field or any field is changed
FCount() : Total number of fields read
FieldPos( cName ) : Field Number of a field name
FieldName( nPos ) : Name of field at pos
FieldType( nPos ) : Type of field at pos
Usage:
DBF:
USE CUSTOMER NEW ALIAS CUST
oRec := TDataRow():New( "CUST" )
? oRec:RecNo,oRec:Age, oRec:Salary
oRec:Salary += 2000
? oRec:Salary
? oRec:Modified()
oRec:Save()
RecordSet:
oRs := FW_OpenRecordSet( "c:\fwh\samples\xbrtest.mdb", "CUSTOMER" )
oRec := TDataRow():New( "CUST" )
? oRec:RecNo,oRec:Age, oRec:Salary
oRec:Salary += 2000
? oRec:Salary
? oRec:Modified()
oRec:Save()
Same way for oDbf, oBrw.
We can make our own GET dialog using oRec:<fldnames> as get variables. We can use oRec:Undo() to cancel changes and finally oRec:Save() to
save the changes.
TDataRow class provides a default Edit Dialog which is very simple in this version
oRec := TDataRow():New( oRs )
oRec:Edit() // displays edit dialog with Undo, Cancel and Save buttons.
Appending New Records:
Create a phantom blank record by specifying .t. as 3rd parameter.
oRec := TDataRow():New( cAlias/oRs, nil, .t. ) // 3rd param .t. indicates new blank record
oRec:Edit()
When saved the TDataRow method appends the data to a new record.
We are not conerned how TDataRow appends ( for dbf dbappend() for oRs AddNew() )
For us, the process is transparent.
Customising edit dialogs:
- Create our own procedure defining edit dialog and when oRec:Modified() is true
call oRec:Save()
- Assign our edit dialog function to oRec:bEdit codeblock
- Override oRec:Edit() method in a derived class.
Using TDataRow class for adhoc data.
aData := { ;
{ "Name", Space( 30 ) }, ;
{ "Age", 0 }, ;
{ "City", Space( 20 ) } }
oData := TDataRow():New( aData )
oData:Edit()
and we have the modified data in aData now.