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?
Asked
Active
Viewed 2,659 times
1
-
http://stackoverflow.com/a/3398696/242520 – ta.speot.is Mar 16 '14 at 07:25
1 Answers
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