PDA

View Full Version : Count occurrences in a textbox



lynnnow
04-16-2006, 11:39 PM
Hi,

As you will see the image, I've created a userform to mark errors for a particular document. The first frame textboxes get its values from activerow columns, the second and third frame textboxes have entries that are inserted by the user. Only the yellow label in the third frame (Non-Fatal Errors) gets its value from the activerow column.

The multiple values that are inserted in the second and third frame textboxes are separated by semicolons. I would like to count the number of semicolons in the respective "Correct" textbox and display the count in the corresponding textbox next to the "Correct" textbox. I know that a textbox_change event will be used, but I don't know what command should be used to count the number of semicolons. I referred this site for counting semicolons while using it in Excel formulas, but I've not been successful in finding a code for it.

I'm using Excel 1997 on Win 2k. Can't change the combination, so please help me out.

Hoping for a favorable reply.

Lynx : pray2: :help

OBP
04-17-2006, 02:36 AM
Has each "Correct" text box got a different name?
The vb would be something like
dim count1 as integer, count as integer
For count1 = 1 to len(TextboxName)
if mid(textboxname,count1,1) = "," then count = count +1
next count1
othertextbox = count

Bob Phillips
04-17-2006, 03:21 AM
You could use something like


Private Sub txtFormatCorrect_Change()
With Me.txtFormatCorrect
Me.txtFormatCount.Text = Len(.Text) - Len(Replace(.Text, ";", ""))
End With
End Sub

lynnnow
04-17-2006, 04:04 AM
Hi,

Thanks OPB & xld for your suggestions. I'll try them out, however, I found a guiding finger in one of the posts here and this is what it works out to.

I've got to use the textbox variables since there are so many textboxes. This has made my code longer, though functional. I know there will be a way to make the code shorter, but that would include reconfiguring the rest of the code to suit this change. I'll have to redesign the form. Since it is a working tool as well as a kinda work-in-progress, making changes is going to be tedious and I don't have a luxury of time to tweak the form and code so much.


If TextBox65.Text <> "" Then 'the correct field
TextBox112.Value = 1 'the counter field
For i = 1 To Len(TextBox65.Text)
char = Mid(TextBox65.Text, i, 1)
If char = ";" Then
TextBox112.Value = TextBox112.Value + 1
End If
Next i
Else
TextBox112.Value = 0
End If


Now that this quest is out of the way, another one stands in its place. If you look at the form, in the third frame, I need to count the number of types of errors that have been committed (this is the count of error types, i.e. Word Misuse, Captialization, etc. as a category, not the number that will be displayed in the error counter). I've worked my brains on different methods, but seem to be stuck at a deadend of ideas.

Any directional clues are appreciated.

Lynx

lynnnow
04-17-2006, 04:58 AM
Hi,

Looks like I post my queries and find solutions for them myself.

What I did was inserted a counter in every textbox section to increment the counter instead of using a counter in the end (that is what I was doing previously)

This is how the code looks now:

If TextBox85.Text <> "" Then
ETC = ETC + 1
TextBox122.Value = 1
For i = 1 To Len(TextBox85.Text)
char = Mid(TextBox85.Text, i, 1)
If char = ";" Then
TextBox122.Value = TextBox122.Value + 1
End If
Next i
Else
TextBox125.Value = 0
End If