Creating new DBF file from Code

<< Click to Display Table of Contents >>

Navigation:  Apollo VCL Components > FAQ >

Creating new DBF file from Code

With TApolloTable and xBase-like Syntax

Using the TApolloTable, new tables are created using the CreateNew, CreateField, and CreateExec methods. For example:

 

procedure TForm1.Button1Click(Sender: TObject);

begin

 with ApolloTable1 do

begin

 if not CreateNew( 'tester.dbf', SDEFOX, 5 ) then

 begin

         ShowMessage( 'Create failed' ); 

         Exit; 

 end;

 CreateField( 'NAME', 'C', 25, 0 ); // CHARACTER

 CreateField( 'WAGE', 'N', 6, 2 ); // NUMERIC, w/DEC value

 CreateField( 'TAXED', 'L', 1, 0 ); // LOGICAL

 CreateField( 'HIRED', 'D', 8, 0 ); // DATE

 CreateField( 'NOTES', 'M', 10, 0 ); // NOTES

 if CreateExec then

                  ShowMessage( 'Create succeeded' )

 else

         ShowMessage( 'Create failed' ); 

end;

end;

 

New records are added with Append or AppendBlank and field values are stored with Replace.

 

New indexes are created with the Index and/or IndexTag methods.

 

With TApolloQuery and SQL Syntax

For remote tables using Apollo Database Server, you can also use the TApollQuery component's ExecSQL method to issue a CREATE TABLE command, like this:

 

procedure TForm1.Button1Click(Sender: TObject);

begin

 with ApolloQuery1 do

 begin

         SQL.Clear; 

         SQL.Text := ' CREATE TABLE "tester.dbf" ' +

                 ' ( last_name CHAR(20), ' +

                 ' first_name CHAR(15), ' +

                 ' salary FLOAT(10,2), ' +

                 ' dept_no SMALLINT ' +

                 ' PRIMARY KEY (last_name, first_name) )';

         try

                 ExecSQL; 

                 ShowMessage( 'Create succeeded' ); 

         except

                 ShowMessage( 'Create failed' ); 

         end;

 end;

end;