Delphi and attachment files in MS access database

1.1k views Asked by At

I search many times in Google, SO, and I can't find anything about working with attachment via delphi, so I decide to write this question.

I have a table in .accdb database called Files with those fields:

IDFile PK AutoIncField,
FileName WideStringField,
FilesAttached WideMemoFiled.

enter image description here

How can I save/load files to/from attachment fields using delphi?

Attach files and graphics to the records in your database

The problem here, in delphi the datatype of FilesAttached is TWideMemoField, when I write ShowMessage(FDTable1FilesAttached.Value); it give just the name of the attachment.

I don't know how to Insert/save files to/from that field using delphi.

1

There are 1 answers

5
MartynA On

It didn't seem that hard to find VBA/C# examples of working with .accdb Attachment fields which should translate fairly easily into Delphi. However, it turned out to be more difficult than I imagined to find something that a) hadn't misunderstood what Attachment fields actually are and b) actually works. Skip to the update section below.

For example, googling

accdb create attachment in vba

gives numerous hits including this one

http://sourcedaddy.com/ms-access/working-with-attachment-fields.html

which you might try as a starting point. It uses MS DAO objects, and includes straightforward code for storing files to Attachment fields and for accessing them. You would need to create a Delphi wrapper unit for the DAO type library, if you don't already have one, using the IDE's Import Type Library

If you would prefer something ADO-based, you might take a look at

https://www.codeproject.com/Questions/843001/Handling-fields-of-Attachment-type-in-MS-Access-us

Update See the function OpenFirstAttachmentAsTempFile in the post by "aspen" (date = 4/11/2012 07:18 am) in this thread

https://access-programmers.co.uk/forums/showthread.php?t=224112&page=2

which shows an apparently successful attempt to extract a file from an attachment field (the thread also contains several other attempts at coding this function).

Note in particular this line

Set rstChild = rstCurrent.Fields(strFieldName).Value ' the .Value for a complex field returns the underlying recordset

which implies that the Value of the attachment field can return a recordset which contains the attached file(s).

Presumably, importing a recent version of the DAO type library into Delphi would allow a Delphi app to do the same thing, and then one could reverse-engineer the rstChild recordset to see how to populate this field in code. I haven't done that yet, though.