Saving OLE Objects in Access field with VBA

后端 未结 1 340
失恋的感觉
失恋的感觉 2020-12-12 05:03

I know that this subject has been covered to large extend, but I have not been able to find a solution to my particular problem.

I have a table with a column

相关标签:
1条回答
  • 2020-12-12 05:45

    If you want to store a file as an OLE Package shell object, doing some GUI coding (opening a form with an OLE object, then using that to store the file) is the only way as far as I know.

    Create an unbound form called frmLoadOLEObj, with on it a bound OLE object called MyBoundOLEFrame.

    On the form, add the following code:

    Public Sub SaveOLEObj(rs As DAO.Recordset, fldName As String, FileName As Variant)
        'Save the position of the recordset
        Dim bkmrk As Variant
        bkmrk = rs.Bookmark
        'Bind the form to the recordset
        Set Me.Recordset = rs
        'Move the form to the saved position
        Me.Bookmark = bkmrk
        'Bind the OLE frame to the field
        MyBoundOLEFrame.ControlSource = fldName
        MyBoundOLEFrame.Class = "Package"
        'Load the attachment into the OLE frame
        MyBoundOLEFrame.SourceDoc = FileName
        MyBoundOLEFrame.Action = acOLECreateEmbed
    End Sub
    

    Then, to add a file to a record:

    Dim rsa As DAO.Recordset
    Set rsa = CurrentDb.OpenRecordset("ZipCodeAttachments", dbOpenDynaset, dbSeeChanges)
    Dim frmOLE As New Form_frmLoadOLEObj
    frmOLE.SaveOLEObj rs, "Attachments", Application.CurrentProject.Path & "\Attachments\537.zip"
    

    As you can see, this is very "hacky" code, because it runs GUI operations, and you have code on a form that is not a form, but really a module, but you need a form to put the control on because you can't have the control without the form. I'd rather have a BLOB any day.

    0 讨论(0)
提交回复
热议问题