Recent

Author Topic: .DBF database file  (Read 2132 times)

Stoutall

  • Newbie
  • Posts: 1
.DBF database file
« on: October 18, 2017, 10:08:35 pm »
Hello.

The following error occurs when I attempt to open a .DBF database file: 'File does not exist'. I use Microsoft Access dBase driver for .DBF files.

Any suggestions, friends...

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8583
  • FPC developer.
Re: .DBF database file
« Reply #1 on: October 18, 2017, 10:22:40 pm »
And which classes do you use in Lazarus to connect to Access?

Handoko

  • Hero Member
  • *****
  • Posts: 3631
  • My goal: build my own game engine using Lazarus
Re: .DBF database file
« Reply #2 on: October 19, 2017, 05:19:04 am »
Hello Stoutall,
Welcome to the forum.

You don't need to use MS. Access driver nor ODBC to connect dbf files. But you first need to make sure your Lazarus already has DBFLaz package installed properly:

Lazarus main menu >  Package > Install/Uninstall Packages

If you can see DBFLaz 0.1.1 on the left panel, then it means it's already installed. If it is on the right panel, you should double click on it and click "Save and rebuild IDE", wait some minutes then your IDE will be restarted.

Here I wrote a simple demo to show how to connect dbf file and show its fields' names:

Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, dbf, FileUtil, Forms, Controls, Graphics, Dialogs,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Button1: TButton;
  17.     Dbf1: TDbf;
  18.     OpenDialog1: TOpenDialog;
  19.     procedure Button1Click(Sender: TObject);
  20.   end;
  21.  
  22. var
  23.   Form1: TForm1;
  24.  
  25. implementation
  26.  
  27. {$R *.lfm}
  28.  
  29. { TForm1 }
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. var
  33.   S: string;
  34.   i: Integer;
  35. begin
  36.   if not(OpenDialog1.Execute) then Exit;
  37.   S := 'Field Names:';
  38.   try
  39.     with Dbf1 do
  40.       begin
  41.         FilePathFull := ExtractFileDir(OpenDialog1.FileName);
  42.         TableName := ExtractFileName(OpenDialog1.FileName);
  43.         Open;
  44.         for i := 0 to (FieldDefs.Count-1) do
  45.           S := S + #13 + FieldDefs[i].Name;
  46.         Close;
  47.       end;
  48.   except
  49.     S := 'Error!'
  50.   end;
  51.   ShowMessage(S);
  52. end;
  53.  
  54. end.

You can download the DBFields.zip for testing.

 

TinyPortal © 2005-2018