PDA

View Full Version : Using a UserForm more than once



macshimi
11-09-2006, 05:07 AM
I have a UserForm which is used to print two copies of a document.

The user needs to repeat this for a user-specified number(TxtCnt) of documents and I do not know how to return control to the UserForm so that the next set of data can be entered.

The user enters the number of docs to complete in a TexBox TxtCnt.

The For.. Next Statement works but control does not return to the UserForm after printing. How do i do that?

This is a simplified version of the code (I have removed a lot of repetitive
lines):

Private Sub cmdPrint_Click()
Dim strReason As String
Dim strType As String
Dim i As Integer
For i = 1 To TxtCnt.Value

If optReason1 = True Then strReason = "Actual Exit"
If optType1 = True Then strType = "Leaving Service"

Application.ScreenUpdating = False

UpdateBookmark "bmkID", txtID.Value
UpdateBookmark "bmkSur", txtSur.Value

Application.ScreenUpdating = True
ActiveDocument.PrintOut Copies:=2

optReason1.Value = True
optType1.Value = True
txtID.Value = Null
txtSur.Value = Null
Application.ScreenUpdating = True
Next i

End Sub
Thanks for any help you can offer.

mdmackillop
11-09-2006, 11:19 AM
Hi Macshimi,
Welcome to VBAX.
BTW if you select your code and click the VBA button, it formats as shown.
Regards
MD

Try

Private Sub cmdPrint_Click()
Dim strReason As String
Dim strType As String
Static i As Integer
i = i + 1
If optReason1 = True Then strReason = "Actual Exit"
If optType1 = True Then strType = "Leaving Service"
Application.ScreenUpdating = False
UpdateBookmark "bmkID", txtID.Value
UpdateBookmark "bmkSur", txtSur.Value
Application.ScreenUpdating = True
ActiveDocument.PrintOut Copies:=2
optReason1.Value = True
optType1.Value = True
txtID.Value = Null
txtSur.Value = Null
Application.ScreenUpdating = True

If i = txtcnt.Value Then Unload UserForm1
End Sub

TonyJollans
11-09-2006, 12:14 PM
I don't understand either what you are asking, or what your process is doing.

You have a UserForm with a button, cmdPrint. When this is pressed you have a loop which plays around a bit and prints the active document. It doesn't close the document so unless the UpdateBookmark routine closes the document, or otherwise changes the active document, you're going to reprint the same document each time round the loop. You also clear txtID and txtSur (somewhat unconventionally with Null) so they only have values first time round as well.

Whatever, when the routine has finished, as you do nothing to change the default behaviour, the Userform should retain the focus unless you have displayed it as non-modal.

You do say the code is simplified so I'm probably being a litle unfair but I still don't understand what you are asking. Malcolm's solution simply dismisses the form after the user has pressed the button a certain number of times (provided, of course, that the user doesn't change the value between presses).

Am I just being really thick?

macshimi
11-10-2006, 04:55 AM
Hi Tony

I doubt if you are being thick, as I know I don't have a full understanding of Visual Basic.

We have this document notifiying the administrators that a member has left the pension scheme, it is never saved as it printed, signed and posted.

At the end of the month we can have a number of leavers and I wanted the user to say how many (txtCnt) and enter the first leaver.

The user clicks the Print button, two copies are printed.

I now need the UserForm to clear, display again so the next leaver details can be entered and printed.

That is what I wanted to happen but after the first input the Userform doesn't re-appear.

Thanks for your interest.

macshimi
11-10-2006, 04:57 AM
Hi mdmackillop

Thanks for the advice and your help, i will try it and let you know.

fumei
11-10-2006, 04:23 PM
I must be thick as well. Unless you unload the userform (and DO you have it non-modal?), executing the print commandbutton will not close the userform.

Seems to me there is stuff missing. For example, you have string variables (strreason, strType) that are set in the Print procedure...but are never used in the procedure.