Working with DBF Files in Delphi Without Driver

At a time when all the database using the client-server is a primary requirement, the use of DBF may seem strange. But sometimes, we need a temporary file, for example, for report. It is interesting if we could use as a DBF database file with no driver at all.

DBF file is an ancient type of table. But do not underestimate this file, in some places the use of tables to DBF format is still used. Thus studying this file as a programming exercise material remain current. In this Discussion I would do involve creating a DBF file, filling with data, or connect it to the datasource, so the report can be made from it. Any discussion in this posting using TDBF that can be downloaded at: http://tdbf.sourceforge.net/. By using TDBF, you do not need to use any drivers, everything can be done directly from Delphi.

A. Making DBF File

Drag TDBF VCL from Data Access tab (Picture 1)

Picture 1

At FormCreate, type the following program code:

with dbf1 do
    begin
        FilePathFull :=ExtractFilePath(Application.ExeName);
        TableLevel := 7;
        TableName := ‘batch.dbf’;

        with FieldDefs do
        begin
             Add(’batch’,ftString,8);
             Add(’tgl’,ftDate);
             Add(’produk’,ftString,60);
         end;

         CreateTable;
    end;

FilePathFull is a property that indicates where the DBF file will reside. In the code above, the DBF file is considered in this program exe file. In the line of program code below, is a way of knowing where its exe file:

ExtractFilePath(Application.ExeName);

TableLevel states dbf file types to be created. Below is a table out more about the types of dbf file:

  1. Level 3   : dBase III+
  2. Level 4   : dBase IV
  3. Level 7   : dBase VIII
  4. Level 25 : FoxPro

FieldDefs is used for adding field type. Complete list of field types can be seen below:

  1. ftUnknown
  2. ftString
  3. ftSmallInt
  4. ftInteger
  5. ftWord
  6. ftBoolean
  7. ftFloat
  8. ftCurrency (TableLevel 25)
  9. ftBCD (TableLevel 25)
  10. ftDate
  11. ftTime
  12. ftDateTime
  13. ftBytes (TableLevel 25)
  14. ftVarBytes
  15. ftAutoInc (TableLevel 7 or 25)
  16. ftBlob
  17. ftMemo
  18. ftGraphic
  19. ftFmtMemo
  20. ftParadoxOle
  21. ftDBaseOle
  22. ftTypedBinary
  23. ftCursor
  24. ftFixedChar
  25. ftWideString
  26. ftLargeInt
  27. ftADT
  28. ftArray
  29. ftReference
  30. ftDataSet
  31. ftOraBlob
  32. ftOraClob
  33. ftVariant
  34. ftInterface
  35. ftIDispatch
  36. ftGuid
  37. ftTimeStamp
  38. ftFMTBcd

CreateTable used to create tables based on predefined definitions. Without CreateTable command, then the dbf file on the hard drive has not been established.

B. Filling Data

Filling data can be performed with the following code sample:

    with dbf1 do
    begin
        DisableControls;    
        Open;
        Append;
        FieldByName(’BATCH’).Value :=  ‘0001′;
        FieldByName(’PRODUCTDATE’).Value := DateTimePicker1.Date;
        FieldByName(’PRODUCTNAME’).Value := ‘Chocolate Bread’;
        Post;
        EnableControls;
    end;

DisableControl is used to turn off the update component. By turning off this function, then the execution is faster. This property does not have to be set.

Open is used to activate the table in order to receive the table operations

Append is used to prepare a place for the new record.

FieldByName (). Value is used to connect the field to the table with data input.

Post is used to enter data that is handed to the command FieldByName (). Value into a table

EnableControl used to activate the component.

C. Connection to DataSource

TDBF can be referred by TDataSource, and thus, the contents of the table TDBF can be displayed on all components that use TDataSource. For example, below I will use the TDBGrid to display the contents of a DBF table.

Put TDBF, TDataSource, and TDBGrid to the form.

PICTURE 2

set DataSource’s TDataSet property to TDBF. Set TDataGrid to TDataSet:

DataSource1.DataSet  := Dbf1; DbGrid1.DataSource  := DataSource1;

Place the following code in formcreate, so that when the form began to show, directly invoked DBF table.

with Dbf1 do
  begin
      FilePathFull := ExtractFilePath(Application.ExeName);
      TableName := ‘batch.dbf’;
      Open;
  end;

comments powered by Disqus
Loading