Re: Tech Eval



JGM,

While I couldn't duplicate the problems you encountered in my code, I see
the merit in yours. Thanks.


--
Greg Maxey/Word MVP
See:
http://gregmaxey.mvps.org/word_tips.htm
For some helpful tips using Word.

Jean-Guy Marcil wrote:
> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
>
>> 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.
>
> I guess it boils down to personal taste, but I prefer to code against
> a finite list of what I want as opposed to coding against an infinite
> list of what I do not want. I mean, in this case, all I want is "_",
> letters or numbers, that's it. Anything else I do not want. I think
> this solves the On Error Resume Next problem, no?
>
> Also, in your code, as soon as you change the content of the textbox,
> the Change event is recursively called. Since you are making the
> change, you know the change is good, so you do not have to let the
> code run twice to test this new change. Also, you can run into
> problems. For example, in your code, the block:
> If InStr(Chr$(32), testChar) > 0 Then
> changes " " for "_", but since there is a change if the user types a
> " ", it called the Change event again, and somehow, this second run
> removed the "_" (because of
> If InStr(Chr$(44) & " `~!@#$%^&*()-+={}[]|?/><:;""", testChar) > 0
> Then I think). I could not get the code to replace the " " with a "_"
> until I removed the second Change even pass. and exited the Sub (The
> other If blocks are not needed if a pattern is found).
>
> Finally, I prefer starting the code with a "With" block to avoid
> typing "Me.TextBox1" over and over.
>
> So, with these in mind, my version is as follows:
>
> '_______________________________________
> Option Explicit
>
> Public SkipChangeBool As Boolean
>
> '_______________________________________
> Private Sub TextBox1_Change()
>
> If SkipChangeBool Then Exit Sub
> CheckData
>
> End Sub
> '_______________________________________
>
>
> '_______________________________________
> Private Sub CheckData()
>
> Dim TestChar As String
>
> With Me.TextBox1
> TestChar = Right(.Text, 1)
> If TestChar = "" Then Exit Sub 'In case user backspaces back to the
> beginning
> If Len(.Text) = 1 Then
> If Not .Text Like "[A-z]" Then 'And Not .Text Like "_" Then
> SkipChangeBool = True
> .Text = Left(.Text, Len(.Text) - 1)
> SkipChangeBool = False
> MsgBox "Bookmark name must begin with a" _
> & " letter.", vbInformation & vbOKOnly, "Invalid Character"
> Exit Sub
> End If
> End If
> If TestChar Like " " Then
> SkipChangeBool = True
> .Text = Left(.Text, Len(.Text) - 1) & "_"
> SkipChangeBool = False
> Exit Sub
> End If
> If Not TestChar Like "[A-z]" And Not TestChar Like "_" _
> And Not TestChar Like "[0-9]" Then
> SkipChangeBool = True
> .Text = Left(.Text, Len(.Text) - 1)
> SkipChangeBool = False
> Exit Sub
> End If
> If Len(.Text) > 40 Then
> SkipChangeBool = True
> MsgBox "Bookmark name is limited to 40 characters.", _
> vbInformation & vbOKOnly, "Limit"
> .Text = Left(.Text, 40)
> SkipChangeBool = False
> Exit Sub
> End If
> End With
>
> End Sub
> '_______________________________________
>
> Longish, but easy to manage, no?


.



Relevant Pages

  • Re: Restrict Find&Repalce to Selected Text
    ... Greg Maxey/Word MVP ... >> Sub FRinSelectionOnly() ... >> Dim oRg As Range ... >> oRg.InRangeLoop ...
    (microsoft.public.word.vba.general)
  • Re: find and replace text that is not in a table
    ... Thanks Greg. ... Sub TwoSpacesAfterSentence2() ... 'This should prevent most cases of improper double spacing ... As String, newSpace As String) ...
    (microsoft.public.word.vba.general)
  • Re: Prompt to Save When Closing
    ... Thanks, Greg. ... > Public Sub BatchToggleEmbedSmartTabs() ... >Dim PathToUse As String ...
    (microsoft.public.word.docmanagement)
  • Re: not equal to
    ... Thank you Greg. ... Sub ScratchMacro() ... MsgBox "Do this" ... That is the command that I came up with and which works perfectly ...
    (microsoft.public.word.vba.general)
  • Re: Tech Eval
    ... Greg Maxey nous racontait que: ... Also, in your code, as soon as you change the content of the textbox, the ... Private Sub TextBox1_Change ... Dim TestChar As String ...
    (microsoft.public.word.vba.userforms)