PDA

View Full Version : Using Retry/Cancel buttons in a mesage box



BPatters
01-16-2014, 03:04 PM
OK, So I got saddled with working on a form at work. In the form, I'm trying to add a message box that pops up if you don't enter the correct number of digits in the associated textbox for a phone number. At this point, the message box pops up fine, however, the cursor moves to the next message box despite the error. I have played with the retry/cancel button feature and have not been succesful to this point. Currently all there is is a box that pops up to tell you that the information you've entered is invalid.
What I'm trying to accomplish is, have a message box pop up with the Retry and Cancel options. That much I've figured out. But, if you push cancel, it should allow the cursor to move on to the next textbox. If you push retry, the textbox should clear and remian in the current textbox. I've stripped all but the basic code out.
I also should mention that all of my vba coding has come from goole and trial and error. I know nothing about the correct syntax when it comes to writing code. Any help/suggestions you can provide would be greatly appreciated. Here is the current code for the offending textbox.
Thanks


Private Sub TextBox6_LostFocus()
Me.TextBox6.Value = Format(TextBox6.Text, "(###) ###-####")
If Len(TextBox6.Text) <> 14 Then
TextBox6.Text = ""
If Len(TextBox6.Text) <> 14 Then
MsgBox "Please Enter a 10 Digit Phone Number"
End If
End If
End Sub

Jay Freedman
01-20-2014, 09:12 PM
It's clear from your code that the text boxes in your form are ActiveX controls. That makes your task a thousand times harder than if you used form fields (or, in Word 2007 and later, content controls). Quoting from Cindy Meister's article http://msdn.microsoft.com/en-us/library/office/aa140269%28v=office.10%29.aspx


In addition, there is no SetFocus method in the Word document environment. This makes changing the order in which ActiveX controls are selected a bit of a challenge. It's especially critical if data validation is required, where the user should be returned to a control if the content is not acceptable. ...
Note Be extremely careful when testing with active GetFocus and LostFocus events. Be sure to save all your work in Word before doing anything that could cause these to fire, as you could end up in an endless loop. If the focus starts to bounce back and forth between controls, triggering these two events in succession, Word will appear to freeze.

I highly recommend that you replace the ActiveX controls with form fields (see, for example, http://gregmaxey.mvps.org/word_tip_pages/master_your_formfields.html). If you need to stay with ActiveX, modify your code like this:


Private Sub TextBox6_LostFocus()
Dim rsp As VbMsgBoxResult
TextBox6.Text = Format(TextBox6.Text, "(###) ###-####")
If Len(TextBox6.Text) <> 14 Then
TextBox6.Text = ""
rsp = MsgBox("Please Enter a 10 Digit Phone Number", vbRetryCancel)
If rsp = vbRetry Then
Application.OnTime When:=Now + TimeValue("00:00:01"), _
Name:="GoBackToTB6"
End If
End If
End Sub

Private Sub GoBackToTB6()
TextBox6.Activate
End Sub

BPatters
01-21-2014, 07:31 AM
I used your code and I do appreciate the help. The formatting works perfectly. However, I get an error message when textbox6 is activated that says "This method or property is not available because the object refers to a protected area of the document." Do I need to unlock the document to have the textbox re-activate using this code?

Jay Freedman
01-21-2014, 09:57 AM
If all the controls in the form are ActiveX and there are no form fields, then the form should not be protected for "Filling in forms". Instead, you can use the "No changes (Read only)" protection, and mark all of the controls as "exceptions" for Everyone (that is, select a control and check the box in the Exceptions area of the pane, then repeat for the other controls).

If you're using a mix of ActiveX and form fields, so you need the "Filling in forms" protection, then my question would be "Why use ActiveX?" In that case, replace the ActiveX controls with text form fields. Then see http://www.word.mvps.org/FAQs/TblsFldsFms/ValidateFFields.htm for help with the macro.