使用MS Access VBA添加/查看附件 [英] Add/view attachments using MS Access VBA

查看:159
本文介绍了使用MS Access VBA添加/查看附件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用这些功能来管理附件表,而不使用in Access界面,因此人们无法删除或破坏内容,但是,每次尝试调用以下任何方法时,我都会收到Argument not Optional编译器错误这些功能.

I'm trying to use these functions to manage an attachment table without using the in Access interface so people can't delete or break stuff, however, I'm getting Argument not Optional compiler errors whenever I try and call any of these functions.

在我有按钮的onclick事件中

in the onclick event of a button i have

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

我遇到的另一个问题是,这段代码仅用于从直接路径导入,我确实需要文件选择的文件对话框方法,但是我不确定除了

Another problem I'm having with this is that this code is only for importing from a direct path and I'd really need a file dialog method of file selection, but I'm not sure what to put beyond

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

推荐答案

您的第一个问题来自您没有仔细阅读所提及链接中的代码的事实.
AddAttachment子例程定义为:

Your first problem comes from the fact that you didn't carefully read the code in the link you mention.
The AddAttachment subroutine is defined as:

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

这意味着它具有3个必填参数:

This means that it has 3 mandatory parameters:

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

  • rstCurrent an open recordset for the table where you want to store your file. The file will be added to the recordset current record.

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

strFiledNamethe name of the attachment field where the file will be saved. Your tblAttach table that you created in Access must have at least one Attachment field (and probably other fields as well for information related to the attachment so you can find it, like a document name, and ID, maybe the original path of the document, etc).

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

strFilePath the absolute path to where the file to be attached is located.

第二个问题是让用户通过文件对话框选择所需的文件:

Your second problem is to let users select the file they want through a file dialog:

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()让用户选择文件.该函数将返回文件的完整路径,或者如果操作被取消或未选择文件,则返回一个空字符串.

Call this function SelectFile() to let the user choose a file. The function will return the full path to the file or an empty string if the operation was cancelled or no file selected.

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

For letting the user select the name and location of the file when they want to save the attachment, the code is similar:

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")来建议附件的名称,然后让用户选择保存附件的位置(他们也可以更改名称).该函数会将完整路径返回到您将保存附件的文件.

Call SelectSaveAs("toto.xls") for instance to suggest a name for the attachment and let the user select where they will save it (and they can change the name as well). The function will return the full path to the file where you will save the attachment.

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

Now, you can put everything together.

假设您创建的tblAttach中包含Files字段.
我们可以这样在您提到的链接中重写测试:

Say you have created a tblAttach that has a Files field in it.
We can rewrite the test in the link you mention as such:

    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

要让用户保存回该文件,您将执行类似的操作:打开记录集,移至包含要保存的文件的记录,询问用户文件名,然后将所有这些信息传递给SaveAttachment子例程.

To let the user save back the file, you would do something similar: open the recordset, move to the record that contains the file you want to save, ask the user for the filename, then pass all this information to the SaveAttachment subroutine.

这篇关于使用MS Access VBA添加/查看附件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆