附加然后将其存储在表格中。 Ms访问

时间:2017-03-24 08:41:48

标签: vba ms-access access-vba

我在MS Access中开发了一种功能,可以将图片附加到表单中,然后将其存储在表格中。但由于我有不同的用户,我希望他们附加图片,然后将这些附件存储在同一个应用程序的表中,但我希望这些附件能够以用户的方式附加,即在表的不同行中附加。

以下是我所做的截图。

enter image description here

以下是我编写的代码

Option Compare Database

'Public Function SaveAttachments(strPath As String, Optional strPattern As
'String = "*.*") As Long
'Public Function LoadAttachments(strPath As String, Optional strPattern As i
'String = "*.*") As Long

Public Function LoadAttachments()
'(strPath As String, Optional StrPattern As String) As Long

    Dim dbs As DAO.Database
    Dim rst As DAO.Recordset2
    Dim rsA As DAO.Recordset2
    Dim fld As DAO.Field2
    Dim strFile As String

    'Get the database, recordset, and attachment field
    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblAttachments")
    Set fld = rst("Fieldatttachment")

    'Navigate through the table
    Do While Not rst.EOF

       'Get the recordset for the Attachments field
        Set rsA = fld.Value

        'Load all attachments in the specified directory
        strFile = Dir(strPath & "\*.*")

        rst.Edit
        Do While Len(strFile) > 0    
            'Add a new attachment that matches the pattern.
            'Pass "" to match all files.
            If strFile Like StrPattern Then
                rsA.AddNew
                rsA("FileData").LoadFromFile strPath & "\" & strFile
                rsA.Update

                'Increment the number of files added
                LoadAttachments = LoadAttachments + 1
            End If
            strFile = Dir
        Loop

        'Save all attachments in the field    
        If rsA("FileName") Like StrPattern Then
                strFullPath = strPath & "\" & rsA("FileName")

        rsA.Close

        rst.Update
        'Next record
        rst.MoveNext
    Loop

    rst.Close
    dbs.Close

    Set fld = Nothing
    Set rsA = Nothing
    Set rst = Nothing
    Set dbs = Nothing
End Function

Private Sub Fieldatttachment_Click()
    Call LoadAttachments
End Sub

2 个答案:

答案 0 :(得分:0)

在表格中添加一个字段UserId。

打开表单时过滤用户的ID。

每当创建/插入记录时,使用用户ID填写此字段(默认情况下)。

将应用程序设计为通常隐藏表,以便用户无法直接访问表。

答案 1 :(得分:0)

考虑将 UserId 字段添加到表中,然后将其从表单作为参数传递给函数 LoadAttachments ,其中使用{{1}}过滤记录集条款。请注意,我通过为其分配数据类型来使函数成为可返回对象。

{{1}}