如何从Microsoft Access中的“附件”字段查询附件数?

时间:2011-08-25 16:51:45

标签: ms-access access-vba

我的一个用户有一个Microsoft Access数据库,在表中他有一个附件字段。在他的一个查询中,他想要返回该字段包含的附件​​数量。我试图让这个工作无济于事。我已经尝试创建一个VBA模块并将其传入该字段,但它对我有误。我尝试将参数创建为DAO.Recordset,DAO.Field,Attachment等。

我也试过像[MyField] .AttachmentCount那样查询字段。

3 个答案:

答案 0 :(得分:8)

我目前没有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;

答案 1 :(得分:3)

当附件字段不包含任何对象/附件时,我发现上面的AttachmentCount函数失败了。要在这种情况下实际从函数中获得0的返回值,我添加了三行代码来提供以下内容:

Function AttachmentCount(TableName As String, Field As String, WhereClause As String)
    Dim rsRecords As DAO.Recordset, rsAttach As DAO.Recordset

    On Error GoTo Handler

    AttachmentCount = 0

    Set rsRecords = CurrentDb.OpenRecordset("SELECT * FROM [" & TableName & "] WHERE " & WhereClause, dbOpenDynaset)
    If rsRecords.EOF Then Exit Function

    Set rsAttach = rsRecords.Fields(Field).Value
    rsAttach.MoveLast
    rsAttach.MoveFirst

    If rsAttach.EOF Then Exit Function

    AttachmentCount = rsAttach.RecordCount

Handler:
    Exit Function

End Function

非常感谢您原来的功能!如果它不适合你,我仍然会为如何获得附件计数而烦恼。

答案 2 :(得分:0)

将所选字段中的所有附件添加到数据集中,然后您可以对其进行计数

    OleDbConnection connect = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='db/Adb.accdb'"); //set up connection
    //CL_ID is a fk so attachments can be linked to users
    OleDbCommand sql = new OleDbCommand("SELECT at_ID, [at_Name].[FileData], [at_Name].[FileName], [at_Name].[FileType] FROM Attachments WHERE at_ID =1;", connect);
    //adding sql to addapter to be ran

    OleDbDataAdapter OleDA = new OleDbDataAdapter(sql);
    //attempting to open connection
    try { connect.Open(); }
    catch (Exception err) { System.Console.WriteLine(err); }

    DataSet Set1 = new DataSet();
    OleDA.Fill(Set1); //create and fill dataset
    connect.Close();

    Set1.Tables[0].Rows.Count;