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?
Related
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\....
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.
I'm trying to output multiple forms to a pdf file with the following code in Access:
Public Function Print_Form()
Dim myPath, reportName As String
For Each r In gvParent_Vals
glParent_id = r
If giMsgBox = 1 Then
//this method opens the form and makes it the active object
Select_Form
//set file path and pdf file to send form to
myPath = "C:\Users\C062342\Desktop\"
reportName = "test.pdf"
//output page 1 of the form
DoCmd.OutputTo acOutputForm, "Frm_Main_Report", acFormatPDF, myPath & reportName, False
// set global variable to page 2 source form and specify target to replace (subform of page 1 form)
gsActiveForm = "Frm_Main_Report_Pg2"
Set goCurrForm = Forms![Frm_Main_Report].Form.[Frm_Main_Report_Pg1]
//method to set the page 2 form by setting the target equal to the new source object
Activate_Form
//send page 2 to the same pdf file
DoCmd.OutputTo acOutputForm, "Frm_Main_Report", acFormatPDF, myPath & reportName, False
//method to Close the form
Close_Form
End If
Next r
End Function
When I do this in a loop I only get the very last form that opens, as if when Access is sending each form to the PDF file it is overwriting the last one each time. The other problem is that I need to output the form in landscape and I'm not sure how get this to happen in the PDF file. Are there any settings that go with this method or code that would precede or follow it that would allow me to accomplish this?
What you are trying is not possible. Access cannot append to an existing PDF.
DoCmd.OutputTo with an existing PDF file will overwrite that file.
The best course of action is to create a report that contains all data you want to export.
Or: use an external PDF printer driver that has multi-document capabilities, i.e. you create several print jobs from Access, and the driver combines them into one PDF file.
Example: http://freepdfxp.de/index_en.html
But this will a manual operation, unless you find a driver that can be fully automated.
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;
This function, when clicked on should ideally copy all of worksheet 2 to another workbook and save that sheet as a csv file. its not working... and i don't know how to tell it where to save the file (i want it to save to desktop). help please?
Is there any easier way i can do this?
Private Sub CommandButton2_Click()
'save as csv'
fname = "cambs_uploader.csv"
Sheet2.SaveAs fname, xlCSV
End Sub
If it's as simple as saving to your desktop every time then you can just put the full path in the string, rather than just the file name. However, if you want to save it by selection from a directory tree, then the easiest way in Excel is to use its Application.GetSaveAsFilename() method.
Documentation: http://msdn.microsoft.com/en-us/library/office/ff195734.aspx
Example:
Sub CommandButton2_Click()
Dim fName As String
Dim saveSheet As Worksheet
Set saveSheet = ActiveWorkbook.Sheets("Sheet2") ' Change your sheet name
saveSheet.SaveAs Application.GetSaveAsFilename("cambs_uploader.csv", ".csv", 1, "Save File")
Set saveSheet = Nothing
Exit Sub