Form Fields

R

ron

Hi folks

I'm rather new to using VBA in word, but have done some
previous work in Access. I'm the only one in my office
who has any idea about 'stuff behind the scenes', and as a
result, I've been asked to try to modernise a few of our
documents.

I'm working in Word 2000, running under either NT4 or XP.
I have created a document with a series of form fields
(both text & check boxes). The form is protected. I am
trying to write 'Onexit' macros for the form fields in
order to confirm that the user has completed the field,
and to validate any data that they have enterred. Each
field has a unique bookmark allocated in teh form field
properties screen.

Everything I've done seems to work ok except for the final
bit - if the user has either failed to complete the field,
or has completed it invalidly, I want to either :

1) Blank the field (if appropriate) and set the cursor
back to the start of that field, or,

2) If the field is one of a series of fields (either
text or check box), blank them all and set the cursor back
to the start of the the first box in the series (which may
or may not be the first box in the document)

If I was doing it in Acess, I would simply set the focus
to the particluar field using it's name, but that doesn't
seem to work in Word - the cursor just seems to go on to
the next field and I can't figure out how to fix this!

Grateful for any help that you can provide. Also, if
anyone could recommend a good book available in the UK,
I'd be most appreciative.

Thanks

Ron Snape
 
D

DA

Hi Ron

Here's a sample routine that shows you how to address a
checkbox and textfield (named "Check1" and "Text1"
respectively).

---------
Sub ChangeFields()
With ThisDocument
.FormFields("Check1").CheckBox.Value = True
If .FormFields("Text1").Result = "" Then _
MsgBox "Please enter a value in the selected textbox"
.FormFields("Text1").Select
End With
End Sub
 
R

Ron

Dennis

Thanks very much. Unfortunatley, it still isn't working.
After having looked at various other posts on similar
problems, I've tried at least 4 different ways to do this
now, and none of them seem to work.

The different variations are (choose any one of the
commented-out lines):

---------
With ThisDocument
If .FormFields("Date1").Result = "" Then
MsgBox ("Enter the date of the 1st day of Illness")
' .FormFields("Date1").Select
' .Bookmarks("Date1").Range.Fields(1).Result.Select
' Selection.GoTo What:=wdGoToBookmark, Name:="Date1"
' Selection.GoTo What:=wdGoToField, Name:="Date1"
End If
End With
---------

This is the 4th field on the document. The 1st 3 don't
have any validation attached to them at all. When tabbing
out of the 4th field without entering a date value, it
clearly goes through the code (I've taken it through step
by step in the debugger) and generqates the error message,
but then it seems to go into the following field and
activate the 'on entry' macro for that field, but still
doesn't go back to the 4th field. What I am trying to do
is to get it to go back to the start of the 4th field
("Date1") without going into the next field, and highlight
it in the same way that it is highlighted when you tab
into it from the field before it.

Am I doing something stupid, or is there something wrong
with my setup or environment?

Grateful for any further advice you can give.

Thanks

Ron

------Original Message-----
 
D

DA

My guess is that you are running your code as the fourth
field exit macro. What you should be doing is moving your
validity check onto the entry macro for the fifth field.

All the best,
Dennis
 
R

Ron

Dennis

Yes - you're right. What I was trying to do was to write
a small unique macro for each field to ensure that the
user had completed it correctly before they were allowed
to move onto the next field. I've re-written my macro on
the basis that it is activated 'on-entry' and checks all
of the previous fields.

It's still disconcerting to see the cursor move to the
next field (which then becomes highlighted) with the
MsgBox appearing superimposed on top - this was one of the
things that I was trying to avoid.

Thanks for your help with this. Whilst I can run with
your suggested solution, do you know of any way that
the 'on-exit' macro can return the focus back to the field
that has just been left without it moving to the next
field - I got the impression from Mark Tangard's article
on the Microsoft Word MVP FAQ Site that the instruction

'ActiveDocument.Bookmarks("Text4").Range.Fields
(1).Result.Select'

would do this, but it doesn't seem to.

Thanks for your help - at least I've got something that
will do the job.

Cheers
Ron
 

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