简体   繁体   English

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

[英]How to query number of attachments from Attachment field in Microsoft Access?

One of my users has a Microsoft Access database and in the table he has an attachment field.我的一个用户有一个 Microsoft Access 数据库,在表中他有一个附件字段。 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.我试过创建一个 VBA 模块并将该字段传递给它,但它对我出错。 I've tried creating the parameter as a DAO.Recordset, DAO.Field, Attachment, etc.我尝试将参数创建为 DAO.Recordset、DAO.Field、Attachment 等。

I've also tried querying the field like this [MyField].AttachmentCount.我也试过像这样查询字段 [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.我目前没有 2007 来测试这个,但本文解释了如何使用 LoadFromFile 和 SaveToFile 访问附件。

See if you can access the count like so (with DAO )... obviously use your table names.看看你是否可以像这样访问计数(使用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 : 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.我在 Access 2010 中对其进行了测试,并且可以正常工作。

PART 1 - Create a generic function to get the attachment count for any field in any table.第 1 部分- 创建一个通用函数来获取任何表中任何字段的附件计数。 Place this code inside a module.将此代码放在模块中。

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 - Use the custom function in your Access query.第 2 部分- 在 Access 查询中使用自定义函数。

SELECT Table1.ID, AttachmentCount("Table1","MyAttach","[ID]=" & [ID]) AS [Num Attach]
FROM Table1;

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.参数 1 是您的附件所在的表,参数 2 是您的表中附件所在的字段,最后一个参数是您的表选择正确记录的 WHERE 子句。

Hope this helps!希望这可以帮助!

UPDATE更新

This SQL query also worked for me:这个 SQL 查询也对我有用:

SELECT t.ID, Count(t.MyAttach.FileName) AS [Num Attachments]
FROM Table1 AS t
GROUP BY t.ID;

I found the AttachmentCount function above failed when the attachment field contained no objects/attachments.当附件字段不包含对象/附件时,我发现上面的AttachmentCount函数失败。 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:在这种情况下,为了实际从函数中获得 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

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.如果不是你,我还在为如何获得附件计数而挠头。

adds all attachments from selected field into a dataset which allows you to then count it将选定字段中的所有附件添加到数据集中,然后您可以对其进行计数

    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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM