Solved

Count # of Char's in Textbox

Posted on 1998-02-07
13
562 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now