Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

Validate an MS Word form field

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
DavidAreen
Asked:
DavidAreen
1 Solution
 
wobbledCommented:
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
 
DavidAreenAuthor Commented:
Works perfectly, thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now