我正在尝试使用这些函数来管理附件表,而不使用Access接口,因此人们无法删除或破坏内容,但是,每当我尝试调用任何这些函数时,我都会收到Argument而不是可选编译器错误.
在我的按钮的onclick事件中
Database.OpenRecordset tblAttach
Recordset.AddNew
Call AddAttachment
Recordset.Update
Run Code Online (Sandbox Code Playgroud)
我遇到的另一个问题是这个代码只用于从直接路径导入,我真的需要一个文件选择的文件对话框方法,但是我不知道要放什么
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
Run Code Online (Sandbox Code Playgroud)
您的第一个问题来自于您没有仔细阅读您提到的链接中的代码.
该AddAttachment
子程序被定义为:
AddAttachment(ByRef rstCurrent As DAO.Recordset, _
ByVal strFieldName As String, _
ByVal strFilePath As String)
Run Code Online (Sandbox Code Playgroud)
这意味着它有3个必需参数:
rstCurrent
要存储文件的表的打开记录集.该文件将添加到记录集当前记录中.
strFiledName
要保存文件的附件字段的名称.您tblAttach
在Access中创建的表必须至少有一个"附件"字段(可能还有其他字段以及与附件相关的信息,以便您可以找到它,如文档名称和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
Run Code Online (Sandbox Code Playgroud)
调用此函数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
Run Code Online (Sandbox Code Playgroud)
呼叫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
Run Code Online (Sandbox Code Playgroud)
要让用户保存文件,您可以执行类似的操作:打开记录集,移动到包含要保存的文件的记录,询问用户文件名,然后将所有这些信息传递给SaveAttachment
子例程.
归档时间: |
|
查看次数: |
26791 次 |
最近记录: |