Solved

Validate an MS Word form field

Posted on 2009-05-06
2
263 Views
Last Modified: 2012-05-06
I have an exit macro on a Word form field to undertake a validation of the input. In the event of an error I use the .select to set the focus back to the field, however, this does not work as it seems to execute the tab key press subsequent to the function return and takes it onto the next field.

How do I ignore the key event to get the focus back onto the field that has the error?
0
Comment
Question by:DavidAreen
2 Comments
 
LVL 17

Accepted Solution

by:
wobbled earned 500 total points
ID: 24313759
Hi,

This happens because the Tab event kicks in after the code has run.  What you need to do is create a global variable to store the error in.

Below is the code to do this.

I have created ExitFieldCode as a sample of how to do this.  I have called my checkbox in this example "thisone"

You then need to put in the following field the code for FieldEntry as code that runs on entering the field.  This will check to see if there is a global variable of an error set and if so it will select that field

Hope this helps
Option Explicit
 

Public gblErr As Boolean

Public gstrField As String
 

Sub ExitFieldCode()
 

    gblErr = False

    gstrField = ""
 

    If ActiveDocument.FormFields("thisone").CheckBox.Value = False Then

        MsgBox "You have not checked this etc"

        gblErr = True

        gstrField = "thisone"

        'other stuff here

    Else

        'stuff here

    End If
 
 

End Sub
 

Sub FieldEntry()
 

    If gblErr = True Then

        ActiveDocument.FormFields(gstrField).Select

    End If
 

End Sub

Open in new window

0
 
LVL 1

Author Closing Comment

by:DavidAreen
ID: 31578414
Works perfectly, thanks.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
*Best* PDF to Word Convertor 4 112
PowerShell Script to export to HTML to MS Word Format 10 142
Surface Book vs Surface Pro 4 56
MS Word Office 365 Mail Merge 2 51
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now