【问题标题】:Add/view attachments using MS Access VBA使用 MS Access VBA 添加/查看附件
【发布时间】:2018-07-14 02:52:01
【问题描述】:

我正在尝试使用这些函数来管理附件表,而无需使用 Access 接口,因此人们无法删除或破坏内容,但是,每当我尝试调用任何这些功能。

http://blogs.office.com/b/microsoft-access/archive/2007/08/24/adding-removing-and-saving-files-from-the-new-attachment-field-in-access-2007.aspx

在我拥有的按钮的 onclick 事件中

Database.OpenRecordset tblAttach
Recordset.AddNew
Call AddAttachment
Recordset.Update

我遇到的另一个问题是此代码仅用于从直接路径导入,我确实需要文件选择的文件对话框方法,但我不知道该放什么

Dim f As Object
Set f = Application.FileDialog(3)
f.AllowMultiSelect = False
'*** not sure how to get the path to f to insert it into the table
f.Show

【问题讨论】:

  • 那个链接失效了。

标签: vba ms-access


【解决方案1】:

您的第一个问题来自您没有仔细阅读您提到的链接中的代码。
AddAttachment 子程序定义为:

AddAttachment(ByRef rstCurrent As DAO.Recordset, _
              ByVal strFieldName As String, _
              ByVal strFilePath As String)

这意味着它有 3 个强制参数:

  • rstCurrent 要存储文件的表的打开记录集。该文件将被添加到记录集当前记录中。

  • strFiledName将保存文件的附件字段的名称。您在 Access 中创建的 tblAttach 表必须至少有一个附件字段(可能还有其他字段,以获取与附件相关的信息,以便您可以找到它,例如文档名称和 ID,可能是原始路径文件等)。

  • strFilePath要附加的文件所在的绝对路径。

您的第二个问题是让用户通过文件对话框选择他们想要的文件:

Public Function SelectFile() As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogOpen)
    With fd
        .AllowMultiSelect = False
        .Title = "Please select file to attach"
        If .show = True Then
            SelectFile = .SelectedItems(1)
        Else
            Exit Function
        End If
    End With
    Set fd = Nothing
End Function

调用这个函数SelectFile()让用户选择一个文件。如果操作被取消或未选择文件,该函数将返回文件的完整路径或空字符串。

为了让用户在保存附件时选择文件的名称和位置,代码类似:

Public Function SelectSaveAs(initialName As String) As String
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogSaveAs)
    With fd
        .Title = "Save Attachment"
        .InitialFileName = initialName
        If .show = True Then
            SelectSaveAs = .SelectedItems(1)
        Else
            Exit Function
        End If
    End With
End Function

例如,致电SelectSaveAs("toto.xls") 为附件建议一个名称,并让用户选择他们将保存它的位置(他们也可以更改名称)。该函数将返回保存附件的文件的完整路径。

现在,您可以将所有内容放在一起。

假设您创建了一个 tblAttach,其中包含一个 Files 字段。
我们可以在您提到的链接中重写测试:

    Dim dbs As DAO.database
    Dim rst As DAO.RecordSet

    ' Ask the user for the file
    Dim filepath As String
    filepath = SelectFile()

    ' Check that the user selected something
    If Len(filepath) = 0 Then
        Debug.Assert "No file selected!"
        Exit Sub
    End If

    Set dbs = CurrentDb
    Set rst = dbs.OpenRecordset("tblAttach")

    ' Add a new row and an attachment
    rst.AddNew
    AddAttachment rst, "Files", filepath
    rst.Update

    ' Close the recordset
    rst.Close
    Set rst = Nothing
    Set dbs = Nothing

要让用户保存文件,您可以执行类似的操作:打开记录集,移动到包含您要保存的文件的记录,询问用户文件名,然后将所有这些信息传递给 @ 987654334@子程序。

【讨论】:

  • OP 的链接已失效。谁能找到 AddAttachment 的完整代码?我在 Microsoft.com 上找不到它
【解决方案2】:
'----- code to browse, select file and attach to access table
'----- Thanks a lot for earlier submissions, I have just put together all related codes
'----- the code is to add attachments to the attachment field in Ms Access Table. If you don't want to bring up built in form, you can use this code to browse the file and attach
'------ please add this code as Module in Ms Access.  
'------ being a public function, you call this code from any form just by adding 4 parameters  for example  Table name is EmpMaster, Attachment field name is Empcertificate, name of the ID field is EmpID, record ID number say 101
'----- Add_Attachment "EmpMaster", "Empcertificate", "EmpID", 101
'---- it works for me

Option Compare Database
Public Function Add_Attachment(strTableName, 
strAttachField, strIDfield As String, i As Long)

'------------ code to browse file and select file to attach
    Dim fd As FileDialog
    Dim oFD As Variant
    Dim strFileName As String

    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    With fd
        .ButtonName = "Select"
        .AllowMultiSelect = False
        .Title = "Choose File"
        .InitialView = msoFileDialogViewDetails
        .Show

        For Each oFD In .SelectedItems
            strFileName = oFD
        Next oFD
        On Error GoTo 0
    End With

    Set fd = Nothing

'------------ from here code for file attachment process

    Dim cdb As DAO.Database, rstMain As DAO.Recordset, rstAttach As DAO.Recordset2, _
        fldAttach As DAO.Field2
    Set cdb = CurrentDb
    Set rstMain = cdb.OpenRecordset("SELECT " & strAttachField & " FROM " & strTableName & " where " & strIDfield & "= " & i, dbOpenDynaset)

    rstMain.Edit
   Set rstAttach = rstMain(strAttachField).Value
    rstAttach.AddNew

    Set fldAttach = rstAttach.Fields("FileData")

    fldAttach.LoadFromFile strFileName
    rstAttach.Update
    rstAttach.Close
    Set rstAttach = Nothing
    rstMain.Update
    rstMain.MoveNext
rstMain.Close
Set rstMain = Nothing
Set cdb = Nothing
End Function

【讨论】:

  • 感谢您提供此代码 sn-p,它可能会提供一些有限的即时帮助。 proper explanation 将通过展示为什么这是解决问题的好方法,并使其对有其他类似问题的未来读者更有用,从而大大提高其长期价值。请edit您的回答添加一些解释,包括您所做的假设。
  • 希望我添加的解释有点道理。
猜你喜欢
  • 2013-01-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多