以下是拖放“附加”文件以便与 MS Access 一起使用的方法。 (当前使用Office 365版本1811)
Access 目前允许拖放到超链接字段。
使用此功能,此示例允许拖放将附件文件存储到存储位置,同时保留到原始位置和新位置的链接。
当将文件拖放到表单上的 HyperlinkIn 框中或以正常方式更改超链接时,该事件将运行。
由于 2GB 的限制,最好将文件存储在带有链接的存储位置,而不是将其存储在 .accdb 文件中。您可以将其称为数据库+文件服务器架构。通过使用记录编号以及可选的数据库和表名称以及附件编号,您可以确保文件名的唯一性。
制作一个包含 3 个字段的表和表单。
ID(自动编号)
HyperlInkIN(超链接)
HyperLinkOUT(超链接)
为 HyperlinkIn 表单控件的 AfterUpdate 事件插入此 VBA 代码。
Private Sub HyperlinkIN_AfterUpdate()
Dim InPath As String
Dim FileName As String
Dim OutFolder As String
Dim OutPath As String
Dim RecordNo As String
Dim FileExt As String
OutFolder = "\\networkdrive\vol1\attachments\" 'specify the output folder
InPath = Me!HyperlinkIN.Hyperlink.Address
RecordNo = Me!ID
' BrianDP Added next 3 lines of Code to fix Path if Local to C Drive
If Left(InPath, 6) = "..\..\" Then
InPath = "c:\" & Right(InPath, Len(InPath) - 6)
End If
If Len(InPath) > 0 Then
FileName = Right(InPath, Len(InPath) - InStrRev(InPath, "\")) 'get the file name
FileExt = Right(FileName, Len(FileName) - InStrRev(FileName, ".") + 1) ' get the file extension with dot
'build the new path with output folder path and record number and date and extension
OutPath = OutFolder & "Record " & RecordNo & " Attachment " & Format(Now(), "ddmmmyy") & FileExt
FileCopy InPath, OutPath
Me!HyperlinkOUT = "#" & OutPath & "#"
MsgBox "Copied file to archives " & vbCrLf & InPath & vbCrLf & OutPath
End If
End Sub
我对 VBA 缺乏经验,因此可能有更好的方法来确保和验证成功的文件复制,但这个示例适合我并且很容易理解。我使用 MsgBox 来帮助调试,并注释掉实际的文件副本。