Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a PDF file from an Access form

Status
Not open for further replies.

InspectorHound

Programmer
Nov 21, 2014
48
US



I have an access form with and ID field and I would like to open a PDF after I click on this field in the form ( I would use a hyperlink). The value of the ID field is the same as the name of the PDF and I have the access database in the same folder as the PDF's. what is the syntax for calling the PDF and can I call more than one PDF at a time -- I mean the pdf name has a few more letters than the ID field so that more than one PDF is associated with that ID.









 
If you use a hyperlink field there is no code to worry about. You create the hyperlink and then you can click it to open the PDF. You could have child table to allow you to associate multiple hyperlinks per each ID. Then your form would look something like

Parent Information......
Sub Form Linked Documents
Doc1.PDF
Doc2.PDF
Doc3.PDF


You can then click and launch any document.
 
I am not sure I understand. The name of the PDF file is the same as the value in the textbox -- how do I pass this value so that the correct PDF can be found in the file folder?
 
Most people would make a hyperlink to the document in the folder. So as they add a new document to the folder, they would also create a record with a hyperlink field. Is there a reason not to create a hyperlink and instead go searching a folder for pdfs matching a value? You can do that, but you said you wanted to use a hyperlink field.
I would use a hyperlink

You could do what you say. Say the ID is ABC. Then go find every document with ABC... and open it. That seems a little strange.

This examples opens a PDF in the database folder that has the same name as the value passed into the routine
Code:
Public Function GetDatabaseFullName() As String
  GetDataBaseFolder = CurrentProject.FullName
End Function
Public Function GetDataBaseName() As String
  GetDataBaseName = CurrentProject.Name
End Function
Public Function GetDataBaseFolder() As String
  GetDataBaseFolder = CurrentProject.Path
End Function

Public Sub ShowPDF(ID)
   Dim FileName As String
   Dim FileNameAndPath As String
   Dim objFileSystem As Scripting.FileSystemObject
   'Assume the file name is the ID.PDF
   
   Set objFileSystem = CreateObject("Scripting.FileSystemObject")
   FileName = ID & ".PDF"
   FileNameAndPath = GetDataBaseFolder & "\" & FileName
   If objFileSystem.FileExists(FileNameAndPath) Then
     Application.FollowHyperlink (FileNameAndPath)
   Else
     MsgBox "No PDF named: " & FileName & " in folder " & GetDataBaseFolder
   End If
   Exit Sub
   MsgBox Err.Number & "  " & Err.Description & Chr(13) & "In PictureFromFile Class"
End Sub

so you would call it like
showPDF(me.IDFieldName)

Now searching for all the pdfs of the form ABC*, makes it a lot more difficult. You then would have to loop all the documents in the folder looking for things of the form ABC*, open the document then move to the next document. It can be done, but that is a dumb design IMO. You should have a child record in the DB for each document, instead of randomly searching a folder.
 
The name of the PDF file is the same as the value in the textbox"

I had this situation once and I created a new field where I prepended my path to the front of the "value in the textbox" and I appended ".pdf" after the "value in the textbox." I made that field a hyperlink and clicking it opened the file.

This only helps you open one at a time and does not address "the pdf name has a few more letters than the ID field so that more than one PDF is associated with that ID"

I joined this forum in 2005. I am still a hack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top