[SOLVED] Creating the TBufDataset in the data module oncreate event lazarus / 16-Databases ---------------------------------------------------------------------- gmold · 2022-03-12 07:15 Hello, my first post!. I'm new to Lazarus/Pascal but not programming (retired mainframe programmer).  I taught myself 'old Pascal' from this document ==> https://apo.ansto.gov.au/dspace/handle/10238/900  (which is excellent and I enjoyed the exercises  ) and subsequently have decided to 'modernize' and learn Object Pascal. I bought the Lazarus Handbook and in Book1 page 197 there is a demo of how to use a DataModule, DataSource and TBufDataset and link them to a DBgrid an Navigator.  The book says put the CreateDataset in the Oncreate Event which I did.   The program complies, the Form displays but the DBGrid is 'inactive' and the underlying dataset never gets created. I put a break point on the Oncreate procedure in the DataModule unit but the debugger does not stop the program at that point.   Which suggest to me that the code is not run but I'm not sure. Question: Is the OnCreate procedure a good place/correct place to create the underlying table and if there is a better place where would you do it. Glenn Australia.  Lazarus 2.012 FPC 3.20 ---------------------------------------------------------------------- wp · 2022-03-12 09:26 I found the example in the book and followed its steps. The demo is working but some details are missing... Let me start with your issue: You probably did not connect the book code with the OnCreate event of the datamodule. Click on the datamodule (but not on one of the added components). Go to the Object Inspector (the tabbed table at the left of the Lazarus IDE), page "Events", line "OnCreate". Click on the '...' button. The IDE now should add a code skeleton for this event into which you put the code of the book (DatamoduleCreate), or if you wrote that before, the IDE will jump to this method. Repeat the same with the OnDestroy event in which the dataset is saved to disk. When you try to compile you will notice an error that the variable "Application" is not found in the datamodule. This is because "Application" is declared in unit Forms, and this normally is not needed by a datamodule. Add "Forms" to the uses list of the datamodule, and you should pass this one. There is another compilation error in the OnValidate event handler: the function "YearOf" is not found. It is in unit DateUtils - add it to the uses clauses. Your question: Yes, TDatamodule.OnCreate often is the correct place to create the dataset. Another way would be to create it already at designtime: right-click on the bufdataset and select "Create Dataset". Note that this will turn on the "Active" property of the dataset. Switch it back to off because you'd also have to specify the path to the database file in the object inspector then, and this is not a good idea because there is more flexibility when you do this at runtime. ---------------------------------------------------------------------- gmold · 2022-03-12 11:08 Hello, thank you for your reply.  I figured out the application.location error by looking at the picture on page 197 and I noticed that uses forms ; in the implementation sections. I verified this in the fpc documentation and added it and it fixed the issue. I have not added the year validation so the that is not a problem.  What I do not see is the 'create dataset ' option if I right click on the bufdataset. See the attached. The code for the datamodule unit is below.  The methods for create and destroy exist. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit udm; {$mode objfpc}{$H+} interface uses  Classes, SysUtils, DB, BufDataset; type   { TdmStaff }   TdmStaff = class(TDataModule)    bdStaff: TBufDataset;    dsModule: TDataSource;    bdStaffAvailable: TFloatField;    bdStaffName: TStringField;    bdStaffStaffCode: TStringField;    procedure DataModuleCreate(Sender: TObject);    procedure DataModuleDestroy(Sender: TObject);   private     FMyDB : string;  public   end; var  dmStaff: TdmStaff; implementationuses forms  ; {$R *.lfm} { TdmStaff }   procedure TdmStaff.DataModuleCreate(Sender: TObject);begin  FMyDB := IncludeTrailingPathDelimiter(application.location);  if fileexists(fmydb + 'staff.bds') then      bdStaff.LoadFromFile(fmydb + 'staff.bds')  else   bdStaff.createdataset;end; procedure TdmStaff.DataModuleDestroy(Sender: TObject);begin    bdStaff.SaveToFile(fmydb + 'staff.bds');end; end. Glenn, Australia. ---------------------------------------------------------------------- wp · 2022-03-12 11:19 Sorry - I misread the version information in your signature and used Laz 2.2.0/FPC 3.2.2 in my test. You are right - the "Create Dataset" item was added to the TBufDataset's context menu after the release of FPC3.2.0. ---------------------------------------------------------------------- gmold · 2022-03-13 04:36 Hi, I upgraded to Lazarus/Fpc and it works now.   Thank you. Glenn, Australia