[SOLVED] Performing Maintenance on Dbf Files problem... lazarus / 63-Database ---------------------------------------------------------------------- 1HuntnMan · 2023-12-18 19:33 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? ---------------------------------------------------------------------- 1HuntnMan · 2023-12-19 12:12 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 · 2023-12-19 16:49 Resolved, just made the File Maintenance a different Application! ---------------------------------------------------------------------- 1HuntnMan · 2023-12-27 15:13 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 · 2023-12-28 02:15 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   DbfSTProvCodes.Close;  DbfCountries.Close;  DbfCategories.Close;  DbfContacts.Close;  FrmRebldCntksDB.ShowModal;  DbfSTProvCodes.Open;  DbfCountries.Open;  DbfCategories.Open;  DbfContacts.Open; ---------------------------------------------------------------------- 1HuntnMan · 2023-12-28 12:48 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 · 2023-12-28 13:17 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. ---------------------------------------------------------------------- Handoko · 2023-12-28 14:21 +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.