Solved

Count # of Char's in Textbox

Posted on 1998-02-07
13
582 Views
Last Modified: 2008-03-06
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
0
Comment
Question by:Jambyte
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
13 Comments
 
LVL 5

Expert Comment

by:cekman
ID: 1967837
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
 
LVL 2

Author Comment

by:Jambyte
ID: 1967838
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
 
LVL 17

Expert Comment

by:ramrom
ID: 1967839
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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
LVL 2

Author Comment

by:Jambyte
ID: 1967840
basicly i need to get ride of the for next loop i have in there, know of any way i can do that

0
 
LVL 17

Expert Comment

by:ramrom
ID: 1967841
Why do you want to get rid of the loop?
0
 
LVL 5

Expert Comment

by:cekman
ID: 1967842
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
 
LVL 2

Author Comment

by:Jambyte
ID: 1967843
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
 
LVL 17

Expert Comment

by:ramrom
ID: 1967844
Unlock the question and I'll propose an answer.
0
 
LVL 2

Author Comment

by:Jambyte
ID: 1967845
yeah go for it
0
 
LVL 17

Accepted Solution

by:
ramrom earned 50 total points
ID: 1967846
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
 
LVL 2

Author Comment

by:Jambyte
ID: 1967847
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
 
LVL 17

Expert Comment

by:ramrom
ID: 1967848
Are you saying that backspace and delete are not deleting the character they are supposed to delete? It works OK for me.
0
 
LVL 2

Author Comment

by:Jambyte
ID: 1967849
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

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question