One of my users has a Microsoft Access database and in the table he has an attachment field. In one of his queries, he wants to return the number of attachments that the field contains. I have tried to get this to work to no avail. I've tried creating a VBA module and passing into it the field but it errors on me. I've tried creating the parameter as a DAO.Recordset, DAO.Field, Attachment, etc.
I've also tried querying the field like this [MyField].AttachmentCount.
I don't have 2007 with me currently to test this, but this article explains how to access the attachments with LoadFromFile and SaveToFile.
See if you can access the count like so (with DAO)... obviously use your table names.
EDIT: Sorry for the delay on this; I haven't had a chance to look at it.
I think this should be a solution for you. I tested it in Access 2010 and it works.
PART 1 - Create a generic function to get the attachment count for any field in any table. Place this code inside a module.
PART 2 - Use the custom function in your Access query.
Parameter 1 is the table where your attachments are, parameter 2 is the field in your table where the attachments are, and the last parameter is a WHERE clause for your table to select the right record.
Hope this helps!
UPDATE
This SQL query also worked for me:
adds all attachments from selected field into a dataset which allows you to then count it
I found the
AttachmentCount
function above failed when the attachment field contained no objects/attachments. To actually get a return value of 0 out of the function in this case I added three lines of code to give the below:Thank you so much for the original function! If it hadn't been for you I'd still be scratching my head as to how to obtain the attachment count.