Re: Embed PDF file in bound control - Final
Okay, after a week of frustration I have worked out all of the possible reasons for my code not working and have a documented process for moving forward.
To recap:
The scenario is that I want to embed a document (could be RTF, PDF, DOC, etc) directly in my database.
I understand the wisdom of storing a pointer to a location of the file outside of the DB and, having considered all of the pros and cons, have still opted to keep the document in the DB as opposed to outside of the DB.
The actual form that is used is fairly complex in that it is a compound form. The "host" form is the container for 2 other forms, unimaginatively called A & B for this discussion. I will refer to the host form as H.
So, H contains A & B.
Form H is bound to my FACT TABLE, maintaining the absolute minimum information required to track how the data is to be read. This form will have the current user, security status, session information and current record being viewed.
Form A will then be linked to Form H as a child form. Form A is bound to the database linked to the primary key of the current record currently being viewed from Form H.
Form A also contains a ]TAB CONTROL that has multiple forms within it. One of these subforms contained within the TAB CONTROL is bound to a related table that has a bound OLE control.
Form B is a administrative form presenting the user with various status details as well as options to select from. The information in Form B is based on a query of data in a subform contained in form A.
There is a button on Form B that launches a process that does the following:
- Creates a compound PDF made up of several documents generated from selections in Form A
- Stores this compound document in the database, viewable through the subform embedded within the TAB CONTROL on Form A
- Formulats an Email with the compound PDF as an attachment, taking the [TO], [SUBJECT] & [BODY] information from subforms contained with the TAB CONTROL
- Sends the Email, deleting the item from the SENT ITEMS of the OUTLOOK client
- Returns control to the user with an "Operation Successful" msgbox
The wall I walked into came about
because of the levels that the forms are all nested.
The code as originally mentioned actually works on its own, but as soon as you start referencing the forms too deeply the code starts showing wobblies.
The range of problems experienced went from errors mentioned in the start of the post through to some really frustrating ones.
The solution that I have opted for eventually has been to create a form that has only the fields required for the operation. I provide values for all of the fields via VBA.
On a click of the button in FORM B I launch the form in a hidden form, set the values, commit the data to the DB, do a REQUERY of the embedded subforms within Form A, close the hidden form and everything works!
The code for the entire operation is as follows:
'============ Code Starts ==============================
'Note: Sub requires a parameters that contains the full name & path
' of the file to be embedded.
'====================================================
Public Sub EmbedPDF(varFile)
On Error Resume Next
Dim frmName As String
frmName = "frm_AddDocument"
DoCmd.OpenForm frmName, acNormal, , , acFormAdd, acHidden
With Forms![frm_AddDocument]
!DocTypeID = 13
!DocSubject = "PDF Document Image"
With !Document
.Enabled = True
.Locked = False
.OLETypeAllowed = acOLEEmbedded
.Class = "AcroExch.Document"
.SourceDoc = (varFile)
.Action = acOLECreateEmbed
End With
DoCmd.RunCommand acCmdSaveRecord
End With
DoCmd.Close acForm, frmName
Forms![FORMH]![frmFORMA].Form![sfrm_Documents].Form.SetFocus
Forms![FORMH]![frmFORMA].Form![sfrm_Documents].Form.Requery
End Sub
'=========== Cod Ends ===============================
That took much longer than I anticipated
Now I can go to bed, knowing that I will not be breaking out in a cold sweat in the middle of the night because this darn OLE/PDF thing has gotten me again
Hope this helps some other sucker to sleep well one day.
Sean
There's a tokoloshi in the net - & he's loving it 