我在 Microsoft Access 中有一个表单,允许用户将附件上传到每条记录。我想通过让用户将文件拖放到附件字段中来使其更加用户友好。执行此操作的最佳方法是什么/我该如何执行此操作?
拖放可能更复杂一些,用 VBA 代码来操作您想要实现的目标怎么样?这篇文章对您想做的事情有很好的参考。 http://www.access-freak.com/tutorials.html#Tutorial07
这是一种拖放“附加”文件以与 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 来帮助调试,并注释掉实际的文件副本。
因为在搜索“MS Access 拖放”时此页面是第一个出现的,所以我在这里添加我的部分。如果您想要一些很酷的 UI,您可以查看我的 Github,获取使用 .NET 包装器 dll 的示例数据库。它允许您简单地调用函数并使用文件拖放函数打开文件对话框。结果以 JSONArray 字符串形式返回。
代码可以简单为
Dim FilePaths As String
FilePaths = gDll.DLL.ShowDialogForFile("No multiple files allowed", False)
'Will return a JSONArray string.
'Multiple files can be opend by setting AllowMulti:=true
这是它的样子;