1 (edited by novanbr 2021-09-18 23:03:31)

Topic: How to delete the attachment file (DBFile)?

Hello Sir,
I need help, when i delete a record/data with the atachment/DBFile (i use "LinkFile") from TableGrid, the record is deleted but the attachment is not (its still there in the folder).
What I want is when I delete a record from TableGrid, the attachment (DBFile) is deleted too, how to do that?.
Please help me Sir, thank you.

2 (edited by CDB 2021-09-19 00:43:33)

Re: How to delete the attachment file (DBFile)?

You will need to do this using Script.

In the AfterClick event of your delete button you will need to run the deletefile function.


//Note not tested.

procedure frmYourFormName_Button1_OnAfterClick (filetext : string);
var FileName : string;
begin
  FileName := filetext;
  if FileExists(FileName) then
  begin
     DeleteFile(FileName);
  end
  else
    MessageBox('File ' + FileName + 'does not exist ','Delete Error ' , MB_OK+MB_ICONERROR);
end;

There are at least a couple of ways on how to pass the filename to the Button event.

First is to have a label or textedit that has its Visible property set to false and write the SQL to populate the label Caption or the Text of an edit text and then pass that to the filetext parameter.

or call the Button_OnClick event and write the delete SQL code there.

It depends on whether you have already written code to delete the entry in the database or if you are letting MVD do it for you behind the scenes.

var
 localImagePath : string;
ImageId:string;
begin
ImageId := intToStr(frmYour_form_name.your_component_name.dbItemID);

localImagePath := SQLExecute(' SELECT your_database_field_name FROM your_table_name WHERE id="'+ImageId+'"');
frmEdit.text := localImagePath;

// The above code needs to be called before you delete the database entry if you are using the inbuilt delete function of MVD. If you are writing the code yourself for the complete delete function, you could combine the above code into the button click event. Unless you want a universal function to call for other file deletions.
end;
On a clear disk you can seek forever

Re: How to delete the attachment file (DBFile)?

Hello Mr.CDB,
Thank you verry much for your help, sir.
I'm using your code in the Delete button.
Its working good, it can delete the files and the photos
But unfortunately, its deleted after I push Delete button
Not after the Yes button in conformation windows.
(After I push delete button, confirmation windows show
2 option button Yes and Cancel button).

So how to make it work in the Yes button in confirmation windows?.
I cannot access it, because confirmation windows is builtin MVD, not a regular form.
Please help me, sir.
Thank you.

Post's attachments

Attachment icon How_to_delete_file.rar 905.09 kb, 164 downloads since 2021-09-22