MS Access attachment file vba query (Add, Edit, Delete)

postingart

New member
Local time
Tomorrow, 03:41
Joined
Jun 27, 2021
Messages
17
I want to attached image and save attached image in specific folder by using vba query
i want to add edit and delete query regarding attachment file.

This is save button code

Private Sub cmdSave_Click()
On Error GoTo err_msg

CurrentDb.Execute "INSERT INTO ImageList (ImgTitle,ImgPath) VALUES('" & Me.ImgTitle.Text & "','" & Me.ImageTitle.Picture & "') ", dbFailOnError
CurrentDb.Close

MsgBox " Data Has Been Saved Successfully ", vbInformation + vbOKOnly, canMnu
Me.ImgTitle.Text = ""
Me.ImageTitle.Picture = ""


Exit Sub
err_msg:
MsgBox Err.Source & " " & Err.Number & " - " & Err.Description, vbCritical + vbOKOnly, "Error"
End Sub
 

Attachments

Last edited:
you do realize saving Attachment will bloat your db?
 

Attachments

you do realize saving Attachment will bloat your db?
thanks u sir, but my issue not resolved
when i click on save button then only save image not whole record like image id , image title
  1. i want when i click on save button then save whole record in database ( image save in specific folder)
  2. when click on delete button then delete image with title & id too
thanks
 
1. do you want to save the Image to your table (as attachment) or just save the "path" of
the image to a text field in your table?
 
this will save to attachment field.
when delete, it will delete the whole record.
 

Attachments

1. do you want to save the Image to your table (as attachment) or just save the "path" of
the image to a text field in your table?
Sir i want when click on save button then move image into specific folder like (D:\\ImageFile)
and save image file with ID and when click on delete then image also delete with record
 
test this demo.
 

Attachments

I did not take a look at arnelgp's database, but here are a few code snippets that may help you:

Rich (BB code):
Dim destinationFolder As String
destinationFolder = "C:/tmp/"


Sub CopyFileAndInsertIntoDatabase(fileLink As String)
    Dim fso As New FileSystemObject, fileName As String
    
    fileName = fso.GetFileName(fileLink)
    'I recommend XCOPY because it allows to copy files that are open
    Shell ("XCOPY /Y /Z /f """ & fileLink & """ """ & destinationFolder & """)
    DoCmd.RunSQL "INSERT INTO ImageList (ImgTitle,ImgPath) VALUES('" & Me.ImgTitle.Text & "','" & destinationFolder & fileName "')", dbFailOnError
End Sub

Sub DeleteAttachment(fileLink As String)
    DoCmd.RunSQL "DELETE * FROM ImageList WHERE ImgPath = '" & fileLink & "'
    ' First remove readonly attribute, if set
    SetAttr fileLink, vbNormal          
    ' Then delete the file
    Kill fileLink
End Sub

You can adjust them to your needs :)
 

Users who are viewing this thread

Back
Top Bottom