PDA

View Full Version : Manadatory fields in Word form skips to next field after error



Rightasrainy
10-10-2012, 04:12 AM
Hello my clever friends!

I am afraid to say I am a complete VBA newb, so please excuse my simple explanation...

I am creating a new user form in Word, and need to make some fields mandatory. So far I am using;

Public Sub ExitText()
With GetCurrentFF
If Len(Trim(.Result)) = 0 Then
MsgBox "You can't leave " & .Name & " blank"
End If
End With
End Sub
Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function

Which works fine, BUT after the error message is given the cursor jumps to the next form field. I believe there is a bug that means the macro runs too quickly, and as a result, although the cursor does return to the original formfield for an instant, it then jumps to the formfield you tabbed to or clicked in... I have found this online;

Fortunately, you can get round this timing bug (as indeed you can get around most of Word VBA's timing bugs) by using Application.Ontime. The following pair of macros working together do work
Sub ExitText2()
With ActiveDocument.FormFields("Text2")
If Len(.Result) > 0 And Left$(.Result, 3) <> "KLM" Then
Application.OnTime When:=Now + TimeValue("00:00:01"), Name:="GoBacktoText2"
MsgBox "The first three letters must be 'KLM'"
End If
End With
End Sub
Sub GoBacktoText2()
ActiveDocument.Bookmarks("Text2").Range.Fields(1).Result.Select
End Sub



But I'm not sure how to apply the Application Online Macro to my current Macro??

If someone can solve this you will make me a very happy lady.

Thank you in advance!

Lorraine

gmaxey
10-10-2012, 04:47 AM
Lorraine,

First, it sounds like you are creating an online/protected/e-form and not a userform. There is a big difference: http://gregmaxey.mvps.org/word_tip_pages/create_employ_userform.html

Secondly, 1 second is a long time to wait in the computing world.

Option Explicit
Dim m_FF As FormField
Public Sub FFOnExit()
Set m_FF = GetCurrentFF
With m_FF
If Len(Trim(.Result)) = 0 Then
MsgBox "You can't leave " & .Name & " blank"
Application.OnTime When:=DateAdd("s", 0.1, Now), Name:="GoBacktoFF"
End If
End With
End Sub

Private Function GetCurrentFF() As Word.FormField
With Selection
If .FormFields.Count = 1 Then
Set GetCurrentFF = .FormFields(1)
ElseIf .FormFields.Count = 0 And .Bookmarks.Count > 0 Then
Set GetCurrentFF = ActiveDocument.FormFields(.Bookmarks(.Bookmarks.Count).Name)
End If
End With
End Function
Sub GoBacktoFF()
ActiveDocument.Bookmarks(m_FF.Name).Range.Fields(1).Result.Select
End Sub

Rightasrainy
10-10-2012, 05:09 AM
Hello,

Thank you very much for answer,

However this now doesn't even bring up an error message?? It is acting as if there are no macros.

Unfortunately I am unable to access your site from my works network!

Thanks again.

Lorraine

gmaxey
10-10-2012, 05:50 AM
There is no error message. Have you set FFOnExit to run as the OnExit macro?

Rightasrainy
10-10-2012, 06:00 AM
Oh dear. Feel silly now. Told you I was a newb!

Very much appreciated.

Thank you Greg :)

gmaxey
10-10-2012, 06:17 AM
My pleasure.