Drag and Drop File into Microsoft Access

纵饮孤独 提交于 2020-05-27 12:37:04

问题


I have a form in Microsoft Access which lets users upload attachments to each record. I'd like to make it a little user friendly by letting users drag and drop files into the attachment field. What is the best way of doing this/how do I do this?


回答1:


Drag and drop might be a bit more sophisticated, how about VBA code to manipulate what you wish to achieve? This article has a great reference to what you wish to do. http://www.access-freak.com/tutorials.html#Tutorial07




回答2:


Here is a way to drag and drop "attached" files for use with MS Access database.

(Currently using Office 365 Version 1811) MS Access currently allows drag and drop to a hyperlink field. Using this capability this example allows drag and drop to store an attachment file to a storage location while keeping a link to the original and new locations. The event runs when a file is dropped into the HyperlinkIn box on the form or when the hyperlink is changed the normal way.

It is better to store the file in a storage location with a link than to store it within the .accdb file due to the 2GB limitation. You might call this a database + file server architecture. By using the record number and optionally the database and table name and attachment number you can ensure unique file names.

Make a Table and Form with 3 fields. ID (AutoNumber) HyperlInkIN (hyperlink) HyperLinkOUT (hyperlink)

Insert this VBS code for AfterUpdate event for the HyperlinkIn form control.

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

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

I am somewhat inexperienced with vba so there may be some better ways to ensure and verify a successful file copy but this example works for me and is easy for me to understand. I used the MsgBox to help debug with the actual file copy commented out.




回答3:


Because this page comes as first when searching for "MS Access drag drop", I'm adding my part here. If you are after some cool UI, you can checkout my Github for sample database using .NET wrapper dll. Which allows you to simply call a function and to open filedialog with file-drag-and-drop function. Result is returned as a JSONArray string.

code can be simple as

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

here what it looks like;



来源:https://stackoverflow.com/questions/24915460/drag-and-drop-file-into-microsoft-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!