Liz,
Let's start with how the userform is called. Say you've created a
userform definition and named it InputUF. (You should always rename
forms from the default UserForm1 to something informative.) Then in a
module, you'll write a macro -- maybe AutoNew or AutoOpen, or
something else, which I'll call demoMain -- to start the userform. VBA
will let you do that by writing the single line
InputUF.Show
but that's really not a good way to do it. Instead, consider the
userform definition to be a "template" or "cookie cutter" for making
new userforms. This code uses the New statement to make a copy of the
InputUF definition, runs the copy with the Show statement, and
destroys the copy by setting it to Nothing.
Sub demoMain()
Dim UFcopy As InputUF
Set UFcopy = New InputUF
UFcopy.Show
Set UFcopy = Nothing
End Sub
In the userform's code, when you call Me.Hide to remove the userform
from the screen, execution resumes in the demoMain macro at the line
after the .Show statement. Until you call Me.Hide, though, the
userform stays on the screen and has the input focus.
Now, here's how this works for validating input. Let's say the
userform is very simple, just a text box named textInput, an OK button
named cmdOK, and a Cancel button named cmdCancel. In the code of the
userform, besides whatever other procedures you have -- such as
Userform_Initialize -- you'll have click procedures for the two
buttons. This example shows only how to make sure there is something
in the text box.
Private Sub cmdCancel_Click()
Me.Hide
End Sub
Private Sub cmdOK_Click()
If Len(Trim(textInput.Text)) > 0 Then
' there is something other than blanks
Me.Hide
Else
' the box is empty or blanks
MsgBox "Please enter text"
textInput.SetFocus
End If
End Sub
The If statement tests whether the text in the box, not counting any
blank characters, is at least one character long. If it is, then the
Me.Hide runs and execution goes back to the demoMain macro. If the box
is empty or contains only blanks, the user is shown a message (what
Karl meant by "spit at the user") and the cursor is moved from the OK
button back to the text box. Then the cmdOK_Click procedure ends, and
the userform waits for more input. It can repeat the same
test-and-response over and over, until the user finally enters
something or clicks the Cancel button.
Of course, the If statement can be much more complex, such as using
the text as the key in a database query to verify that the record
exists, or checking that the text has the proper length and characters
to be a serial number.
When the demoMain macro gets control after the Me.Hide, the userform
is still in memory even though it's been removed from the screen. It
can refer to any of the items in the userform; for example, it can
look at what's in the textbox by inserting a line like this between
the .Show statement and the Nothing assignment:
userResponse = UFcopy.textInput.Text
Now the user's response is in a string variable in the macro and you
can do something with it, such as inserting it into the document.
After you do the Nothing assignment, the userform and all its contents
are wiped out. The userResponse string, though, will keep its assigned
value.
This validation is not the same idea as a modal form. Word's userforms
are modal by default, which just means that the user can't interact
with any other part of Word as long as the userform has the focus. You
can specify in the .Show statement that you want a nonmodal userform
(by including the optional parameter Modal:=False), which means that
the user can click into the document, move the insertion point, and
edit the document while the userform continues to be displayed (the
built-in Find dialog works like this.)
Karl's other point is that you should _not_ declare variables at the
top of a module or userform code, unless you specifically mean them to
be available to all procedures in that module or form. The usual
method is to declare a variable inside a procedure, so that it belongs
only to that procedure. The concern is that if you make a variable
available throughout the module, then it's too easy to change its
value when you don't mean to -- if you use the same name in two widely
separated places for different purposes, VBA thinks they're the same
variable, and that leads to bugs. Module-wide or "global" variables
should be kept to an absolute minimum.
--
Regards,
Jay Freedman
Microsoft Word MVP FAQ:
http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
So does this mean this: in a module (and I assume in the code related
to a form) if I put all decalariations at the top of the module are
they always evaluated first prior to any code being exceuted? And can
you explain what you mean by "exposure" please.
Unless I'm being stupid I don't understand. I am hiding & unloading
and as I understand it I should also set the form to nothing to ensure
that it is removed from memory, this is when eithe everything os OK or
Cancel or Close is invoked. But if the data entered is invalid I need
to understand how to "spit at the user", how does it work? If the form
is already displayed does it stay loaded and can I make it "modal" as
in Access so that comntrol stays with it untill it is either completed
or cancelled? So if the code has done the "check if valid" and found
the data to be invalid if I exit the procedure is control back with
the user? Sorry to be so dumb, but this is 3 days of learning very
fast!!!