Navigation:  Samples >

Fields sample

Previous pageReturn to chapter overviewNext page

This example creates a TableDef object and a Field object, appends the Field to the Fields collection in the new TableDef, and appends the TableDef to the TableDefs collection in the current database. The example enumerates all the fields in the new TableDef object and all the properties of the new Field. See the methods and properties listed in the Field summary topic for additional examples.

 

FUNCTION Start

LOCAL dbeng        AS DaoDBEngine

LOCAL dbsNw        AS DaoDatabase

LOCAL tdfTest        AS DaoTableDef

LOCAL fldTest        AS DaoField

LOCAL i        AS LONG

LOCAL oErr        AS USUAL

LOCAL cbOldErr AS CODEBLOCK

Set COlor TO w+/b

cls

BEGIN SEQUENCE

 cbOldErr := ErrorBlock({|oErr|_Break(oErr)})

 dbEng        := DaoDbengine{}

 dbsNw    := dbEng:OpenDatabase("\VO2Jet\Northwind.mdb",NIL,NIL,NIL)

 // Delete table MyTable if it exists

 BEGIN SEQUENCE

         DbsNw:TableDefs:Delete("MyTable")

 END

 //------------------------------

 tdfTest  := dbsNw:CreateTableDef("MyTable",NIL,NIL,NIL)

 fldTest  := tdfTest:CreateField(NIL,NIL,NIL)

 fldTest:Name                        := "MyFirstColumn"

 fldTest:TYpe                        := dbLong

 fldTest:Attributes                := dbAutoIncrField

 fldTest:Required                := TRUE

 tdfTest:Fields:Append(fldTest)

 //------------------------------        

 fldTest  := tdfTest:CreateField(NIL,NIL,NIL)

 fldTest:Name                        := "MySecondColumn"

 fldTest:TYpe                        := dbDate

 fldTest:Required                := TRUE

 fldTest:DefaultValue        := "Date()"

 fldTest:ValidationRule        := ">=Date()"

 fldTest:ValidationText        := "Date must be greater or equal than today"

 tdfTest:Fields:Append(fldTest)

 //------------------------------        

 fldTest  := tdfTest:CreateField(NIL,NIL,NIL)

 fldTest:Name                        := "MyThirdColumn"

 fldTest:TYpe                        := dbText

 fldTest:Required                := FALSE

 fldTest:Size                        := 60

 fldTest:AllowZeroLength        := TRUE

 tdfTest:Fields:Append(fldTest)

 dbsNw:TableDefs:Append(tdfTest)

 

 // Get database name.

 ? "Database Name  : ", dbsNw:Name

 ? "Table Name     : ", tdfTest:Name

 // Enumerate all fields in tdfTest:

 

 FOR i := 1 TO tdfTest:Fields:Count STEP 1

         fldTest := tdfTest:FIELDs:[Item,i]        

         // Enumerate built-in properties of fldTest.

         ? "Name           : ", fldTest:Name

         ? "AllowZeroLength: ", fldTest:AllowZeroLength

         ? "Attributes     : ", DaoEnum2Str(fldTest:Attributes, DaoFieldAttributeEnum(),TRUE)

         ? "CollatingOrder : ", DaoEnum2Str(fldTest:CollatingOrder, DaoCollatingOrderEnum(),FALSE)

         ? "DefaultValue   : ", fldTest:DefaultValue

         ? "OrdinalPosition: ", fldTest:OrdinalPosition

         ? "Required       : ", fldTest:Required

         ? "Size           : ", fldTest:Size

         ? "Type           : ", fldTest:Type, DaoFieldType(fldTest:Type, fldTest:Attributes)

         ? "ValidationRule : ", fldTest:ValidationRule

         ? "ValidationText : ", fldTest:ValidationText

         wait

 NEXT

 DbsNw:TableDefs:Delete("MyTable")

 

RECOVER USING oErr

 Eval(cbOldErr,oErr)        

END

RETURN