我目前没有 2007 年来测试这个,但是本文 http://msdn.microsoft.com/en-us/library/bb257442%28office.12%29.aspx解释如何使用 LoadFromFile 和 SaveToFile 访问附件。
看看你是否可以像这样访问计数(使用DAO)...显然使用你的表名。
' Instantiate the parent recordset.
Set rsEmployees = db.OpenRecordset("YourTableName")
''' Code would go here to move to the desired record
' Activate edit mode.
rsEmployees.Edit
' Instantiate the child recordset.
Set rsPictures = rsEmployees.Fields("Pictures").Value
Debug.Print rsPictures.RecordCount'' <- SEE IF THIS GIVES YOU THE COUNT
EDIT:抱歉耽误了这件事;我还没有机会看它。
我认为这应该是您的解决方案。我在 Access 2010 中测试了它并且它有效。
PART 1- 创建一个通用函数来获取任何表中任何字段的附件计数。将此代码放入模块中。
Function AttachmentCount(TableName As String, Field As String, WhereClause As String)
Dim rsRecords As DAO.Recordset, rsAttach As DAO.Recordset
AttachmentCount = 0
Set rsRecords = CurrentDb.OpenRecordset("SELECT * FROM [" & TableName & "] WHERE " & WhereClause, dbOpenDynaset)
If rsRecords.EOF Then Exit Function
Set rsAttach = rsRecords.Fields(Field).Value
If rsAttach.EOF Then Exit Function
rsAttach.MoveLast
rsAttach.MoveFirst
AttachmentCount = rsAttach.RecordCount
End Function
PART 2- 在 Access 查询中使用自定义函数。
SELECT Table1.ID, AttachmentCount("Table1","MyAttach","[ID]=" & [ID]) AS [Num Attach]
FROM Table1;
参数 1 是附件所在的表,参数 2 是表中附件所在的字段,最后一个参数是表中用于选择正确记录的 WHERE 子句。
希望这可以帮助!
UPDATE
这个 SQL 查询也对我有用:
SELECT t.ID, Count(t.MyAttach.FileName) AS [Num Attachments]
FROM Table1 AS t
GROUP BY t.ID;