我目前没有 2007 来测试这个,但this article 解释了如何使用 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
编辑:抱歉耽搁了;我没有机会看它。
我认为这应该是您的解决方案。我在 Access 2010 中对其进行了测试,并且可以正常工作。
第 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
第 2 部分 - 在 Access 查询中使用自定义函数。
SELECT Table1.ID, AttachmentCount("Table1","MyAttach","[ID]=" & [ID]) AS [Num Attach]
FROM Table1;
参数 1 是附件所在的表,参数 2 是表中附件所在的字段,最后一个参数是 WHERE 子句,供表选择正确的记录。
希望这会有所帮助!
更新
这个 SQL 查询也对我有用:
SELECT t.ID, Count(t.MyAttach.FileName) AS [Num Attachments]
FROM Table1 AS t
GROUP BY t.ID;