Tech Eval



Hi,

I have a textbox in a userform that I am trying to limit the entries (real
time) to to characters allowed in a bookmark name (e.g., alpha characters,
numbers, "_"). The first character must me a letter and the maximum length
is 40 characters. I am also replacing spacebar entris (after the first
character) with "_".

The code below seems to be working but I was wondering if there was a way to
abbreviate "Me.TextBox1.Text" in some way and as I built this on the fly I
suspect that I have driven a tack with a ten pound hammer and have
overlooked a more graceful way of achieving the desired result.

Any ideas for improvement are appreciated.


Private Sub TextBox1_Change()
testChar = Right(Me.TextBox1.Text, 1)
If Len(Me.TextBox1.Text) = 1 Then
If InStr(" 0123456789", testChar) > 0 Then
Me.TextBox1.Text = Left(Me.TextBox1.Text, Len(Me.TextBox1.Text) - 1)
MsgBox "Bookmark name must begin with a" _
& " letter.", vbInformation & vbOKOnly, "Invalid Character"
End If
End If
On Error Resume Next
If InStr(Chr$(32), testChar) > 0 Then
Me.TextBox1.Text = Left(Me.TextBox1.Text, Len(Me.TextBox1.Text) - 1) _
& Chr$(95)
End If
On Error GoTo 0
On Error Resume Next
If InStr(Chr$(44) & " `~!@#$%^&*()-+={}[]|?/><:;""", testChar) > 0 Then
Me.TextBox1.Text = Left(Me.TextBox1.Text, Len(Me.TextBox1.Text) - 1)
End If
On Error GoTo 0
If Len(Me.TextBox1.Text) > 40 Then
MsgBox "Bookmark name is limited to 40 characters.", _
vbInformation & vbOKOnly, "Limit"
Me.TextBox1.Text = Left(Me.TextBox1.Text, 40)
End If
End Sub
--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.


.



Relevant Pages

  • Re: Tech Eval
    ... speeds, but the patterns are shorter and less confusing than lists of ... characters. ... On Error GoTo 0 ... >Private Sub TextBox1_Change ...
    (microsoft.public.word.vba.userforms)
  • Re: FSO Issues with Unicode
    ... That didn't show me which characters were the problem. ... If you find that "simple script", please post it, I would appreciate it. ... Dim strParam2, xmldoc, pi, e, sTemp ... On Error GoTo 0 ...
    (microsoft.public.scripting.vbscript)
  • Re: Tech Eval
    ... Greg Maxey/Word MVP ... > characters. ... > On Error GoTo 0 ... >> Private Sub TextBox1_Change ...
    (microsoft.public.word.vba.userforms)
  • Re: Tech Eval
    ... >time) to to characters allowed in a bookmark name (e.g., alpha characters, ... >Private Sub TextBox1_Change ... >On Error GoTo 0 ... control with a default property, you do not need to include the .text ...
    (microsoft.public.word.vba.userforms)
  • Problem with VBA returning the contents of a long formula.
    ... formulas that are longer than 1022 characters including the leading ... Dim BigString As String ... Dim ThisPartIndex As Integer ... On Error GoTo 0 ...
    (microsoft.public.excel.misc)