Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MANDATORY FIELDS IN WORD FORM 3

Status
Not open for further replies.

kettie

Technical User
Sep 18, 2002
24
0
0
AU
I have developed a form in word which has a number of drop down boxes. I would like to force the user to choose an item from the drop down list, but am unsure how to do this.

Is it possible to do this in a Word form and, if so, how can I do this?
 
I'm stealing this from a friend of mine who answered this very question not too long ago on another site:

Let's make a sample, ok ?
Please craete a new document and put 5 formfield (textbox).
Have them bookmark names (right
click->properties->fieldsettings->bookmark), Field1, Field2, Field3 etc...
or Text1, Text2... But make sure all have one unique name.
Now goto VBA (Alt+F11) and insert a new module (Insert->Module) and copy
and paste the following code into this new module.

'----Code Start----
Sub FieldEnter()
If ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Previous Is
Nothing Then Exit Sub
With ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Previous
If Trim(.Result) = "" Then
.Select
End If
End With
End Sub
'----Code End----

Now return document and set ALL formfields' Enter macro as FieldEnter macro (right click on the formfield and select FieldEnter from the RunMacro on Entry dropdown box).

Protect your document and try to navigate between fields from the first one without entering anything, then entering something.

I hope it helps. If you like, you can download the sample doc here:




Anne Troy
 
Actually, that code fails on the very LAST FORM FIELD.

This code accounts for the last form field also...
Code:
Sub FieldEnter()
    If ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Previous Is Nothing Then
        With ActiveDocument.FormFields(ActiveDocument.FormFields.Count)
            If Trim(.Result) = "" Then
                .Select
            End If
        End With
    Else
        With ActiveDocument.FormFields(Selection.Bookmarks(1).Name).Previous
            If Trim(.Result) = "" Then
                .Select
            End If
        End With
    End If
End Sub
:)

Skip,
Skip@TheOfficeExperts.com
 
As if....I'm a southern Quakertonian...that means I migrate south, not north to the LV.

:)

Anyway, thought I oughtta give you a star! And only saying so in case I forget between now and a few seconds from now...

Anne Troy
 
G'day to everyone who replied to my thread. A big thanks.

The problem is that I'm able to get this to work for a text form field, but my document contains drop-down form fields and it doesn't appear to work for that kind of form field (or perhaps I have done something incorrectly).

Does the code need to be changed to cater for drop-down fields?

Thanks again

Kettie
[gorgeous]
 
Skip

Thanks for that help - it seems to work.

If I might be a pain, could I just get some help understanding the code as I will be applying it to an existing document.

Do I have to substitute the "Name" mentioned in the following((Selection.Bookmarks(1).Name))with the unique name I called each of my bookmarks or leave it as is.

Also, do I have to substitute the document name "FormFields" mentioned here (ActiveDocument.FormFields), with the name of my existing document [the name of my document is AccidentReportForm]?

Sorry to trouble you

Kettie
[gorgeous]
 
No problem Kettie,

This is general code.

ActiveDocument.FormFields(Selection.Bookmarks(1).Name)

ActiveDocument is the document that is open and in the active window.

FormFields is a collection of ALL the form fields in the active document

Selection.Bookmarks(1).Name -- first Selection is where your cursor is at the time this procedure runs. There are many Bookmark collections, based on the context. The context is the Selection, which is where your cursor is. Turns out that in that context, there is ONLY ONE Bookmark -- the Bookmark of the Form Field. Since Bookmarks is a collection (a collection of ONE in this instance) we have to determine which Bookmark we are referring to. Well, since there is only one, it's Selection.Bookmarks(1) that we are after. Having established the correct Bookmark, it is the Name Property that we need in order to identify the Form Field.

And then it is the Previous Form Field that we are testing EXCEPT in the instance when the Previous is NOTHING. THen we have to find the LAST Form Field, which we can identify as ActiveDocument.FormFields(ActiveDocument.FormFields.Count), where ActiveDocument.FormFields.Count is the TOTAL number of Form Fields, so it is the LAST Form Field.

Hope this helps :)

Skip,
Skip@TheOfficeExperts.com
 
kettie,

Here's a streamlined version...
Code:
Sub FieldExit()
    Dim obFF As FormField
    With ActiveDocument
        Set obFF = .FormFields(Selection.Bookmarks(1).Name).Previous 'PREV FormField
        If obFF Is Nothing Then _
            Set obFF = .FormFields(.FormFields.Count) 'LAST FormField
        With obFF
            If Trim(.Result) = "" Then _
                .Select
        End With
    End With
    Set obFF = Nothing
End Sub
:)

Skip,
Skip@TheOfficeExperts.com
 
Skip

You are just too good.

Thanks and have a good day (or maybe it's evening where you are). Whatever the time, I hope it's a good one

Kettie
[wavey2]
 
And the kangaroos are hopping and koalas hoeing into the gum leaves down here in beautiful downtown Melbourne town!!

[lol]
 
You did 'bonza' (means fantastic).

You're a 'little ripper' (means teriffic person).

Kettie
[gorgeous]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top