![]() |
|
|||||||||
| |
Related Information...
|
|||||||||
|
Training > Tutorials > Word AutoForms > Code Snippets |
||||||||||
|
On this page...
|
Code Snippets I receive many fan letters from readers of my AutoForm articles. I'm happy that I've been able to provide useful information, which so many users appreciate. Along with many of these thank you letters, users occasionally ask for help when they have ideas about how to create their forms, but have trouble implementing their ideas into working code. Although I may not always be able to provide the ultimate answer, I do my best to help them out with solutions. As time permits, I'll post these custom solutions here. However, also know that MouseTrax.com sponsors two VBA support groups that you can join, free. Get the help you need, 24x7. See our Resources page for details.
Here's my code suggestion with commented explanations... Sub pAddAnswers() '
first set variables for all the field results
in
the form vOne
= Val(ActiveDocument.FormFields("Expect1").Result) '
now set a variable equal to the value of
each
of these vTotal = vOne + vTwo + vThree + _ '
Change the total cell to a bookmark Call pToggleProtectDoc ' once unlocked...you zip over to the total bookmark ActiveDocument.Bookmarks("myTotalBookmark").Select ' assuming the user may go through Selection.EndKey Unit:=wdLine, Extend:=wdExtend ' now you set the bookmark to ActiveDocument.Bookmarks("myTotalBookmark").
_ ' now you can create a new variable vAverage = vTotal / 8 ' now do the same as you did
with the total... ActiveDocument.Bookmarks("myAverageBookmark").Select ' delete any possible, previously entered average number Selection.EndKey Unit:=wdLine, Extend:=wdExtend ' and
set the bookmark result to ActiveDocument.Bookmarks("myAverageBookmark").Range.Text = vAverage ' now
call the toggle procedure again Call pToggleProtectDoc End Sub Sub pToggleProtectDoc() ' this
code toggles the form protection. ' first
I declare the word DOC Set Doc = ActiveDocument ' if
the form is password protected, then Doc.Protect NoReset:=True, Type:=wdAllowOnlyFormFields ' remember
to add the password argument Filling
a DropDown on the Fly
Here's my code suggestion with commented explanations... Sub pFillDropDown() 'declare variables for new connection and recordset Dim vConnection as New ADODB.Connection 'provide connection string for data using Jet Provider for Access database vConnection.ConnectionString
= "data source=c:\myDataFolder\myDatabase.mdb;" & _ 'open connection vConnection.Open 'open
a RecordSet with all vRecordSet.Open "SELECT * FROM
ClientInfo",
_ ' make
sure dropdown is empty of ActiveDocument.FormField("myDropDown").
_ ' if
there are no null entries... Do Until vRecordSet.EOF 'close objects vRecordSet.Close 'clear objects to free up memory Set vRecordSet = Nothing Sub pDisplayDropDownChoice() ' a
simple message box to display MsgBox
ActiveDocument.FormFields _ |
|||||||||
|
|
||||||||||