 |
 |
 |
 |
 |
Please Fill Out This Form, Part 3 |
|
 |
|
 |
|
This article is the third in a series of articles on
automating forms. If you've been following along, you now know how to build a basic form
template with Form Fields and use VBA (Visual Basic
for Applications) code to help move users through
a form efficiently. If not, go back and check the
November/December 2000 and March/April 2001 issues of
Computor Companion for these first two lessons so you can catch up. (You can find
them at www.computorcompanion.com.)
In this article, I show you how to create a
form that uses message boxes to inform users and
input boxes to request specific information. Then
with that information, the form fills out the
information for the user, without making the user tab
through fields.
 |
1 Use the skills you learned in the previous
two articles to build a simple form like
the one below. Be sure your form has form fields
for Text and Yes/No checkboxes. Make it simple, since you'll be concentrating more on the
VBA code now, rather than worrying about the form details. Be sure to double-click each field
to change the default bookmark names to
realistic names that are easier to remember.
|
|
|
2 Now click Tools|Macro|Macros to enter
the Visual Basic Editor (VBE). Name the macro AutoNew and make
sure you've clicked the Macro in drop-down box and selected your
template. Then click Create and the VBE opens. |
|
|
3 Because this macro is called AutoNew, it automatically runs when the user creates a new document. Type the code at right into your macro window exactly as I've written it. Note
that the MsgBox code wraps, but in your code, do
not hit the Enter key until the closing parentheses.
The macro says: set a variable called vAnswer
to hold the user's yes or no answer. The
If…Then statement asks what the value of the vAnswer
variable is and then does something for each value.
If the user clicks Yes the MsgBox function returns
6 in vAnswer, and the macro runs another macro called mUserName. Otherwise (the user
didn't choose Yes), the macro Exits and stops all
further automation. Note that in Visual Basic, 6 is
the MsgBox Yes value and 7 is the No value. |
Sub AutoNew()
add any optional macro comments here, if you want
vAnswer = MsgBox(Prompt:="This form will now run automatically to acquire information and fill
in the form. Do you want to continue?", Buttons:=vbYesNo)
If vAnswer = 6 Then
mUserName
Else
Exit Sub
End If
End Sub
|
|
4 If you ran this macro now, you'd get an
error because you haven't written the mUserName macro yet. So after the
End Sub line in your current macro, press Enter. Now type
Sub mUserName() and press enter. Wow! A new
macro starts for you. Now add a comment. Press
Enter and type `an input box to get user name to put
into form. (You include the single quote to
designate comments.) Now type the code at right:
This macro says: create a variable called vUserName to hold the results of whatever
the user types into the InputBox function. Then
make the FormField named bkUserName equal to the results of that variable.
|
vUserName = InputBox(Prompt:="Pls enter your
name and click ok.")
ActiveDocument.FormFields("bkUserName").Result
= vUserName
|
|
5 If you did everything right, when you click File|New to create a new document from this template, the first message should pop up.
If you say yes, you get the InputBox. Whatever
you type into the InputBox is then put into the
Name field. NOTE: Be sure your bookmark names
match the code and remember to click Tools|Protect
Document to lock your template for Forms! Now give it
a shot. Close the VBE. Lock and save your
template. Click File|New and choose your
template…and cross your fingers!
Hopefully, you didn't have any typos in your code
so you didn't receive any errors. If you did, just click
the Debug option to see which line has the problem.
Fix the typo then stop the debugger by clicking
the square Stop button above the code window and
give it another shot.
|

|
|
6 Now try writing code on your own! Try setting the Checkbox result. Be sure you have checkbox fields on your form for both Yes and
No. Label the bookmarks ckYes and ckNo. Then create
a new macro in your code windows by hitting Enter
after the last macro, type Sub mYesNo(), and press
Enter. In the code, set a vYesNoAnswer variable
equal to the equivalent of the answer to the message
box MsgBox(Prompt:="Does user need private
modem jack?", Buttons:=vbYesNo). Then write an
If…Then statement that deciphers your answers and sets
the appropriate checkbox value. It should read:
If vYesNoAnswer = 6 Then
ActiveDocument.FormField("ckYes").Checkbox.Value _
= True
Else
ActiveDocument.FormField("ckNo").Checkbox.Value _
= True
End If
Use the syntax (exact layout and statement
format) from your previous code to see if you can pull the
rest of this off yourself with the above information.
Important Note: Remember you'll also have to add
the mYesNo macro call to the end of your
previous mUserName macro (before the End Sub) so
that macro will call the next macro to run. Otherwise,
how will it know to run?
One of the best ways to learn how to write code
is to be given a project and then wrestle it out on
your own. Remember to hit the F1 key for VBA help!
If you can't make it work, feel free to e-mail me
at dian@mousetrax.com and I'll help you solve it.
Good luck!
|
|
For more information on forms, be sure to check out all of Dian's articles
at this link http://www.mousetrax.com/techpage.html#autoforms.
|
 |
Like the articles in Computor Companion? Check out our computer tips books!
 |
Logical Tips for Mastering Your Computer:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Your Computer More Effectively
Go from Computing Newbie to Power User!
|
Read about this book on Amazon |
 |
Logical Tips for Mastering the Internet:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use the Internet More Effectively
Go from Internet Newbie to Expert!
|
Read about this book on Amazon |
 |
Logical Tips for Mastering Microsoft Office:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Microsoft Office More Effectively
Don't Let Microsoft Office Drive You Crazy!
|
Read about this book on Amazon |
 |
Logical Tips for Mastering Microsoft Windows:
Quick Shortcuts, Tips, Tricks, and Techniques to Help You Use Microsoft Windows More Effectively
Combat Windows Weirdness!
|
Read about this book on Amazon |
|
 |
|
|
 |
 |
Did you like this article? Let us know! Please click here to send feedback on this article. NOTE: If you have computer-related questions, you must post them on our forum. We do not answer computer questions via email. |
 |
|
 |
 |
|
 |