1 (edited by AD1408 2017-03-07 14:20:44)

Topic: Delete on cancel to avoid blank record

I have mentioned something similar before but couldn't find any solution.


There is an issue with MVD when two stages of adding a new record used. I can understand that in order to save second level it needs to save first level after initiating second level add new records as a blank or incomplete record. It's all fine up to here. However, when I click on cancel button on level 1 form, I'd like blank or incomplete record to be deleted rather than deleting them manually on main form tGrid.


Please see attached MVD sample project student performance db.
1. On main form click on add student button
2. On student form I made last name field required to avoid saving totally blank record. Then click on add button to add a test after putting some value in last name field.
3. On test window I add some details or cancel without adding any details.
4. Back to student form, I decided not to save the record by clicking on CANCEL button but MVD saves the record. What I'm looking for on click of cancel button, record is deleted and now shown on main form tGrid.

Cancel delete applies when adding new record not when editing existing record.


------------

Post's attachments

Attachment icon Students Blank Record.zip 16.73 kb, 487 downloads since 2017-03-07 

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

Re: Delete on cancel to avoid blank record

Hello.


Check out this script:

procedure frmStudent_btnCancel_OnClick (Sender: string; var Cancel: boolean);
begin
    if (frmStudent.dbAction = 'NewRecord') and (frmStudent.btnSave.dbGeneralTableId<>-1) then
    begin
        SQLExecute('DELETE FROM student WHERE id='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
    end;
end;
Dmitry.

Re: Delete on cancel to avoid blank record

Thank you very much Dmitry...................


It works fine. It's almost there as to what I was looking for.
On new record clicking on Cancel button on student form deletes the record in all circumstances.
Currently it deletes new record, if I initiate second level add (i.e. clicking on add button on student form to add a Test then canceling without save). However, If I save something  on second level, naturally it shows on student form tGrid. What I like to do is clicking on cancel button on students form deletes the record regardless student form tGrid has saved record/s or not - Only on newrecord situation.

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

Re: Delete on cancel to avoid blank record

Try this

procedure frmStudent_btnCancel_OnClick (Sender: string; var Cancel: boolean);
begin
  if (frmStudent.dbAction = 'NewRecord') then
    begin
      SQLExecute('DELETE FROM checks WHERE id_Student='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
      SQLExecute('DELETE FROM student WHERE id='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
    end;
end;

Re: Delete on cancel to avoid blank record

Thank you very much EHW.......................


That's what I wanted do.


I wanted to add your warning after cancel code to it. In this situation giving user another chance before canceling (deleting) the record.
I tried as follows but couldn't make it work... Perhaps they don't fit together?

var
Start_Data, End_Data: String;

procedure frmStudent_Cancel_Button_OnClick (Sender: string; var Cancel: boolean);
begin
    End_Data := Load_Record_String(frmStudent);
    If Start_Data <> End_Data then
      Begin
        If MessageBox('Changes were made to the data on this form.'+#13#10#13#10+ 'Are you sure you want to cancel changes?','Confirm',MB_YESNO+MB_ICONQUESTION) = IDYES then
          frmStudent.Close else
          Begin
          frmStudent.btnSave.SetFocus;
          Cancel := True;
          Exit;
          End;
      End;

    if (frmStudent.dbAction = 'NewRecord') then
      begin
      SQLExecute('DELETE FROM checks WHERE id_Student='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
      SQLExecute('DELETE FROM student WHERE id='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
      end;
end;

procedure frmStudent_OnShow (Sender: string; Action: string);
begin
    Start_Data := Load_Record_String(frmStudent);
end;

function Load_Record_String(Form: TAForm): String;
var
    i,c : Integer;
    Record_String: String;
begin
    c := Form.ComponentCount - 1;
    Record_String := '';
    For i := 0 to c do
    begin
       IF Form.Components[i] is TdbEdit Then Record_String := Record_String + TdbEdit(Form.Components[i]).text;
       IF Form.Components[i] is TdbEditCount Then Record_String := Record_String + TdbEditCount(Form.Components[i]).text;
       IF Form.Components[i] is TdbMemo Then Record_String := Record_String + TdbMemo(Form.Components[i]).text;
       IF Form.Components[i] is TdbComboBox Then Record_String := Record_String + IntToStr(TdbComboBox(Form.Components[i]).dbItemID);
       IF Form.Components[i] is TdbCheckBox Then
         Begin
           If TdbCheckBox(Form.Components[i]).checked = True then Record_String := Record_String + '1'
             else Record_String := Record_String + '0';
         End;
       IF Form.Components[i] is TdbDateTimePicker Then
         Begin
          if TdbDateTimePicker(Form.Components[i]).Checked = False then Record_String := Record_String + ''
           else Record_String := Record_String + DateToStr(TdbDateTimePicker(Form.Components[i]).datetime);
         End;
    end;
    Result := Record_String;
end;
Adam
God... please help me become the person my dog thinks I am.

Re: Delete on cancel to avoid blank record

Change the cancel button code to the following.


procedure frmStudent_btnCancel_OnClick (Sender: string; var Cancel: boolean);
begin
    End_Data := Load_Record_String(frmStudent);
    If Start_Data <> End_Data then
      Begin
        If MessageBox('Changes were made to the data on this form.'+#13#10#13#10+ 'Are you sure you want to cancel changes?','Confirm',MB_YESNO+MB_ICONQUESTION) = IDYES then
          Begin
             if frmStudent.dbAction = 'NewRecord' then
              begin
               SQLExecute('DELETE FROM checks WHERE id_Student='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
               SQLExecute('DELETE FROM student WHERE id='+IntToStr(frmStudent.btnSave.dbGeneralTableId));
              end;
              frmStudent.Close
          End else
          Begin
          frmStudent.btnSave.SetFocus;
          Cancel := True;
          Exit;
          End;
      End;
end;

Also, I noticed you changed the name of the cancel button so you need to use the On_click procedure name above.


One more thing. The method of comparing changes I showed you will work for all top level forms (such as frmStudent in your case), but if you have a lower level form (a child form such as frmCheck in your case) off of the main form and you want to use the same methodology for checking at cancel time, then you will need to define separate global variables for the lower level form (something like Chk_Start_Data and Chk_End_Data) and change the names in the cancel button procedure.

Re: Delete on cancel to avoid blank record

Thanks a lot EHW...........................


Great stuff... Appreciated......

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