Count # of Char's in Textbox

Hi,
im trying to count some of char's in a textbox control in real time using the ON CHANGE event, but exclude from counting some char's.  I have this sub that that does exactly what i need but its to slow for the onchange event it works best as button or after event. i have been trying to make it so that it doesn't do a loop on the whole string every time the user press's a key, it just loops on the last char in the textbox, and checks to see if they add'ed or deleted a char, but i coudn't get it to rember it when they changed from record to record. any help would be great.  You can mail me at: Tablet@ix.netcom.com if nessasary. thank you for your time.
Dim X As Integer, Counter As Integer, Addition As Integer
X = Len(Me!Inscription.text)
For Counter = 1 To X
If Asc(Mid(Me!Inscription.text, Counter, 1)) >= 65 And Asc(Mid(Me!Inscription.text, Counter, 1)) <= 90 Then addition = addition + 1
If Asc(Mid(Me!Inscription.text, Counter, 1)) >= 97 And Asc(Mid(Me!Inscription.text, Counter, 1)) <= 122 Then addition = addition + 1
If Asc(Mid(Me!Inscription.text, Counter, 1)) >= 48 And Asc(Mid(Me!Inscription.text, Counter, 1)) <= 57 Then addition = addition + 1
Next
[letters] = addition
LVL 2
JambyteAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cekmanCommented:
Did you try putting your code on the AFTERUPDATE event of the control rather than the ONCHANGE. That way the code will only get executed once when they exit the control rather than on each keypress.

What do you mean about not 'remembering it when change records'? What does it need to remember. If it needs to remember the inscription value could you store this in a public variable at the end of the code?

Hope this helps
Good Luck
CEkman
0
JambyteAuthor Commented:
yeah it works as an after up date event, but i would like it in the on change event.
would you know how to make this an on change event for the textbox control?

0
ramromconsultant Commented:
You can speed things up considerably by computing Asc() once and combining the tests:
dim ch ' variant runs MUCH faster than String!!!
ch = Asc(Mid(Me!Inscription.text, Counter, 1))
If ch >= 65 And ch <= 90 Or ch >= 97 And ch <= 122 Or ch >= 48 And ch <= 57 Then c = c + 1

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JambyteAuthor Commented:
basicly i need to get ride of the for next loop i have in there, know of any way i can do that

0
ramromconsultant Commented:
Why do you want to get rid of the loop?
0
cekmanCommented:
Seems your wanting to evaluate the key the user just typed - if this is so - try the KEYPRESS event. That way you can evaluate each key as it is pressed instead of looping through the entire string on each change.
0
JambyteAuthor Commented:
im trying to do that with the on change evevnt, i can't just use the keypress event cuz then i won't know if someone has deleted a char or add'ed one, i still have to count the number of char's  in the string, but i only have to preform the code that checks what char they entered last, get it? see right now it checks the whole string for my char's to exclude it only needs to check the last one they enter in. it would make it much faster.
0
ramromconsultant Commented:
Unlock the question and I'll propose an answer.
0
JambyteAuthor Commented:
yeah go for it
0
ramromconsultant Commented:
In the declaration section of the form module put:
Dim Key, SelLen, Modifier

This may seem like a long-winded answer, but it is as efficient (IMHO) as it can be. The only place where a loop is involved is when the user hilights (Selects) some text and cuts it, or pastes text in. In essence each character is evaluated once when it is entered or deleted.

Private Sub Text5_Keydown(KeyCode As Integer, Shift As Integer)
' this is the Keydown Event Procedure for the text box
' it remembers the keycode of the last key pressed
' the modifier of the key (2 = ctrl)
' and the length of any selected text (before the selection is deleted)
  Key = KeyCode
  Modifier = Shift ' 1 = shift, 2 = ctrl, 4 = alt
  SelLen = Text5.SelLength
End Sub

Private Sub Text5_Change()
' this is the OnChange Event Procedure for the text box
' ctr is the counter of chars that are alphanumeric
' prev holds the text that was in the text box before the current change
' length is the length of the text that was in the text box before the current change
'  used to determine # of chars pasted
' since comparisons are case insensitive we need only check against upper case
Static ctr%, prev, length%
Dim i%, ch, NumPasted%
  If SelLen > 0 Then ' user had selected some text, which was deleted
  ' decrement ctr for each alphanumeric char in the selection
    For i = Text5.SelStart To Text5.SelStart + SelLen
      ch = Mid(prev, i, 1) ' next char in the selection
      If ch >= "A" And ch <= "Z" Or ch >= "0" And ch <= "9" Then ' alphanumeric char deleted
        ctr = ctr - 1
      End If
    Next
  ElseIf Key = 8 Or Key = 46 Then ' bs or del with no current selection
    ch = Mid(prev, Text5.SelStart + 1, 1) ' deleted char to left
    If ch >= "A" And ch <= "Z" Or ch >= "0" And ch <= "9" Then
       ctr = ctr - 1
    End If
  ElseIf Key = 86 And Modifier = 2 Then  ' ctrl-v; user pasted text in
    NumPasted = Len(Text5.Text) - length
    For i = Text5.SelStart - NumPasted + 1 To Text5.SelStart
      ch = Mid(Text5.Text, i, 1) ' next char in the selection
      If ch >= "A" And ch <= "Z" Or ch >= "0" And ch <= "9" Then ' alphanumeric char pasted
        ctr = ctr + 1
      End If
    Next
  End If
  ' increment ctr if current key is alphanumeric
  ch = Chr(Key)
  If Modifier <> 2 Then ' ctrl key not down
    If (ch >= "A" And ch <= "Z") Or (Modifier <> 1 And ch >= "0" And ch <= "9") Then ' alphanumeric char entered
     ctr = ctr + 1
    End If
  End If
  prev = Text5.Text ' remember text for next pass
  length = Len(Text5.Text) ' remember length for next pass
  Text7 = ctr ' text7 is a text box on my form that shows the current count of alphanumeric chars
End Sub

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JambyteAuthor Commented:
it doesn't when i press backspace or delete  it doesn't erase, it does how ever erase and subtrack when i select text with the mouse. any ideas?
0
ramromconsultant Commented:
Are you saying that backspace and delete are not deleting the character they are supposed to delete? It works OK for me.
0
JambyteAuthor Commented:
if i press ctrl backspace it doesn't subtrack, also,i can't get it to delete the part that i select with the mouse, i was wrong when i said it does, it doesn't, it only deletes one char when i Cut or select text and delete.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.