Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word 97 -- Text Box Validation in VBA

Status
Not open for further replies.

vbaprogammer

Programmer
Sep 16, 1999
59
US
I have a text box controlled by a checkbox which, on exit, cannot be empty. I prefill the text box with the format (999,999-99-99) [txtSelectLoanNumber].

On exit the code checks for content and length, and displays a message box if not correct. Otherwise it reformats the text input by the user, and moves to the next text box (txtSelectLender).

However, if I wish to change my mind, and uncheck the checkbox, I still get the message "Invalid Loan Number". Actually it should clear the textbox (txtSelectLoanNumber) and then disable the textbox.

If the loan number is incorrect, the cursor should move back to the txtSelectLoanNumber text box, but it also moves to the next box.

Here's my code. I'm hoping someone can point out the problem. (Remember, this is Word and some events and properties in Access are not available here.)

Thanks,

Dan

(Each statement should be on one line below.)
'===Checkbox to enable text boxes
'=======
Private Sub chkSelectLoanNumber_Click()
If Me.chkSelectLoanNumber Then
Me.txtSelectLoanNumber = "" 'Whether true or false, clear text boxes
Me.txtSelectLender = ""
Me.txtSelectLoanNumber.Enabled = Me.chkSelectLoanNumber.Value
Me.txtSelectLender.Enabled = Me.chkSelectLoanNumber.Value
On Error Resume Next
Me.txtSelectLoanNumber.SetFocus
End If
End Sub

Private Sub txtSelectLoanNumber_Enter()
If Len(txtSelectLoanNumber) = 0 And txtSelectLoanNumber <> &quot;999,999-99-99&quot; Then
Me.txtSelectLoanNumber.Text = &quot;999,999-99-99&quot;
Me.txtSelectLoanNumber.SelStart = 0
Me.txtSelectLoanNumber.SelLength = 13
Else
Me.txtSelectLoanNumber.SelStart = 0
Me.txtSelectLoanNumber.SelLength = 13
End If
End Sub

Private Sub txtSelectLoanNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Me.txtSelectLoanNumber.Text = &quot;999,999-99-99&quot; Or Len(Me.txtSelectLoanNumber.Text) < 10 Or Len(Me.txtSelectLoanNumber.Text) > 13 Then
MsgBox &quot;Invalid Loan Number&quot;
Cancel = True
Me.txtSelectLoanNumber.SetFocus 'Doesn't work
Me.txtSelectLoanNumber.SelStart = 0
Me.txtSelectLoanNumber.SelLength = 13
Else
Me.txtSelectLoanNumber.Text = Format(Me.txtSelectLoanNumber.Text, &quot;000\,000\-00\-00&quot;)
End If
End Sub


[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top