【问题标题】:How to query number of attachments from Attachment field in Microsoft Access?如何从 Microsoft Access 的附件字段中查询附件数量?
【发布时间】:2011-08-25 16:51:44
【问题描述】:

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

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

【问题讨论】:

  • OLE 对象中的附件?如果你直接打开表,你在那个字段中看到了什么?另外,如果您进入设计模式,字段类型是“OLE 对象”吗?
  • 该字段显示一个回形针和一个代表该字段中附件数量的数字。我不相信这会被视为 OLE 对象,但我可能是错的。也就是说,我无法通过我需要的查询访问回形针旁边的号码。

标签: ms-access vba


【解决方案1】:

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

【讨论】:

  • 抱歉尝试...rsPictures.RecordCount;我认为它是嵌入在该字段中的记录集。如果没有,请尝试rsPictures.Count
  • 我读过那篇文章。我的问题不是编写代码,我的问题是在查询中实现这种类型的东西。因此,如果我采用了您正在谈论的代码 sn-p,最终,我需要从查询中调用它并传入该字段。该功能是什么样的?或者,是否已经有一个函数可以在我的查询中调用?它看起来像这样:选择 AttachCount([MyAttachmentField]),另一个字段来自.....
【解决方案2】:

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

非常感谢原始功能!如果不是你,我仍然会为如何获取附件计数而摸不着头脑。

【讨论】:

    【解决方案3】:

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

        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;
    

    【讨论】:

      猜你喜欢
      • 2020-02-04
      • 2014-04-21
      • 2016-12-09
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      相关资源
      最近更新 更多