Exporting Multiple MS Access Forms to A Single PDF File - ms-access

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.

Related

Vb.Net (Visual basic), Web Browser Control; make an external file available to pages

I've been developing an "IDE" for custom software that relies on a web browser control to display it's information. The software uses an external file "common.vbs" to access common functions within it's web pages. Sprinkled throughout various web pages within the application is the html tag:
<script LANGUAGE="VBScript" src="common.vbs"></script>
In my application I've tried the following code after importing the code into a resource string:
Dim sPath As String = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)
sPath = Path.Combine(sPath, "common.vbs")
Try
Dim fs As FileStream = File.Create(sPath)
Dim sData As String = My.Resources.common
Dim Info As Byte() = New UTF8Encoding(True).GetBytes(My.Resources.common)
fs.Write(Info, 0, Info.Length)
fs.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
The code doesn't error. When I test (withing the same function) reading the file back, it's contents are returned; I know the file is being written, but when the any of the pages load with the above HTML tags, I get an error from the page:
An error has occurred in the script on this page
Line: 0
Char: 0
Error: Script error
Code: 0
URL: about:common.vbs
The file I'm trying to "include" contains only functions like:
Function DoHelp
window.external.DoHelp "", "", -1, -1
End Function
Function SQLDate( d)
SQLDate = "'" & DatePart("yyyy", d) & "-" & DatePart("m", d) & "-" & DatePart("d", d) & "'"
End Function
Function MonitorJob( jobId)
on error resume next
window.external.UIControl.MonitorJob jobId, 0
End Function
Within the application, there are no problems loading and using any of the functions contained within the file. The actual contents of the file is from the application and hasn't been edited in any manor. Any page that I load with my application that has the file included errors out, including simple test pages.
Does anyone have any ideas on how to get the web browsers control recognize a file from within a script's scr tag that's not part of the actual page being loaded and supplied from the hosting application? Editing all of the pages within the source application really isn't an option; may are dynamically created and there is a large number of them.
I've tried to include as much information as I can here, but if you need more info, please feel free to ask!
Thanks, Fred
EDIT / UPDATE:
I've tried writing all of the files I need to "include" into a temp folder, write the calling HTML file to the same folder, then load the web control using a file stream. The results are the same.
EDIT / UPDATE
I must have things wrong; Writing all of the "resource Files" into the temp folder, then writing the resulting HTML page into the same file and finally calling the Navigate method to that temp folder and file results in the page properly loading images and the underlying included VBS file.
Last EDIT / UPDATE
It took me a while to get all of the parts working correctly. Below is the answer that solved the issue.
Part of the problem is how the web control is loaded. Using the document methods circumvents accessing the file system because no base location is used. In order to access items in the file system, the "source page" needs to be in the file system. The next part of the problem was getting the resources into the file system. Below is the code that I used. Important note: for demonstration reasons only, the code for the Temp Path is located inside this routine. It actually resides out side the routine for access in other routines.
Sub UpdateFiles(sData)
'Write all the dependent files into the working folder.
Dim sLocalPath As String = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Do While Directory.Exists(TempWorkingPath) Or File.Exists(TempWorkingPath)
sLocalPath = Path.Combine(Path.GetTempPath, Guid.NewGuid.ToString)
Loop
Directory.CreateDirectory(sLocalPath)
File.WriteAllBytes(Path.Combine(sLocalPath, "common.vbs"), System.Text.Encoding.UTF32.GetBytes(My.Resources.common))
Dim aImageList() As String = Split(My.Resources.M3ImageList, vbCrLf)
Dim sImageList As String = Replace(Join(Split(My.Resources.M3ImageList, vbCrLf), ","), "-", "_")
Dim ResourceSet As Resources.ResourceSet = My.Resources.ResourceManager.GetResourceSet(Globalization.CultureInfo.CurrentCulture, True, True)
For Each Dict As DictionaryEntry In ResourceSet.OfType(Of Object)()
If TypeOf (Dict.Value) Is Drawing.Image Then
If InStr(sImageList, Dict.Key, vbTextCompare) > 0 Then
Dim ImageConverter As New ImageConverter
Dim aBytes() As Byte = ImageConverter.ConvertTo(Dict.Value, GetType(Byte()))
Dim sName As String = Replace(Dict.Key.ToString & ".gif", "_", "-")
File.WriteAllBytes(Path.Combine(sLocalPath, sName), aBytes)
End If
End If
Next
File.WriteAllText(Path.Combine(sLocalPath, "index.html"), sData)
End Sub
I used a separate resource file that listed all of the files I needed to drive this routine. One point of note, when I imported all of the GIF's, the -'s were converted into _'s, thus the replace statements. I also used a filter (the Instr command) so that only the items in the list were exported.
I spent a lot of time on this, and I hope others will find it useful!
Fred

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?

Dynamically filling a dropdown box in a web page using an Excel sheet with VBA

I have an Excel sheet say A that contains a list from A1 to A75 that has to be dynamically replace the value in a webpage that contains a drop down list.
For each and every replacement value it needs to pull the report from the web page and save that to an Excel sheet B.
All of this function has to happen by clicking a form control button in Excel.
Please suggest & post the code if any.
I'm new to VBA and Excel; I’ve been instructed to get the values of currencies across the world with other currencies. This has to be done 365 days.
Although I’ve suggested to try with Macro in order to pull the data from webpage to excel, but after viewing the site i came to know that each and every currency has to be selected from the dropdown list and need to export those currencies to an excel file.
This becomes a hectic issue, so i thought of a solution and come up with an idea (I'm not sure whether this gonna work or not) and posted here to seek assistance for this issue.
//
Further assistance to the question, Excel file contains more than 75 currencies and those currencies need to be selected with the System date and need to select the Currency code starting from "AED" to "ZWD", for each entry need to export the file to excel. There is an option available for that in that webpage.
//
Hope this clarifies your requirement, for further assistance please let me know !
Best Regards,Pravin
it's hard if you don't have good VBA and HTML knowledge.
in general you can do this by using a http request object instead of the IE automation. This is faster and from my point of view also easier:
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "GET", "http://www.cboden.de"
oRequest.Send
MsgBox oRequest.ResponseText
If you are behind a proxy you can use something like this:
Const HTTPREQUEST_PROXYSETTING_PROXY = 2
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.setProxy HTTPREQUEST_PROXYSETTING_PROXY, "http://proxy.intern:8080"
oRequest.Open "GET", "http://www.cboden.de"
oRequest.Send
MsgBox oRequest.ResponseText
and if you want to use POST (instead of the GET method) to pass some values to the webserver, you can try this:
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "POST", "http://www.cboden.de/misc/posttest.php"
oRequest.SetRequestHeader "Content-Typ", "application/x-www-form-urlencoded"
oRequest.Send "var1=123&anothervar=test"
MsgBox oRequest.ResponseText
if you put it into a function then you can use it in you worksheet:
Function getCustomHyperlink(ByVal pURL As String) As String
Dim oRequest As Object
Set oRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
oRequest.Open "GET", pURL
oRequest.Send
getCustomHyperlink = oRequest.ResponseText
End Function
within the worksheet you can then say for example:
=getCustomHyperlink("https://www.google.com/search?q=" & A1 )
if your search value is in A1
If you have to use the GET or the POST method depends on the website you want to retrieve.
1 In Excel IDE, add reference to "C:\Windows\System32\ieframe.dll".
(Press F11 excel to go to IDE) (Tools | Reference | Browse | locate the above file )
2 Create a module with following code and run it. It will open the IE and go the yahoo site
Sub aa()
Dim aa As New SHDocVw.InternetExplorer
aa.Visible = True
aa.Navigate "http://www.yahoo.com.hk"
Stop
End Sub
3 Complete the above program by this idea:
Excel program is emulating a user navigation to the required web page.
The program waits the page is completely loaded. Then read the page content by aa.document.
Write the required data from aa.document into an excel worksheet.
Repeat.

Drag and Drop File into Microsoft 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;

Using Windows Explorer to get path(string) of a file to store in a table

I currently have a database set up to store the paths of pictures associated with my data and display them in imageframes with VBA. It works great, but the process of adding a picture is a bit tedious, and users struggle to use it correctly(type the wrong path, forget to include the extension, etc). This results in a bunch of garbage entries in the database. I would like to simplify this process. Ideally, when "add picture" is clicked, I would like for it to open up windows explorer, have the user select the desired picture, get the path of that picture, and insert it into the table. Again, I'm not using an OLE, just a text field for the path. Is this possible?
As Remou referenced, the FileDialog object can be used to accomplish this. For many people, it may be necessary to add references to the MS Office Object Library(The Access Library is not sufficient). The code that I used to collect a path name from a file selected in explorer is as follows:
Public Sub ShowFileDialog()
Dim dlgOpen As FileDialog
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = False
.InitialFileName = "Z:\" 'Initial Path when explorer is opened
.Show
If .SelectedItems.Count = 0 Then
MsgBox ("No file Selected") 'No file selected
Else
Me.txtPath = .SelectedItems(1) 'sets textbox on the form to the path selected
End If
End With
End Sub