stop-validate user input in a form

A

and

The single character value a user enters in a text box in my user form,
should be validated (checked whether it already exists in the active
document). The user box should stay visible until a value is entered
that does not exist in the active document.

How can I approach this problem? With a while loop?

Best regards,

ANDy
 
J

Jean-Guy Marcil

and was telling us:
and nous racontait que :
The single character value a user enters in a text box in my user
form, should be validated (checked whether it already exists in the
active document). The user box should stay visible until a value is
entered that does not exist in the active document.

How can I approach this problem? With a while loop?

Something like this should get you started. Here I assumed that letter case
was irrelevant, if it is relevant, remove the LCase() from the code. Also,
to test this idea, the value to be checked against in the document has to be
delimited by a bookmark which is called "CharValue" in this example. You
could call it whatever you want, just change the code accordingly.

Insert this code in the userform code panel and change TextBox1 to fit you
textbox name.

'_______________________________________
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If LCase(Me.TextBox1.Value) = _
LCase(ActiveDocument.Bookmarks("CharValue").Range.Text) Then
MsgBox "Invalid value, try again."
Me.TextBox1.Value = ""
Cancel = True
End If

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
D

Doug Robbins

The following code should do what you want:

Private Sub txtSingle_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal
Shift As Integer)
Dim i As Long
For i = 1 To ActiveDocument.Characters.Count
If Shift = 0 Then
If Asc(UCase(ActiveDocument.Characters(i))) = KeyCode Then
Calendar.Hide
Else
txtSingle.Text = ""
End If
Else
If Asc(ActiveDocument.Characters(i)) = KeyCode Then
Calendar.Hide
Else
txtSingle.Text = ""
End If
End If
Next i
End Sub

where txtSingle is the control into which the user will enter the character.

--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top