1

So i've tried searching for a solution and I often find DAO as the solution but I don't know exactly how to use it, I mean what to import or does it need dlls?

Ciper123
  • 121
  • 1
  • 13

1 Answers1

1

The following VB.NET code uses DAO to add a file named "Sample.pdf" into a field named [AttachmentsField] in the table named [MyTable]:

Imports Microsoft.Office.Interop.Access.Dao

Module Module1

    Sub Main()
        ' this code requires that your project have the following COM Reference:
        '     Microsoft Office 14.0 Access Database Engine Object Library
        Dim dbe As New DBEngine
        Dim db As Database = dbe.OpenDatabase("C:\Users\Public\Database1.accdb")
        Dim rstRecord As Recordset = db.OpenRecordset( _
                "SELECT * FROM MyTable WHERE ID=1", _
                RecordsetTypeEnum.dbOpenDynaset)
        rstRecord.Edit()
        Dim rstAttachments As Recordset2 = rstRecord.Fields("AttachmentsField").Value
        rstAttachments.AddNew()
        Dim AttachmentData As Field2 = rstAttachments.Fields("FileData")
        AttachmentData.LoadFromFile("C:\Users\Gord\Desktop\Sample.pdf")
        rstAttachments.Update()
        rstAttachments.Close()
        rstRecord.Update()
        rstRecord.Close()
        db.Close()
    End Sub

End Module
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418