Recent

Author Topic: [SOLVED]DBGrid- How to populate  (Read 3556 times)

What I can do

  • Full Member
  • ***
  • Posts: 152
[SOLVED]DBGrid- How to populate
« on: May 04, 2024, 05:54:50 pm »
OS: Win 10
Compiler: Lazarus 3.2
Does any one have working demo of how to access data and populate a DBGrid from a file *.mdb
Code: Pascal  [Select][+][-]
  1. uses
  2.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, DBGrids;
  3.  
  4. type
  5.  
  6.   { TForm1 }
  7.  
  8.   TForm1 = class(TForm)
  9.     DBGrid1: TDBGrid;
  10.   private
  11.  
  12.   public
  13.  
  14.   end;
  15.  
  16. var
  17.   Form1: TForm1;
  18.  
  19. implementation
  20.  
  21. {$R *.lfm}
  22.                  
What connections and dataset and datsxource do I need. I have copyed the https://wiki.freepascal.org/MS_Access and not even got the demo to work. Every thing i view is mostly far more advanced that what I'm look for. The DB file is very small one table less than 100 records
« Last Edit: May 27, 2024, 08:38:00 pm by What I can do »

cdbc

  • Hero Member
  • *****
  • Posts: 2103
    • http://www.cdbc.dk
Re: DBGrid- How to populate
« Reply #1 on: May 04, 2024, 06:24:08 pm »
Hi
1) Search our forum!
2) Have a look at this:https://wiki.lazarus.freepascal.org/ODBCConn
Good luck
Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 3.6 up until Jan 2024 from then on it's both above &: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 4.99

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #2 on: May 04, 2024, 06:27:03 pm »
thank you, I will check that out...

Handoko

  • Hero Member
  • *****
  • Posts: 5407
  • My goal: build my own game engine using Lazarus
Re: DBGrid- How to populate
« Reply #3 on: May 04, 2024, 06:43:51 pm »
I saw you're having compile time issue with the message saying both SQLQuery1 and SQLTransaction1 not found. Have you dropped those components onto the form? If you don't how where to find them, see the video below:

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #4 on: May 04, 2024, 06:54:46 pm »
yes, thank you

but got this error

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #5 on: May 04, 2024, 06:57:03 pm »
is there a problem?

paweld

  • Hero Member
  • *****
  • Posts: 1360
Re: DBGrid- How to populate
« Reply #6 on: May 04, 2024, 06:58:57 pm »
Attached is a sample project - see if it will work for you
Best regards / Pozdrawiam
paweld

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #7 on: May 04, 2024, 07:02:37 pm »
or even this one

Handoko

  • Hero Member
  • *****
  • Posts: 5407
  • My goal: build my own game engine using Lazarus
Re: DBGrid- How to populate
« Reply #8 on: May 04, 2024, 07:05:07 pm »

wp

  • Hero Member
  • *****
  • Posts: 12770
Re: DBGrid- How to populate
« Reply #9 on: May 04, 2024, 07:39:50 pm »
Your posts remind me of my own experiences with Access. Since I am usually working with the 32-bit IDE I was only able to open mdb files when I used the driver specification without the *.accdb (ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb)' - there was no way to get mdb files to load without error from 64-bit applications. Finally I noticed that the corresponding driver, the one with '(*.mdb; *.accdb)' as specified in your code, was not installed by default, at least on my Windows. But it is possible to download and install it from https://www.microsoft.com/en-us/download/details.aspx?id=54920 (Access Database Engine Redistributable). There are downloads for both 32-bit and 64-bit, but you can only install one of them! I installed the 64-bit version since for 32-bit the old driver ('*.mdb') is available by default (at least on my machine).

With the incorrect driver you are alway annoyed by a long barely understandable error message (like in reply #4)!

So, the essence is (after installing the 64-bit Access Database Engine Redistributable):
- for 32-bit applications (i.e. 32-bit Lazarus IDE): use ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb);'
- for 64-bit applications (i.e. 64-bit Lazarus IDE): use ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb; *.accdb);', but only when the 64-bit Access Database Engine Redistributable is installed; if not you cannot use the 64-Lazarus IDE for mdb files.

Regarding your compilation issue: I see in your screenshot of the form in the designer that two components seem to be missing: TSQLTransaction and TSQLQuery. Drop them on the form, and the application should work since all the properties required are set by code in your example.
« Last Edit: May 04, 2024, 07:46:48 pm by wp »

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #10 on: May 04, 2024, 10:47:21 pm »
Thank you, WP
HANDOKO sent me this link
https://www.youtube.com/watch?v=TwiTgeU8t84
but I got an error message....

he had another link to offer as well
https://www.youtube.com/watch?v=Cq5QwKmETxs
but i have not tried that yet.

Now you have given me some other options to test and will post back when I get result.
Thanks to everyone so far.   

The best I judge a quality product is by it's support. The help files in wiki are great if you know what your doing so that's a learning curve but the Lazaus help support more than easily make up for lack of simple details.
Thank everyone again. will be back in toush

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #11 on: May 04, 2024, 11:05:58 pm »
My test code now..
.
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, DB, SQLDB, odbcconn, Forms, Controls, Graphics, Dialogs,
  9.   DBGrids;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     DataSource1: TDataSource;
  17.     DBGrid1: TDBGrid;
  18.     ODBCConnection1: TODBCConnection;
  19.     SQLQuery1: TSQLQuery;
  20.     SQLTransaction1: TSQLTransaction;
  21.     procedure FormCreate(Sender: TObject);
  22.   private
  23.  
  24.   public
  25.  
  26.   end;
  27.  
  28. var
  29.   Form1: TForm1;
  30.  
  31. implementation
  32.  
  33. {$R *.lfm}
  34.  
  35. { TForm1 }
  36.  
  37. procedure TForm1.FormCreate(Sender: TObject);
  38. begin
  39.   ODBCConnection1.Driver:='Microsft Access Driver (*.mdb)';
  40.   ODBCConnection1.Params.Add('DBQ=CarCare.mdb');
  41.   ODBCConnection1.Params.Add('Locale Identifier= 1031');
  42.   ODBCConnection1.Params.Add('ExtendedAnsiSQL=0');
  43.   ODBCConnection1.Params.Add('CHARSET= ansi');
  44.   ODBCConnection1.Connected:=TRUE;
  45.   ODBCConnection1.KeepConnection:=TRUE;
  46.  
  47.   SQLTransaction1.DataBase:= ODBCConnection1;
  48.   SQLTransaction1.Action:= caCommit;
  49.   SQLTransaction1.Active:= TRUE;
  50.  
  51.   SQLQuery1.DataBase:=ODBCConnection1;
  52.   SQLQuery1.UsePrimaryKeyAsKey:=false;
  53.   SQLQuery1.SQL.Text:='SELECT * FROM dbTAble';
  54.   SQLQuery1.Open;
  55.  
  56.   DataSource1.DataSet:=SQLQuery1;
  57.   DBGrid1.DataSource:=DataSource1;
  58.  
  59. end;
  60.  
  61. end.
« Last Edit: May 04, 2024, 11:29:04 pm by What I can do »

What I can do

  • Full Member
  • ***
  • Posts: 152
Re: DBGrid- How to populate
« Reply #12 on: May 04, 2024, 11:58:34 pm »
OK Updated to 'Microsoft Access Driver (*.mdb; *.accdb);
made the addition in my  ODBC Data Sources (64-bit)
in my Lazarus source code
big improvement now the error message is like triple in size ....

wp

  • Hero Member
  • *****
  • Posts: 12770
Re: DBGrid- How to populate
« Reply #13 on: May 05, 2024, 12:12:57 am »
Typo in 'Microsft' - should be 'Microsoft'.

Do you have a 32-bit Lazarus or a 64-bit Lazarus? You can use "ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb)';" only in the 32-bit IDE. For 64-bit you need  the newer "ODBCConnection1.Driver := 'Microsoft Access Driver (*.mdb; *.accdb)';".

And I never made any changes to the ODBC Data sources.

Did a few more experiments and found that the newer 64-bit version has issues with ancient mdb files created by Access 97. As far as I can tell it seems that the old 32-bit driver can open all .mdb files.

Handoko

  • Hero Member
  • *****
  • Posts: 5407
  • My goal: build my own game engine using Lazarus
Re: DBGrid- How to populate
« Reply #14 on: May 05, 2024, 04:43:54 am »
I looked into the reply #12, these lines of the error message:

DBQ=CarCare.mdb
NativeError: -1044
Message: [Microsoft][ODBC Microsoft Access Driver] Not a valid file name.


I think maybe a full path file name is required. Searching the web, I found these comments:
https://github.com/mkleehammer/pyodbc/issues/922
https://stackoverflow.com/questions/40616189/python-odbc-connection-not-a-valid-file-name-error
https://stackoverflow.com/questions/72355227/odbc-microsoft-access-driver-not-a-valid-file-name

 

TinyPortal © 2005-2018