Newbie needs help with editing form fields for numeric values

B

Becki

Hi,
I have a Word 2007 form with 28 individual text fields where a user
must enter valid numbers. I found a post in this group with code that
was similar to what I wanted to do and am trying to modify it without
much success. I'm very new at this and any help would be very much
appreciated! Thanks in advance. :)
~ Becki

What I'm attempting to do:
When the user clicks on the 'Calculate' button, loop through the 28
fields, named 'TextBox1' through 'TextBox28' and edit them to make
sure they are numeric. If it finds one that isn't tell the user via a
message box and place the cursor in that field.

The problem:
I'm getting an error 424 - Object required on the statement 'Set fldFF
= TextFileName'

The code:

' Define public variables
Public fldFF As Word.FormField

Sub GoBacktoFF()
' ActiveDocument.Bookmarks("TextBox1").Result.Select ' Commented
out for now; change to use fldFF?
End Sub

Sub CalcButton1_Click()
' Make sure the hours entered are numeric
Dim TextFieldIndexInteger, MaxIndexInteger As Integer
Dim TextFieldName As Variant

TextFieldIndexInteger = 1
MaxIndexInteger = 28

Do Until TextFieldIndexInteger > MaxIndexInteger
TextFieldName = CVar("TextBox" & TextFieldIndexInteger)
Set fldFF = TextFieldName

With fldFF
If Not IsNumeric(fldFF.Result) Then
Application.OnTime When:=Now + TimeValue("00:00:01"),
_
Name:="GoBacktoFF"
Msg = MsgBox("Enter a valid number", 0, "Part II:
Estimate the Activities")
End If
End With
TextFieldIndexInteger = TextFieldIndexInteger + 1

Loop
 
D

Doug Robbins - Word MVP

See the article "How to validate the contents of a formfield in a Word form"
at:

http://www.word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm

Why not do that on exit from each of the fields instead of wait until the
calculation is attempted.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
B

Becki

Thanks for your help, Doug. I would prefer to do it on each field,
but I don't know how to assign the macro. My experience with VB,
before this project, was limited to Visual Studio which had the
option, through the properties window as the article described, to
assign a macro on exit. I am limited to using Word 2007 and the VB
editor, and I can't find a property that would allow me to assign a
macro to a form field. The properties window has categories of
Appearance, Behavior, Data, Font, Misc and Scrolling. I briefly
considered duplicating the code 28 times for each field, but that is
the epitome of bad programming. :) I then tried creating a macro
using the macro recorder to see what the event is named when I leave a
field, and determined it was,for example, 'TextBox1_Change()', which
means I would end up with 28 modules doing the same thing. That led
me to the approach of using a loop to edit each field. This sounds
like it should be much simpler than I'm making it, there must be a key
concept I'm just not getting. Thanks again for your help.
 
J

Jay Freedman

Piecing together clues from your posts, you are mixing together two
different kinds of text fields. The macro you posted deals with
"formfields", which work in a document that is "protected for forms".
However, the text fields you describe, which have a properties window
and _Change() procedure, are ActiveX controls that are completely
different. No wonder you're having trouble -- the techniques that work
for one kind don't work at all with the other kind.

I would strongly discourage the use of ActiveX fields of any kind. You
can read about them at
<http://msdn2.microsoft.com/en-us/library/aa140269(office.10).aspx>,
especially the discussion under "Appropriateness for the Task".

Form fields for protected forms are covered in these articles:
http://www.computorcompanion.com/LPMArticle.asp?ID=22
http://www.computorcompanion.com/LPMArticle.asp?ID=46
http://www.computorcompanion.com/LPMArticle.asp?ID=119
http://www.computorcompanion.com/LPMArticle.asp?ID=127

This is the kind of field Doug expected you to have; one of their
features is the ability to assign an "exit macro", a macro that runs
whenever the cursor leaves the field.

The last of these articles also discusses using a UserForm (a custom
dialog) to enter data for a form; that topic is expanded in
http://gregmaxey.mvps.org/Create_and_employ_a_UserForm.htm
This is a much more user-friendly method than a protected form, which
has the unfortunate side effect of disabling some popular features of
Word such as spell-checking and inserting graphics. The code behind a
userform can also validate entries at several times: When the user
clicks the OK button, or when the cursor leaves an individual field,
or even when the user types each character within the field.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top