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

postingart

New member
Local time
Today, 12:20
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

  • newTest.accdb
    832 KB · Views: 179
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:20
Joined
May 7, 2009
Messages
19,247
you do realize saving Attachment will bloat your db?
 

Attachments

  • newTest.accdb
    520 KB · Views: 163

postingart

New member
Local time
Today, 12:20
Joined
Jun 27, 2021
Messages
17
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:20
Joined
May 7, 2009
Messages
19,247
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?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:20
Joined
May 7, 2009
Messages
19,247
this will save to attachment field.
when delete, it will delete the whole record.
 

Attachments

  • newTest.accdb
    536 KB · Views: 175

postingart

New member
Local time
Today, 12:20
Joined
Jun 27, 2021
Messages
17
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
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 15:20
Joined
May 7, 2009
Messages
19,247
test this demo.
 

Attachments

  • newTest.accdb
    1.1 MB · Views: 181

Saphirah

Active member
Local time
Today, 09:20
Joined
Apr 5, 2020
Messages
163
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

Top Bottom