Preventing users from removing attachments from the Attachment Control in a Form (1 Viewer)

Cark

Registered User.
Local time
Today, 08:09
Joined
Dec 13, 2016
Messages
153
I have an Attachment Control on a Form and would like to modify the way in which it works so that a user cannot accidentally click "Remove" when they are meaning to click "Add...". Is there a way I can do one or any of the following to introduce a bit more security to keeping the Attachments protected? Like in most situations there will come a time where someone has accidentally attached the incorrect file and I will want to remove it, so could this be controlled in such a way where a password would need to be entered or certain buttons would need to be clicked (e.g to unlock the entire record from being editable?) / a pop-up could appear?

Any examples that people could attach would be really helpful as I am new to the Attachments Control, but can envisage a lot of issues with keeping the attachments linked as users will always find a way of breaking things, so I would like to protect it with as much code as possible.
 

Attachments

  • Capture.PNG
    Capture.PNG
    5.6 KB · Views: 220

theDBguy

I’m here to help
Staff member
Local time
Today, 08:09
Joined
Oct 29, 2018
Messages
21,358
Hi. I don't have an example but the only thing I could think of is to use/create your own custom control where you can design what it can or cannot do.
 

Mark_

Longboard on the internet
Local time
Today, 08:09
Joined
Sep 12, 2017
Messages
2,111
Hi Cark,

Rather than keeping the attachments in your database I strongly suggest keeping a path to them. This avoids issues with database bloat and issues with proper display/edit of attached files.

In your case you would have a child table that holds the paths to attachments (more than one) and can disallow deletions from this table. More to the point, you can also have a file dialog set up that lets your users save attachments to pre-defined folders that you create and then the path to the attachment is saved. You would just need to prevent end users from going in to that folder itself outside of your program.

If this approach will work, I can talk you through several approaches on how to set this up to see if any will work for your specific needs.
 

Cark

Registered User.
Local time
Today, 08:09
Joined
Dec 13, 2016
Messages
153
Hi Mark_,

I had been reading up on attachments and came across the idea of database bloat as you mentioned. Although I don't envisage this particular database reaching the 2gb file size limit, I appreciate it is important to learn new techniques and also to implement best practices so you don't getting bitten in the backside later on down the line because you didn't "envisage" things.

I'm interested in learning all your suggestions. Will these work in subforms?
 

Mark_

Longboard on the internet
Local time
Today, 08:09
Joined
Sep 12, 2017
Messages
2,111
For all approaches you would have a child record that holds the path to the attachment.

Simple approach; User copies attachment to a location then uses a file dialog to find the attachment. Pro: Very easy to set up. Con: Easiest way for the users to mess up and put the attachment in the wrong place.

Reasonable approach; Users uses the file dialog to identify the attachment. Your program then copies the attachment to the proper location and saves the path. Pro: Highly unlikely an end user will put a file in the wrong place. Con: More difficult to set up and requires some type of configuration for where attachments are.

Secure approach; User uses the file file dialog to identify the attachment. File is copied to a location where another process copies it to a save location that users cannot delete from and give the attachment a new name. Pro: Means your end users can't accidentally overwrite or delete attachments. Con: Will take a bit of work to set up and will need a process running on the server to handle copy / rename.

From what I gather your users are at least somewhat familiar with how directories work. As such, using the first approach may work best for you. If you find that users are doing silly things you may need to do more to prevent them from deleting files.

The two biggest advantages this has are 1) You can edit the attachments fairly easily and 2) you can do backups to only get the updated attachments instead of redundant copies of everything.

let us know if you need help with getting any of this to work.
 

Cark

Registered User.
Local time
Today, 08:09
Joined
Dec 13, 2016
Messages
153
All sounds like really good stuff and I appreciate you breaking down the pros and cons of different approaches. Having multiple approaches to pick from is really useful to me so that I can pick based on my individual needs.

Do you by any chance have any examples of this working in practice that you are able to share? I would like to trial the different approaches myself before trying to create one myself from scratch.
 

Mark_

Longboard on the internet
Local time
Today, 08:09
Joined
Sep 12, 2017
Messages
2,111
Here is code I use to find an image and fill in the path for a record:

Code:
Private Sub Btn_FindImage_Click()
Dim asFile As String
Dim afDialog As Office.FileDialog

   Set afDialog = Application.FileDialog(msoFileDialogFilePicker)
   With afDialog
       .Title = "Select Image"
       .InitialView = msoFileDialogViewLargeIcons
       If .Show = True Then
          asFile = Trim(afDialog.SelectedItems(1))
          Me.ImageFile = asFile
          Me.Img.Picture = asFile
       Else
           MsgBox "File selection cancelled by user"
       End If
   End With
End Sub

in the code I'm not limiting the file type because some people like scanning an image into a .PDF file for some strange reason. This code is called from a button. In your case you'd want to cancel saving if they don't actually choose an image file for one of your child records.

I don't have examples of the more elaborate methods, but both rely on copying the file. You would use the basic code above to identify the file and use something like CopyFile to copy from your users selected name to a directory and name you generate.
 

Users who are viewing this thread

Top Bottom