Navigation:  Samples >

Index Sample

Previous pageReturn to chapter overviewNext page

This example creates a new TableDef object and two new Field objects, appends the Field objects to the Fields collection in the new TableDef, and appends the TableDef to the TableDefs collection in the database. Then it creates a new primary Index object, includes the two Field objects in it, and appends the Index to the Indexes collection of the TableDef. Finally, the example enumerates the Index objects in the current database. See the methods and properties listed in the Index summary topic for additional examples.

 

FUNCTION Start

LOCAL dbeng                                AS DaoDBEngine

LOCAL dbsdefault                AS DaoDatabase

LOCAL tdftest                        AS DaoTableDef

LOCAL fldOne, fldTwo        AS DaoField

LOCAL idxPrimary                AS DaoIndex

LOCAL oErr                                AS USUAL

LOCAL cbOldErr                        AS CODEBLOCK

Set COlor TO w+/b

cls

BEGIN SEQUENCE

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

 dbEng        := DaoDbengine{}

 

 // Get workspace and database.

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

 

 // Delete table MyTable if it exists

 BEGIN SEQUENCE

         dbsDefault:TableDefs:Delete("MyTable")

 END

 // Create table with two fields.

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

 fldOne := tdfTest:CreateField("Field1", dbLong,NIL)

 fldOne:Required := TRUE    // No Null values allowed.

 tdfTest:Fields:Append(fldOne)

 fldTwo := tdfTest:CreateField("Field2", dbLong,NIL)

 fldTwo:Required := TRUE    // No Null values allowed.

 

 tdfTest:Fields:Append(fldTwo)

 dbsDefault:TableDefs:Append(tdfTest)

 

 // Create primary index for those two fields.

 idxPrimary := tdfTest:CreateIndex("MyIndex")

 idxPrimary:Primary := TRUE

 fldOne := tdfTest:CreateField("Field1",NIL,NIL)

 idxPrimary:Fields:Append(fldOne)

 fldTwo := tdfTest:CreateField("Field2",NIL,NIL)

 idxPrimary:Fields:Append(fldTwo)

 tdfTest:Indexes:Append(idxPrimary)

 

 // Enumerate index and its fields.

 ? "Index: "        , idxPrimary:Name

 ? "  Required   : " , idxPrimary:Required

 ? "  IgnoreNulls: " , idxPrimary:IgnoreNulls

 ? "  Primary    : "        , idxPrimary:Primary

 ? "  Clustered  : " , idxPrimary:Clustered

 ? "  Unique     : "        , idxPrimary:Unique

 ? "  Foreign    : "        , idxPrimary:Foreign

 

 ? "Fields in Index:"

 idxPrimary:Fields:ForEach({|oFld|QOut(" ",oFld:Name)})

 

RECOVER USING oErr

 Eval(cbOldErr,oErr)        

END

wait

RETURN