G
Guest
Hi all,
I'm trying to work out a kink I'm having with one of the pieces of my
database.
Users of my db have the ability to print Word documents from a form.
Sequentially, they go to the "Letters" form, select which letter to print
from a combo box, Access pulls the pertinent information depending on which
letter type was selected, users can then edit whatever needs to be edited,
they click a Print command button, Word is called and prints out the document
and closes back down, and then an append query is run to append the
type/date/by whom the letter was printed to a letter history table.
Well, all this works fantastically, exept for when they want to print
multiple copies of a document. Obviously, they can do this by clicking the
Print button multiple times, but this causes the append query to append the
same info over and over once for each time the letter was printed.
I'm sure I can make the append query stop appending multiples, if I had
to... but that doesn't solve the bigger issue. I don't want users to have to
click that button 2, 3, whatever number of times. Below is a snippet of the
code I'm using to do all this. I'm only showing the process for two of the
letters used to give an idea:
Option Compare Database
Option Explicit
Private Sub Form_Load()
With Me
..lblField1.Visible = False
..lblField2.Visible = False
..lblField3.Visible = False
..lblField4.Visible = False
..lblField5.Visible = False
..lblField6.Visible = False
..txtField1.Visible = False
..txtField2.Visible = False
..txtField3.Visible = False
..txtField4.Visible = False
..txtField5.Visible = False
..txtField6.Visible = False
End With
End Sub
Private Sub cboLetterType_change()
If Me.cboLetterType.Value = "Dual Enrollment" Then
..lblField1.Visible = True
..lblField1.Caption = "Healthy Options Plan"
..txtField1.Visible = True
..txtField1.ControlSource = "[DualEnrollHOPlan]"
..lblField2.Visible = True
..lblField2.Caption = "End Date"
..txtField2.Visible = True
..txtField2.ControlSource = "[DualEnrollEndDate]"
..lblField3.Visible = True
..lblField3.Caption = "Insurance Company"
..txtField3.Visible = True
..txtField3.ControlSource = "[DualEnrollInsCo]"
..lblField4.Visible = True
..lblField4.Caption = "Subscriber"
..txtField4.Visible = True
..txtField4.ControlSource = "[DualEnrollSubscriber]"
..lblField5.Visible = True
..lblField5.Caption = "Insurance Phone Number"
..txtField5.Visible = True
..txtField5.ControlSource = "[DualEnrollInsPhNumber]"
..lblField6.Visible = False
..txtField6.Visible = False
End With
ElseIf Me.cboLetterType.Value = "Welcome" Then
With Me
..lblField1.Visible = True
..lblField1.Caption = "Client(s)"
..txtField1.Visible = True
..txtField1.ControlSource = "[WelcomeClients]"
..lblField2.Visible = True
..lblField2.Caption = "Amount"
..txtField2.Visible = True
..txtField2.ControlSource = "[WelcomeAmount]"
..lblField3.Visible = True
..lblField3.Caption = "Frequency"
..txtField3.Visible = True
..txtField3.ControlSource = "[WelcomeFrequency]"
..lblField4.Visible = False
..txtField4.Visible = False
..lblField5.Visible = False
..txtField5.Visible = False
..lblField6.Visible = False
..txtField6.Visible = False
End With
End If
End Sub
Private Sub cmdMerge_Click()
On Error GoTo cmdMerge_Err
'Start Microsoft Word
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
If Me.cboLetterType.Value = "Dual Enrollment" Then
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("\\dshsfloly7002\Kdrive\DATA\ESI\Letters\DualEnrollment.doc")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("FirstName1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("LastName").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_LNAME]))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS]))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS2]))
..ActiveDocument.Bookmarks("HOH").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_ID_NUM]))
..ActiveDocument.Bookmarks("HOPlan").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollHOPlan]))
..ActiveDocument.Bookmarks("EndDate").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollEndDate]))
..ActiveDocument.Bookmarks("InsCo1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsCo]))
..ActiveDocument.Bookmarks("InsCo2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsCo]))
..ActiveDocument.Bookmarks("Subscriber").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollSubscriber]))
..ActiveDocument.Bookmarks("InsCoPhone").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsPhNumber]))
End With
ElseIf Me.cboLetterType.Value = "Welcome" Then
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("\\dshsfloly7002\Kdrive\DATA\ESI\Letters\Welcome.doc")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("FirstName1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("FirstName2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("LastName").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_LNAME]))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS]))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS2]))
..ActiveDocument.Bookmarks("HOH").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_ID_NUM]))
..ActiveDocument.Bookmarks("WelcomeClients").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeClients]))
..ActiveDocument.Bookmarks("WelcomeAmount").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeAmount]))
..ActiveDocument.Bookmarks("WelcomeFrequency").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeFrequency]))
End With
End If
'Append information to tblLetterHistory.
DoCmd.OpenQuery "qryAppendLetterHistory", acViewNormal, acEdit
'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Quit Microsoft Word and release the object variable.
objWord.Quit
Set objWord = CreateObject("Word.Application")
Exit Sub
cmdMerge_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
Exit Sub
End Sub
If there is any way for my to allow users to enter in the amount of copies
they wish to print, or even code in the amount of times to print a certain
type of letter, I would really appreciate some help on this! If you need any
more information as to what I am trying to achieve, just ask!
Robert
I'm trying to work out a kink I'm having with one of the pieces of my
database.
Users of my db have the ability to print Word documents from a form.
Sequentially, they go to the "Letters" form, select which letter to print
from a combo box, Access pulls the pertinent information depending on which
letter type was selected, users can then edit whatever needs to be edited,
they click a Print command button, Word is called and prints out the document
and closes back down, and then an append query is run to append the
type/date/by whom the letter was printed to a letter history table.
Well, all this works fantastically, exept for when they want to print
multiple copies of a document. Obviously, they can do this by clicking the
Print button multiple times, but this causes the append query to append the
same info over and over once for each time the letter was printed.
I'm sure I can make the append query stop appending multiples, if I had
to... but that doesn't solve the bigger issue. I don't want users to have to
click that button 2, 3, whatever number of times. Below is a snippet of the
code I'm using to do all this. I'm only showing the process for two of the
letters used to give an idea:
Option Compare Database
Option Explicit
Private Sub Form_Load()
With Me
..lblField1.Visible = False
..lblField2.Visible = False
..lblField3.Visible = False
..lblField4.Visible = False
..lblField5.Visible = False
..lblField6.Visible = False
..txtField1.Visible = False
..txtField2.Visible = False
..txtField3.Visible = False
..txtField4.Visible = False
..txtField5.Visible = False
..txtField6.Visible = False
End With
End Sub
Private Sub cboLetterType_change()
If Me.cboLetterType.Value = "Dual Enrollment" Then
..lblField1.Visible = True
..lblField1.Caption = "Healthy Options Plan"
..txtField1.Visible = True
..txtField1.ControlSource = "[DualEnrollHOPlan]"
..lblField2.Visible = True
..lblField2.Caption = "End Date"
..txtField2.Visible = True
..txtField2.ControlSource = "[DualEnrollEndDate]"
..lblField3.Visible = True
..lblField3.Caption = "Insurance Company"
..txtField3.Visible = True
..txtField3.ControlSource = "[DualEnrollInsCo]"
..lblField4.Visible = True
..lblField4.Caption = "Subscriber"
..txtField4.Visible = True
..txtField4.ControlSource = "[DualEnrollSubscriber]"
..lblField5.Visible = True
..lblField5.Caption = "Insurance Phone Number"
..txtField5.Visible = True
..txtField5.ControlSource = "[DualEnrollInsPhNumber]"
..lblField6.Visible = False
..txtField6.Visible = False
End With
ElseIf Me.cboLetterType.Value = "Welcome" Then
With Me
..lblField1.Visible = True
..lblField1.Caption = "Client(s)"
..txtField1.Visible = True
..txtField1.ControlSource = "[WelcomeClients]"
..lblField2.Visible = True
..lblField2.Caption = "Amount"
..txtField2.Visible = True
..txtField2.ControlSource = "[WelcomeAmount]"
..lblField3.Visible = True
..lblField3.Caption = "Frequency"
..txtField3.Visible = True
..txtField3.ControlSource = "[WelcomeFrequency]"
..lblField4.Visible = False
..txtField4.Visible = False
..lblField5.Visible = False
..txtField5.Visible = False
..lblField6.Visible = False
..txtField6.Visible = False
End With
End If
End Sub
Private Sub cmdMerge_Click()
On Error GoTo cmdMerge_Err
'Start Microsoft Word
Dim objWord As Word.Application
Set objWord = CreateObject("Word.Application")
If Me.cboLetterType.Value = "Dual Enrollment" Then
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("\\dshsfloly7002\Kdrive\DATA\ESI\Letters\DualEnrollment.doc")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("FirstName1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("LastName").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_LNAME]))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS]))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS2]))
..ActiveDocument.Bookmarks("HOH").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_ID_NUM]))
..ActiveDocument.Bookmarks("HOPlan").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollHOPlan]))
..ActiveDocument.Bookmarks("EndDate").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollEndDate]))
..ActiveDocument.Bookmarks("InsCo1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsCo]))
..ActiveDocument.Bookmarks("InsCo2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsCo]))
..ActiveDocument.Bookmarks("Subscriber").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollSubscriber]))
..ActiveDocument.Bookmarks("InsCoPhone").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![DualEnrollInsPhNumber]))
End With
ElseIf Me.cboLetterType.Value = "Welcome" Then
With objWord
'Make the application visible.
..Visible = True
'Open the document.
..Documents.Open ("\\dshsfloly7002\Kdrive\DATA\ESI\Letters\Welcome.doc")
'Move to each bookmark and insert text from the form.
..ActiveDocument.Bookmarks("FirstName1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("FirstName2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_FNAME]))
..ActiveDocument.Bookmarks("LastName").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_LNAME]))
..ActiveDocument.Bookmarks("Address1").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS]))
..ActiveDocument.Bookmarks("Address2").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtMAIL_ADDRESS2]))
..ActiveDocument.Bookmarks("HOH").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![txtHOH_ID_NUM]))
..ActiveDocument.Bookmarks("WelcomeClients").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeClients]))
..ActiveDocument.Bookmarks("WelcomeAmount").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeAmount]))
..ActiveDocument.Bookmarks("WelcomeFrequency").Select
..Selection.Text = (CStr(Forms![frmLetterInfo]![WelcomeFrequency]))
End With
End If
'Append information to tblLetterHistory.
DoCmd.OpenQuery "qryAppendLetterHistory", acViewNormal, acEdit
'Print the document in the foreground so Microsoft Word will not close
'until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
'Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
'Quit Microsoft Word and release the object variable.
objWord.Quit
Set objWord = CreateObject("Word.Application")
Exit Sub
cmdMerge_Err:
'If a field on the form is empty, remove the bookmark text, and
'continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
End If
Exit Sub
End Sub
If there is any way for my to allow users to enter in the amount of copies
they wish to print, or even code in the amount of times to print a certain
type of letter, I would really appreciate some help on this! If you need any
more information as to what I am trying to achieve, just ask!
Robert