Open Attachment in Database

tmill29

New member
Joined
Jun 8, 2014
Messages
2
Programming Experience
Beginner
I know placing attachments in a database is risky and everyone says do not do it, however that is how the powers want it done. That being said, my listbox contains multiple fields and 1 of those shows there is an attachment in the database, however it does not allow to open that attachment because this is just a listbox of items. What I need is to doubleclick on that line item, which I have it selected already and on a doubleclick, open that attachment. Here is the code I have in my form and I get an error:

VB.NET:
Private Sub ListQuality_DblClick(Cancel As Integer)
    Dim rs As DAO.Recordset
    Set rs = Me.RecordsetClone
    OpenFirstAttachmentAsTempFile rs, ".pdf"
End Sub
Public Sub OpenFirstAttachmentAsTempFile(ByRef rstCurrent As DAO.Recordset, ByVal strFieldName As String)
    Dim rstChild As DAO.Recordset2
    Dim fldAttach As DAO.Field2
    Dim strFilePath As String
    Dim strTempDir As String
    strTempDir = Environ("Temp") ' Get the Temp directory from the environment variable.
    If Right(strTempDir, 1) <> "\" Then strTempDir = strTempDir & "\" ' Make sure the path always ends with a backslash.
    Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying recordset.
    strFilePath = strTempDir & rstChild.Fields("FileName").Value ' Append the name of the first (and only) attached file to temp dir.
    If Dir(strFilePath) <> "" Then ' the file already exists--delete it first.
        VBA.SetAttr strFilePath, vbNormal ' remove any file attributes (e.g. read-only) that would block the kill command.
        VBA.Kill strFilePath ' delete the file.
     End If
    Set fldAttach = rstChild.Fields("FileData") ' The binary data of the file.
    fldAttach.SaveToFile strFilePath
    rstChild.close ' cleanup
    VBA.Shell "Explorer.exe " & Chr(34) & strFilePath & Chr(34), vbNormalFocus ' Use Windows Explorer to launch  the file.
End Sub

The error is on.........set rs = me.recordsetclone

It says: run time 7951 you entered an expression that has an invalid reference to the recordset clone property
 
Do you need to be able to add attachments to the database via this same application? I ask because, if all you want to do is read, it can be done using ADO.NET. If you want to add though, DAO will be required and you may as well stick with DAO for the reading too.
 
No. The attachments are added via a data entry form. All I need is for them to be able to double click in this one area and on double click have the attachment in the database open that pertains to that line item they selected.
 
No. The attachments are added via a data entry form.
To be clear, do you mean that the attachments are added outside your application?
All I need is for them to be able to double click in this one area and on double click have the attachment in the database open that pertains to that line item they selected.
If you are just retrieving then you can do away with DAO altogether and just use ADO.NET:

Retrieving & Viewing Attachments from Access 2007 or Later ACCDB Files
 
Back
Top