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

Ms access 如何从Microsoft Access中的“附件”字段中查询附件数?,ms-access,vba,Ms Access,Vba,我的一个用户有一个Microsoft Access数据库,表中有一个附件字段。在他的一个查询中,他希望返回该字段包含的附件数。我试着让它工作,但没有用。我曾尝试创建一个VBA模块并将字段传递给它,但它在我身上出错。我已经尝试将参数创建为DAO.Recordset、DAO.Field、附件等 我也尝试过像这样查询[MyField].AttachmentCount.我现在没有2007来测试这个,但是解释了如何使用LoadFromFile和SaveToFile访问附件 查看是否可以像这样访问计数(使用

我的一个用户有一个Microsoft Access数据库,表中有一个附件字段。在他的一个查询中,他希望返回该字段包含的附件数。我试着让它工作,但没有用。我曾尝试创建一个VBA模块并将字段传递给它,但它在我身上出错。我已经尝试将参数创建为DAO.Recordset、DAO.Field、附件等


我也尝试过像这样查询[MyField].AttachmentCount.

我现在没有2007来测试这个,但是解释了如何使用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
第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;

当附件字段不包含任何对象/附件时,我发现上面的
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

非常感谢您提供的原始功能!如果不是你的话,我仍然在琢磨如何获得附件数量。

将所选字段中的所有附件添加到一个数据集中,这样你就可以进行计数了

    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;

OLE对象中的附件?如果你直接打开表格,你会在那个区域看到什么?此外,如果进入设计模式的字段类型为“OLE对象”,则该字段将显示一个回形针和一个表示该字段中附件数量的数字。我不相信这会被认为是OLE对象,但我可能错了。也就是说,我无法通过我的查询访问回形针旁边的号码,这正是我所需要的。抱歉,请尝试<代码>rsPictures.RecordCount;我认为这是一个嵌入在字段中的记录集。如果没有,请尝试使用rsPictures.Count我读过那篇文章。我的问题不是编写代码,而是在查询中实现这种类型的东西。因此,如果我已经获取了您正在讨论的代码片段,那么最终,我需要从查询中调用它并在字段中传递。这个函数看起来像什么?或者,是否已经有一个我可以在查询中调用的函数?如下所示:选择AttachCount([MyAttachmentField]),另一个字段来自。。。。。
    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;