Topic: How to disable save button?

.I'm adding and saving records on same form. In this case I checked off "Close the current form after saving" option on save action settings dialog window..


Once record saved, I like to disable save button until record edited to avoid duplicate records on accidental click on save button. Ideally, once record saved, save button and edit fields becomes inaccessible, Fields become editable once user clicks on edit button.and save button becomes active when something changed on edit fields.

Adam
God... please help me become the person my dog thinks I am.

2 (edited by mathmathou 2016-09-29 04:34:26)

Re: How to disable save button?

Hello Adam,


Again an interesting question !


Here is what I would try if I were you :


Assuming you use the same form, and you use "hard coded new record and save" functions (no code on your part, just MVD doing it's job :

1- set edit fields and save button to "disabled"
2- just before the edit activates, enable them
3- just after the save action, disable them again


Disabling  AFTER the save action is easy, you just have to use the OnAfterClick property of the save button like :

procedure Form1_SaveButton_OnAfterClick (Sender: string);
begin
     Form1.Edit1.Enabled := False;
     Form1.Edit2.Enabled := False;
     // and so on for as many buttons as you have

     Form1.SaveButton.Enabled := False;
end;

Unfortunately, there is no OnBeforeClick property, so clicking on the "New record" button, can not enable the edit fields and button (that you previously had disabled by default).


What you could use is the OnMouseDown property. This will detect if a mouse button is clicked over the corresponding button, which is equivalent to an imaginary OnBeforeClick


procedure Form1_NewRecordButton_OnMouseDown (Sender: string; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
begin
     Form1.Edit1.Enabled := True;
     Form1.Edit2.Enabled := True;
     // and so on for as many buttons as you have

     Form1.SaveButton.Enabled := True;     
end;

Try it for yourself and let me known if it worked, and don't forget to set your edit fields and save button to disabled by default.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

3 (edited by AD1408 2016-09-29 10:50:13)

Re: How to disable save button?

Hi Mathias,


Thanks a lot for the info and code....


Unfortunately, I couldn't make it work. I applied to frmCustomer. Sample project is attached.

Post's attachments

Attachment icon Inseritng Records.zip 20.13 kb, 395 downloads since 2016-09-29 

Adam
God... please help me become the person my dog thinks I am.

Re: How to disable save button?

Hello AD1408

I look at your project Inserting Records
I only consider form frmCustomers

When I click on Button Add NewCustomer, no entrie is possible, fields of Panel1 (Add New / Edit ...) remain disabled.
Because action of this button (NewRecord), calls frmCustomers.

But why clear all those fields since New Record Share (called by Add New Customer) automatically empty fields ?

On other side, if absolutely you want clear all fiels,instead of draining fields one by one, it is faster to use a loop that will read all these fields (of type TEdit) as : (see attachment

Otherwise why use the OnMouseDown procedure, it has no reason to be used here ?

Is it really usefull for your project to activate/deactivate button Save ?
It smells a bit the style effect.

I absolutely critical not how you think your project and encode (I've been there too during my early developer) but I quickly realized that the end user wants only one thing: that the program quickly leads to the essential.

As with every new project, I recommend pencil and paper to plan for (and note any improvements). But I have to be a dotard

I keep your project on the screen and continue to arrange for it runs perfectly.

JB

Post's attachments

Attachment icon Code.jpg 8.86 kb, 341 downloads since 2016-09-29 

5 (edited by AD1408 2016-09-29 15:32:22)

Re: How to disable save button?

Hi JB,


Thanks for the loop code about clearing fields.


"- Is it really usefull for your project to activate/deactivate button Save ?"


Yes, unless there is another way to stop saving duplicate records.


My original question:
I'm adding and saving records on same form. In this case I checked off "Close the current form after saving" option on save action settings dialog window..

Once record saved, I like to disable save button until record edited to avoid duplicate records on accidental click on save button. Ideally, once record saved, save button and edit fields becomes inaccessible, Fields become editable once user clicks on edit button.and save button becomes active when something changed on edit fields.


___________________________________________________________

Edit:

JB that loop code for clearing fields didn't work for me unless I did something wrong:

 procedure frmCustomer_btnClear_OnClick (Sender: string; var Cancel: boolean);
 var i : integer;
 begin
  for i := 0 to frmCustomer.ControlCount - 1 do
   begin
    if frmCustomer.Controls[i] is TEdit then
       TEdit (frmCustomer.Controls[i]).Text := '';
   end;
  end;

I appreciate and agree fully about your advice of planning before starting to develop. This is a test project JB.. Before committing to main project, I'm trying to resolve sticky points (for me) first.

Adam
God... please help me become the person my dog thinks I am.

Re: How to disable save button?

Hello AD1408

I better understand your project.

About my piece of code, you're right. There is a mistake.
I wrote the head by reading your save procedure.
I will resume at leisure by finding documentation.

About your fear to save duplicates, you could use this Dmitry's code to control  duplicates (se attachment)

I continue to work on your project and stay current

JB

Post's attachments

Attachment icon CHECK DUPLICATE.zip 6.24 kb, 394 downloads since 2016-09-29 

Re: How to disable save button?

Hi Adam, Jean, Math, (League of Nations - LOL!),

I had a quick look at your issue and (as always) try to take the easy / lazy) route!!  Rather than enable / disable various buttons, can you not make the fields on the form invisible and then reveal them when add / edit a record.  As all your fields are in a panel, you can simple make the panel invisible rather than having to do it for each field (panel properties take precedence over objects contained within the panel so can be a great time-saver - and look quite neat too).
Anyway, for what it's worth, I've attached a version of your project doing it this way so have a look and maybe it will be useful.
Derek.

Post's attachments

Attachment icon Inseritng Records.zip 354.03 kb, 399 downloads since 2016-09-29 

Re: How to disable save button?

Hello Derek, Adam, Math, Jean (heu no, Jean it's me !)

Derek, if you hide panel1 in Event OnAfterClidk for Button Save,
then no need to use procedure derekclearform.     

So I tested this :

1 - Behind event frmCustomer.OnShow :  write : frmCustomer.Panel1.Visible := False;

2 - Behind eventOnClick for button Add Customer : write : frmCustomer.Panel1.Visible := True;

3 - Behind event OnAfterClick for button Save : write : frmCustomer.Panel1.Visible := False;

As Button Add Customer calls action New Record, then when panel1 becomes visible, all fields of this panel will be empty because this action drains automatically all the fiels involved.

Therefore no need procedure DerekClearForm.

Are you OK with me ?

JB

Re: How to disable save button?

Ingenious solution Derek. That'll do nicely. Thanks a lot.....


Since JB pointed out, I realized that I'd also need to implement duplicate records checking. I translated Dmitry's code, into frmCustomer but it didn't work. Looks like I'm lost in translation. Here is the code I pasted and changed form. field and button names:

function CheckDublicate (Action, sTable, sField, sValue: string; id: integer;): boolean;
var
   s: string;
begin
     result := False;
     if Action = 'NewRecord' then
     begin
          s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE '+sField+' LIKE "' + sValue + '"');
          if StrToInt(s) > 0 then result := True;
     end;

     if Action = 'ShowRecord' then
     begin
          s := SQLExecute ('SELECT Count(*) FROM '+sTable+' WHERE ('+sField+' LIKE "' + sValue + '") AND (id <> '+ IntToStr(id) +')');
          if StrToInt(s) > 0 then result := True;
     end;
end;



procedure frmCustomer_btnSaveCustomer_OnClick (Sender: string; var Cancel: boolean);
begin
     if CheckDublicate(frmCustomer.dbAction, 'Customer', 'Cust_Surname', frmCustomer.EdCust_Surname.Text, frmCustomer.btnSaveCustomer.dbGeneralTableId) AND
        CheckDublicate(frmCustomer.dbAction, 'Customer', 'Cust_Name', frmCustomer.EdCust_Name.Text, frmCustomer.btnSaveCustomer.dbGeneralTableId) then
     begin
          ShowMessage('Person already exists.');
          Cancel := True;
     end;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: How to disable save button?

Bonsoir JB, Adam, tout le monde!
The reason why I created the 'derekclearform' procedure is to stop a slight error happening.  For example:
1.  You click 'add a new record' but maybe get interrupted, so you press 'cancel' instead of 'save'. 
2.  The next time you 'click add a new record', the 2 'not null' edit fields (in this case, 'name' and 'surname') have not been cleared out. 
Admittedly, 'derekclearform' could have just specified the 2 'not null' fields but for completeness, I added the others as well.
Hope that makes sense.
Derek.

Re: How to disable save button?

Hello Adam,


Wooo, I went to bed and, wooosh !! In the morning many answers to your question :-)


OK, I took the project back from your answer to me, with the code you posted after my answer, because I'm at work and have only little time to review what my very estimated comrades have written (which is certainly very good).


A few points you have to pay attention to but, again, maybe they have already been mentioned by the others :


- On the frmCustomer, the search button was configured a bit wrong :

link it only with the EdSearchCust... edits (4 of them)

the way you configured it, the search button has to be clicked to perform the search but, if you add Button5 in the Increm. Search property of each edit fields, the search is performed without clicking on the button, change event in the edit fields will do it for you.

The save button is linked in the property editor to 'frmCustomer_btnSaveCustomer' in the OnAfterClick event, but this procedure does not exist whereas the procedure frmCustomer_btnSaveCustomer_OnAfterClick (Sender: string); is found at line 42 in your code. Delete the incorrect reference in the OnAfterClick field event and double click the field again to link the correct one.

- on the code part, here is what I wrote which works :

//FOR THE  NEW RECORD BUTTON
procedure frmCustomer_btnNewRecordCustomer (Sender: string; MouseLeft, MouseRight, MouseMiddle: boolean; Shift, Alt, Ctrl: boolean; X, Y: Integer);
begin
    frmCustomer.EdCust_Name.Enabled := True;
    frmCustomer.EdCust_Surname.Enabled := True;
    frmCustomer.EdCust_Street.Enabled := True;
    frmCustomer.EdCust_Area.Enabled := True;
    frmCustomer.EdCust_City.Enabled := True;
    frmCustomer.EdCust_State.Enabled := True;
    frmCustomer.EdCust_ZipCode.Enabled := True;
    frmCustomer.EdCust_Country.Enabled := True;
    frmCustomer.btnSaveCustomer.Enabled := True;
end;
//FOR THE SAVE BUTTON
procedure frmCustomer_btnSaveCustomer_OnAfterClick (Sender: string);
begin
    frmCustomer.EdCust_Name.Clear;
    frmCustomer.EdCust_Surname.Clear;
    frmCustomer.EdCust_Street.Clear;
    frmCustomer.EdCust_Area.Clear;
    frmCustomer.EdCust_City.Clear;
    frmCustomer.EdCust_State.Clear;
    frmCustomer.EdCust_ZipCode.Clear;
    frmCustomer.EdCust_Country.Clear;
    frmCustomer.EdCust_Name.Enabled := False;
    frmCustomer.EdCust_Surname.Enabled := False;
    frmCustomer.EdCust_Street.Enabled := False;
    frmCustomer.EdCust_Area.Enabled := False;
    frmCustomer.EdCust_City.Enabled := False;
    frmCustomer.EdCust_State.Enabled := False;
    frmCustomer.EdCust_ZipCode.Enabled := False;
    frmCustomer.EdCust_Country.Enabled := False;
    frmCustomer.btnSaveCustomer.Enabled := False;
end;

As for the CheckDuplicate function you pointed out.... well.... errrrr..... I don't like it very much. I have my own way of doing it with case sensitiveness and I don't use this one (no offense Dmitry). It's just a bit long to detail here.


Anyway, have a good day / night gentlemen and talk to you soon.


Cheers


Mathias

I'm a very good housekeeper !
Each time I get a divorce, I keep the house

Zaza Gabor

12 (edited by AD1408 2016-09-30 09:22:48)

Re: How to disable save button?

Hi Mathias,


Thanks a lot for the info....


I'm sure I'm doing something incorrectly, as your latest code didn't work at my end. When you have time, if possible, please apply
- disabling/enabling
- your duplicate checking to sample project and upload so that I can where are my mistakes.


Once again thank you for your kind help.


++++++++


One more question Mathias, if I may please..
I noticed you did some days calc example project.
How do I calculate present age of a person from date of birth. I couldn't write a script for it.

Adam
God... please help me become the person my dog thinks I am.

Re: How to disable save button?

Hi Adam, Mathias, JB,

About duplicate checking - Mathias, you said that you have your own way of checking that includes case sensitivity.  Could you show me how you do that?  Like you, I use my own duplicate checking routine - basically, I use a 'pre-save' button (that also shows duplicates for entries entered in different case (UPPER, lower and MiXed) but I would be interested to see how you do it.
I've attached the way I approach it in case it's of any use to anyone.
Regards,
Derek.

Post's attachments

Attachment icon duplicatecheck.zip 338.4 kb, 509 downloads since 2016-10-01 

Re: How to disable save button?

Thanks a lot Derek for sharing... I'll check it out.

Adam
God... please help me become the person my dog thinks I am.