Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Count # of Char's in Textbox

Posted on 1998-02-07
13
Medium Priority
?
588 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
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 200 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

972 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