Recent

Author Topic: [SOLVED] Performing Maintenance on Dbf Files problem...  (Read 2694 times)

1HuntnMan

  • Full Member
  • ***
  • Posts: 244
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
[SOLVED] Performing Maintenance on Dbf Files problem...
« on: December 18, 2023, 08:33:14 pm »
I created a form in which I open all tables used by the application in order to perform maintenance, i.e. packing and reindexing all the tables used by the application.  No issues but when I close the maintenance form, the tables are closed but I didn't close them. So, in the maintenance form OnClose event, I reopened the tables although I never closed them. This doesn't work.  When I close the maintenance form and return to the main form, the tables are closed.

Anyone know why this behavior is happening?
« Last Edit: December 28, 2023, 01:48:57 pm by 1HuntnMan »

1HuntnMan

  • Full Member
  • ***
  • Posts: 244
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Performing Maintenance on Dbf Files problem...
« Reply #1 on: December 19, 2023, 01:12:46 pm »
The main form is just a contacts mgt. form. All the tables are open when the user selects File - Maintenance. The maintenance form kicks off when the user clicks Start and all the tables are packed and reindexed. After the process is completed and they close the Maintenance form, all the tables are closed on the main form. The user has to close out of the app. and restart it in order to use the app. properly. I'm thinking the only way to fix this is to get rid of the Maintenance form and just perform this process everytime the close the system.

1HuntnMan

  • Full Member
  • ***
  • Posts: 244
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Performing Maintenance on Dbf Files problem...
« Reply #2 on: December 19, 2023, 05:49:34 pm »
Resolved, just made the File Maintenance a different Application!

1HuntnMan

  • Full Member
  • ***
  • Posts: 244
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: Performing Maintenance on Dbf Files problem... still an issue!
« Reply #3 on: December 27, 2023, 04:13:38 pm »
Not solved, trying to fix this issue which probably a simple fix. Basically, just attempting to pack/re-index all the tables used by the app. The main form has all the tables in use, not a main form with no tables calling other forms which use the tables.  This is just a client/contacts simple but a bit more involved than a regular contacts mgt. program.  The main form is launched with all the tables open which is clients/contacts.  To perform maintenance on all the tables, which is another form called from the File-Maintenance main menu just does the packing and re-indexing. I can't get this to run because when the maintenance form is accessed and I start the maint. process, it immediately blows up with a file access issue with any of the tables from the main form even though I'm closing the tables just before the maint. form is launched.  I've attached the code from the maint. form along with the error screen shot. If anyone can see the issue more experienced than I would appreciate your input. Tks, 1HuntnMan

Handoko

  • Hero Member
  • *****
  • Posts: 5290
  • My goal: build my own game engine using Lazarus
Re: [NOT SOLVED] Performing Maintenance on Dbf Files problem...
« Reply #4 on: December 28, 2023, 03:15:48 am »
I am not very sure but from what you said:
- The mainform is launched with all tables open
- FrmRebldCntksDB is called for performing tables maintenance
- Running the FrmRebldCntksDB will cause file being used error

Are those above correct? If yes, I believe the fix should be in the mainform. This is my suggestion for fixing the issue:

In the mainform, you need to close all the tables before calling FrmRebldCntksDB and the form should be showed as a modal form, so the code should be something like this:

Code: Pascal  [Select][+][-]
  1.   DbfSTProvCodes.Close;
  2.   DbfCountries.Close;
  3.   DbfCategories.Close;
  4.   DbfContacts.Close;
  5.   FrmRebldCntksDB.ShowModal;
  6.   DbfSTProvCodes.Open;
  7.   DbfCountries.Open;
  8.   DbfCategories.Open;
  9.   DbfContacts.Open;
« Last Edit: December 28, 2023, 09:29:15 am by Handoko »

1HuntnMan

  • Full Member
  • ***
  • Posts: 244
  • From Delphi 7 to Lazarus
    • NewFound Photo Art
Re: [NOT SOLVED] Performing Maintenance on Dbf Files problem...
« Reply #5 on: December 28, 2023, 01:48:35 pm »
Handoko,
  Below is the OnClick event from the mainform to launch the rebuild form. This is the way I changed it per your suggestions, see Handoko comments.  The way I had it before I was just using Form.Show and only closing the Contacts.dbf. The 'finally' is my original code also.

procedure TFrmCntksMain.MnuFileMaintClick(Sender: TObject);
begin
  try
    if DbfContacts.Modified then
      DbfContacts.Post;
    DbfContacts.Close;
    DbfSTProvCodes.Close; // Handoko
    DbfCountries.Close; // Handoko
    DbfCategories.Close; // Handoko
    Application.CreateForm(TFrmRebldCntksDB, FrmRebldCntksDB);
    FrmRebldCntksDB.ShowModal; // Handoko
    // FrmRebldCntksDB.Show;
  finally
    DbfContacts.Open;
    FrmCntksMain.Refresh;
  end;
end;

I had experimented back and forth on this, I think the big solution is showing the form as modal!  I was also originally only closing the Contacts.dbf.

Tks, Donald

CharlyTango

  • Jr. Member
  • **
  • Posts: 70
Re: [SOLVED] Performing Maintenance on Dbf Files problem...
« Reply #6 on: December 28, 2023, 02:17:26 pm »
First of all, an off-topic tip: If you are not forced to use the DBF format, a more modern variant such as SQLite may be more future-proof.
But that's another topic.

If you stick database-sensitive components on a form, all components will of course be closed when you close the form.

For such a purpose there are data modules (File - New - Data Module). This is a quasi invisible form that can remain open and active during the entire runtime of the program. All other forms can then access the data provided there. A modal rorm is not really necessary.

However, keeping the data components in just one form or data module is also worth considering. This only makes sense if all forms in the program should always access the same data (and of course the same table rows). If you change the data record pointer in a form (i.e. you change the displayed row of a table), all other forms in which the same table is used will also change.

In my opinion, it makes more sense (especially with file-based data such as DBF) to use data-sensitive components on the forms where they are needed.

For the special task of data maintenance and reindexing it makes sense to have everything in one place.

The procedure for data maintenance can also be managed in the data module and can also be called from any speed button.
Lazarus stable, Win32/64

Handoko

  • Hero Member
  • *****
  • Posts: 5290
  • My goal: build my own game engine using Lazarus
Re: [SOLVED] Performing Maintenance on Dbf Files problem...
« Reply #7 on: December 28, 2023, 03:21:56 pm »
+1 for CharlyTango.

My solution was a quick fix for OP's issue. I thought OP shouldn't have that bug because that only happened if the code quality was low. CharlyTango mentioned many things that OP can do to improve the code quality.

 

TinyPortal © 2005-2018