Hi,
I am struggling with a compare function of two TextBoxes. The situation is as follows: TextBox1 contains text, say 50 words. TextBox2 contains text (suppose to be the same amount of words (and also same text)). When the user click on a button, cmdTextStart, a percentage must be given of how accurate the text of TextBox1, compare with the text of TextBox2.
THE PROBLEM: TextBox1 contains text that will always stay the same and is 50 words. TextBox 2, contains the text that could be typed in. Say for instance the person has 1 minute to type the text of TextBox1 in TextBox2. After the user has finised typing the text, he click on the cmdTextStart button and an result must be given of how accurate the text of TextBox1, compare with the text of TextBox2. The problem with the code below is that although there are text typed in TextBox2, it gives the error message : Textbox empty or no periods encountered!". Could someone please help me with this problem?
The code look as follows:
Option Explicit
Private Sub cmdTextStart_Click()
Dim colA As New Collection
Dim colB As New Collection
Dim strA As String
Dim strB As String
Dim Words As Long
Dim Percent As Long
Dim intSpace As Long
Dim intSpaceB As Long
Dim strSentenceA As String
Dim strSentenceB As String
Dim i As Long
strA = Text1.Text
strB = Text2.Text
'assumes that both textboxes have same no. of sentences.
'remove space after period
strA = Replace(strA, ". ", "."
strB = Replace(strB, ". ", "."
strA = UCase(strA) 'uppercase strings
strB = UCase(strB)
Do
intSpace = InStr(1, strA, "."
If intSpace <> 0 Then
strSentenceA = Left(strA, intSpace - 1)
colA.Add strSentenceA
strA = Right(strA, Len(strA) - intSpace)
End If
intSpaceB = InStr(1, strB, "."
If intSpaceB <> 0 Then
strSentenceB = Left(strB, intSpaceB - 1)
colB.Add strSentenceB
strB = Right(strB, Len(strB) - intSpaceB)
End If
Loop While intSpace <> 0
For i = 1 To colA.Count
'MsgBox colA.Item(i) & vbCrLf & colB.Item(i)
Call Compare(colA.Item(i), colB.Item(i), Percent, Words)
Next
If Words = 0 Then
MsgBox "Error: Textbox empty or no periods encountered!"
End
End If
Percent = (Percent / Words) * 100 ' and the percentage
MsgBox "The boxes are " & Percent & "% equal to each other", _
vbInformation, "Result..."
End Sub
Sub Compare(strA As String, strB As String, ByRef Percent As Long, ByRef Words As Long)
Dim colA As New Collection
Dim colB As New Collection
Dim intSpace As Integer
Dim intSpaceB As Integer
Dim i As Integer
Dim k As Integer
Dim x As String
Do
intSpace = InStr(1, strA, " "
If intSpace <> 0 Then
colA.Add Left(strA, intSpace - 1)
x = Left(strA, intSpace - 1)
strA = Right(strA, Len(strA) - intSpace)
Else
x = Right(strA, Len(strA) - intSpace)
colA.Add x
End If
Loop While intSpace <> 0
Do
intSpaceB = InStr(1, strB, " "
If intSpaceB <> 0 Then
colB.Add Left(strB, intSpaceB - 1)
strB = Right(strB, Len(strB) - intSpaceB)
Else
x = Right(strB, Len(strB) - intSpaceB)
colB.Add x
End If
Loop While intSpaceB <> 0
'find which sentence is longer.
If colA.Count < colB.Count Then
Words = Words + colB.Count 'word count of sentence having the more words
For i = 1 To colA.Count
For k = 1 To colB.Count
If colA.Item(i) = colB.Item(k) Then
Percent = Percent + 1
colB.Remove (k)
Exit For
End If
Next
Next
Else
Words = Words + colA.Count
For i = 1 To colB.Count
For k = 1 To colA.Count
If colB.Item(i) = colA.Item(k) Then
Percent = Percent + 1
colA.Remove (k)
Exit For
End If
Next
Next
End If
'----- section doesn't work 'faulty err.. bugs' -----
''find which sentence is longer.
'If colA.Count < colB.Count Then
' For i = 1 To colA.Count
' lngSuccess = InStr(1, strTempB, colA.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempB = Replace(strTempB, colA.Item(i), "", , 1)
' 'MsgBox colA.Item(i) & vbCrLf & strTempB
' End If
' Next
' Words = Words + colB.Count 'word count of sentence having the more words
'Else
' For i = 1 To colB.Count
' lngSuccess = InStr(1, strTempA, colB.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempA = Replace(strTempA, colB.Item(i), "", , 1)
' End If
' Next
' Words = Words + colA.Count 'word count of sentence having the more words
'End If
End Sub
I am struggling with a compare function of two TextBoxes. The situation is as follows: TextBox1 contains text, say 50 words. TextBox2 contains text (suppose to be the same amount of words (and also same text)). When the user click on a button, cmdTextStart, a percentage must be given of how accurate the text of TextBox1, compare with the text of TextBox2.
THE PROBLEM: TextBox1 contains text that will always stay the same and is 50 words. TextBox 2, contains the text that could be typed in. Say for instance the person has 1 minute to type the text of TextBox1 in TextBox2. After the user has finised typing the text, he click on the cmdTextStart button and an result must be given of how accurate the text of TextBox1, compare with the text of TextBox2. The problem with the code below is that although there are text typed in TextBox2, it gives the error message : Textbox empty or no periods encountered!". Could someone please help me with this problem?
The code look as follows:
Option Explicit
Private Sub cmdTextStart_Click()
Dim colA As New Collection
Dim colB As New Collection
Dim strA As String
Dim strB As String
Dim Words As Long
Dim Percent As Long
Dim intSpace As Long
Dim intSpaceB As Long
Dim strSentenceA As String
Dim strSentenceB As String
Dim i As Long
strA = Text1.Text
strB = Text2.Text
'assumes that both textboxes have same no. of sentences.
'remove space after period
strA = Replace(strA, ". ", "."
strB = Replace(strB, ". ", "."
strA = UCase(strA) 'uppercase strings
strB = UCase(strB)
Do
intSpace = InStr(1, strA, "."
If intSpace <> 0 Then
strSentenceA = Left(strA, intSpace - 1)
colA.Add strSentenceA
strA = Right(strA, Len(strA) - intSpace)
End If
intSpaceB = InStr(1, strB, "."
If intSpaceB <> 0 Then
strSentenceB = Left(strB, intSpaceB - 1)
colB.Add strSentenceB
strB = Right(strB, Len(strB) - intSpaceB)
End If
Loop While intSpace <> 0
For i = 1 To colA.Count
'MsgBox colA.Item(i) & vbCrLf & colB.Item(i)
Call Compare(colA.Item(i), colB.Item(i), Percent, Words)
Next
If Words = 0 Then
MsgBox "Error: Textbox empty or no periods encountered!"
End
End If
Percent = (Percent / Words) * 100 ' and the percentage
MsgBox "The boxes are " & Percent & "% equal to each other", _
vbInformation, "Result..."
End Sub
Sub Compare(strA As String, strB As String, ByRef Percent As Long, ByRef Words As Long)
Dim colA As New Collection
Dim colB As New Collection
Dim intSpace As Integer
Dim intSpaceB As Integer
Dim i As Integer
Dim k As Integer
Dim x As String
Do
intSpace = InStr(1, strA, " "
If intSpace <> 0 Then
colA.Add Left(strA, intSpace - 1)
x = Left(strA, intSpace - 1)
strA = Right(strA, Len(strA) - intSpace)
Else
x = Right(strA, Len(strA) - intSpace)
colA.Add x
End If
Loop While intSpace <> 0
Do
intSpaceB = InStr(1, strB, " "
If intSpaceB <> 0 Then
colB.Add Left(strB, intSpaceB - 1)
strB = Right(strB, Len(strB) - intSpaceB)
Else
x = Right(strB, Len(strB) - intSpaceB)
colB.Add x
End If
Loop While intSpaceB <> 0
'find which sentence is longer.
If colA.Count < colB.Count Then
Words = Words + colB.Count 'word count of sentence having the more words
For i = 1 To colA.Count
For k = 1 To colB.Count
If colA.Item(i) = colB.Item(k) Then
Percent = Percent + 1
colB.Remove (k)
Exit For
End If
Next
Next
Else
Words = Words + colA.Count
For i = 1 To colB.Count
For k = 1 To colA.Count
If colB.Item(i) = colA.Item(k) Then
Percent = Percent + 1
colA.Remove (k)
Exit For
End If
Next
Next
End If
'----- section doesn't work 'faulty err.. bugs' -----
''find which sentence is longer.
'If colA.Count < colB.Count Then
' For i = 1 To colA.Count
' lngSuccess = InStr(1, strTempB, colA.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempB = Replace(strTempB, colA.Item(i), "", , 1)
' 'MsgBox colA.Item(i) & vbCrLf & strTempB
' End If
' Next
' Words = Words + colB.Count 'word count of sentence having the more words
'Else
' For i = 1 To colB.Count
' lngSuccess = InStr(1, strTempA, colB.Item(i), vbTextCompare)
' If lngSuccess > 0 Then
' Percent = Percent + 1
' strTempA = Replace(strTempA, colB.Item(i), "", , 1)
' End If
' Next
' Words = Words + colA.Count 'word count of sentence having the more words
'End If
End Sub