Drag and Drop File into Microsoft Access - ms-access

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?

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

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.

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;

Related

Can I assign the folder name from form records in application.hyperlink?

I have all folders for clients named using the same file naming rubric. I can use application.hyperlink to open the main folder, but can I use this code to open a subfolder with data from the form records?
Application.FollowHyperlink "w:\main folder\"
Lets assume you have a text box control on your form called txtClientId.
Using the data in that in vba you concatenate that into your hyperlink string;
Dim sPath as String
sPath = "W:\Main Folder\"
sPath = sPath & Me.txtClientId & "\"
Application.FollowHyperlink sPath
I would avoid using a mapped drive letter if possible, much more reliable to use the network share URL as it's not dependent on being set up on the end users local machine. Something like
\\DataServer\MainShare\....

MSAccess - TransferDatabase - File locked open?

I am attempting the following in an AccessDb via VBA:
Export a single table from current DB into a new DB in the same directory, via a Query/TransferDatabase. This seems to work as expected.
Dim ws As Workspace
Dim db_new as Database
strPath = CurrentProject.Path & "\Backend_Database\"
strDBFilename = strPath & Raw_Count_File.accdb"
Set ws = DBEngine.Workspaces(0)
Set db_new = ws.CreateDatabase(strDBFilename, dbLangGeneral)
DoCmd.TransferDatabase acExport, "Microsoft Access", _
strDBFilename, acTable, "tmp_RawCountFile", "Raw_TblMatchedTB"
Within the same function used above (to create the new file), I am attempting next to ZIP the new file into the same directory. The result is 1K Byte ZIP file (it's an empty ZIP ).
If I breakout the code segment that creates the ZIP file into separate function (i.e., under another button), the function works as expected and the proper ZIP file is created.
My Question:
I am guessing the new DB file and subsequent TransferDatabase is leaving the new_db file hanging open and inaccessible to the ZIP function. I attempted to set the various objects = nothing prior to the ZIP function, but same result. Only if I exit the first function and call a second function will it work as desired.
Can I add something to the end of the TransferDatabase function to ensure the resulting file will be available for the ZIP task?
My preference is not to add a secondary button press to this task...
Any suggestions to get me going?
Thanks!
Try to Set db_new = Nothing before zipping, in order to close the newly created db.

Browse to file removing link cell in SQL

We have unique records created via a webpage and this has a browse for file to upload a image specific to this record. When the file is saved the URL location of the file is saved in SQL for that record to be recalled later in the system.
When you edit this record in the webpage and do not select a file, the SQL cell is made null.
here is the save code:
ByVal ID As HttpPostedFileBase
Dim folderPath As String = ""
If ID IsNot Nothing Then
folderPath = Server.MapPath("~/Content/ID/")
If Not Directory.Exists(folderPath) Then
Directory.CreateDirectory(folderPath)
End If
ID.SaveAs(folderPath & Record.UniqueID & ID.FileName)
Record.ID= "/Content/ID/" & Record.UniqueID & ID.FileName
End If
When the Browse for file is 'No File Chosen' it will overwrite the Record.ID making it null. do i need to add a text field to pull SQL URL through to stop the overwriting by checking if that is null?

Re-map linked tables in Access 2010

I am a relative novice with Access and starting from scratch with coding, so be gentle.
I have an Access 2010 database with dozens of linked tables based on .txt files. Sometimes the database moves, the source files move or the file server just gets re-named. In these events I am looking for a simple way for a user of the database to remap and refresh the linked tables. Ideally, it would be user prompted, i.e. the user pushes a button to refresh from a navigation form or something. Then, the system prompts for the new folder location. The folder location would house all of the necessary files, so it only needs to be selected one time. Once selected, all linked tables should remap and refresh with the user getting an error or success message.
I have seen a lot of these questions asked, but they seem to be in older versions of Access or it is not asking for a user prompt or for a user to browse for the new path.
Thanks.
While I agree with Marc B that this seems like a very oddly constructed database you could use the following code the manually link the tables again to the proper location. You would need to work this into a system that can loop through all tables and do them all one by one or adjust this code to do that automatically. But you may want to just rethink your system.
Function SetTableLinkPath(strTableName As String, strTablePath As String)
If Nz(strTableName, "") <> "" And Nz(strTablePath, "") <> "" Then
Dim cdb As DAO.Database
Set cdb = CurrentDb
cdb.TableDefs(strTableName).Connect = ";DATABASE=" & strTablePath
cdb.TableDefs(strTableName).RefreshLink
MsgBox "Table link for " & strTableName & " has been successfully set to the path: " & strTablePath & "."
Else
MsgBox "You must enter a valid Table path and name!"
End If
End Function

user defined Parameter used as part of a filename in Access

I have an access 2007 Database that outputs a report in excel format, the report is dependent on a date parameter that is chosen by the user. This parameter is selected via a textbox (text100) that has a pop up calendar. I would like to use the date in the text box(text100) as part of the filename. I am using the transferspreadsheet method to create the export, However I do not need the column headers. Once the file is created I have the code open the file and delete the headers. Also the current code is using todays date in the filename which is not accurate. The filename needs to reflect the date that was selected by the user in the text box from the pop up calendar
Ok here is the code.
Sub Branch298nohdr()
Dim Filename As String
Dim Path As String
Dim Branch As Integer
Dim Text100 As Date
Dim xl
Branch = "298"
Path = "Path" & Branch & "\"
Filename = "Identity Report " & Branch & " " & _
Replace(Text100, ":", " ") & ".xls"
If Dir(Path & Filename) <> "" Then
MsgBox "File has been created already"
If Dir(Path & Filename) <> "" Then
GoTo 53
End If
Else
Set xl = CreateObject("excel.application")
TempVars.Add "branchnum", Branch
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel8, _
"queryname", Path & Filename, False
xl.workbooks.Open Path & Filename
With xl
.Rows("1:1").entirerow.Delete
.Columns("L:L").select
.Selection.NumberFormat = "0"
.range("a1").select
xl.workbooks(1).Close Savechanges:=True
xl.Quit
Set xl = Nothing
53
MsgBox "Done!"
End With
TempVars.Remove "branchnum"
End If
Branch298nohdr_Exit:
Exit Sub
End Sub
Text 100 is where the user selects a date via a pop up calendar. I would like to use this date as part of the file name. Currently using the text100 as part of the filename it is being referenced as 12:00 am, and then adds this to the file name. I hope this clears up my intention.
Text 100 gets set on the opening form then there are several buttons which allow the user to pick between several branches or all branches.
Well the obvious question is, where does Text100 get first set?
Another style comment, it is better to do
goto ExitSub
'...
ExitSub:
Then your "GoTo 53" - its a little more meaningful.
At that rate, it would be better to move your "Done" message outside of the IF statements, and the TempVars doesn't seem to have a purpose; remove it.
Edit:
I presume if Text100 is a textbox on the form, then the line that reads:
Dim Text100 As Date
is going to override that reference in your code.
If you are referencing that textbox in your code, you need to do it this way:
foo = me.Text100
' or
foo = Forms!FormName.Text100
It's a little tough to determine exactly what your question is, but I think you are asking, "how do I use the contents of a text box as an export file name?" It sounds like somewhere in your code it creates a string for the filename that has & now() tagged on to the end to use the current date in the filename. Can you simply replace the '& now()' with '& textbox.value'?
A JD Long said it is hard to see any question in your posting. Maybe you should edit it again.
But as a general remark you need to escape any special characters that the user entered before you are going to use the input in a file name.
The following reserved characters are not allowed:
< > : " / \ | ? *
For more details on naming files in Windows see: Naming a File or Directory in MSDN.