Microsoft Access - Compare two text boxes and highlight the differences

Asked By Mahima Chandani on 27-May-13 07:44 AM
Hi,
I have a code to compare contents of two text boxes and highlight the differences which is working fine but due to this even if one character has changed the entire text after the change is highlighted in red color.

what I want to do is to change this code to compare text line by line to highlight the differences.. Can you please help me with this..
The code that I already have is as below:

For i = 1 To Len(InkNew.Value)
InkNew.SelStart = i - 1
InkNew.SelLength = 1
If Mid(InkNew.Value, i, 1) = Mid(InkOld.Value, i, 1) Then
InkNew.SelStart = i
InkNew.SelLength = 1
InkNew.SelColor = vbBlack
Else
InkNew.SelStart = i - 2
InkNew.SelLength = 1
InkNew.SelColor = vbRed
End If
Next
x = x + 1
Robbe Morris replied to Mahima Chandani on 27-May-13 10:57 AM
What is your rule to define when a change effectively stops?

This is line one of testing the logic

This is line four of testing the logic

The "r" in "four" effectively throws off the character by character comparison by position.  So, the remainder of the sentence in the second line will be different.  And, thus the remainder of the entire textbox would be different.

The closest thing you could do is to put each word of each line into an array.  As you iterate each line, you'd simply highlight each word in from TextBox 2 "in the current line" that didn't exist in that line for TextBox 1.
Mahima Chandani replied to Robbe Morris on 28-May-13 08:34 AM

I have tried but am just not getting the desired results.. any help with the code will be really appreciated.

Regards,
Mahie
Mahima Chandani replied to Robbe Morris on 30-May-13 08:26 AM
Hi,
I have now modified my code to compare each word.. however while highlighting it does some vague behaviour.. it does not highlight some parts of the text or highlights is wrongly.. can you tell me what am i missing in this code.

Private Sub CompareText1()
Dim txtold() As String, txtnew() As String
txtold = Split(InkOld.Value, " ")
txtnew = Split(InkNew.Value, " ")
ss = 0
ss1 = 0
If UBound(txtnew) > UBound(txtold) Then
ubb = UBound(txtold)
Else
ubb = UBound(txtnew)
End If
st = 1
For i = 0 To ubb
If StrComp(txtnew(i), txtold(i), vbTextCompare) = 0 Then
st = InStr(st, InkNew.Value, txtnew(i))
lenn = Len(txtnew(i))
InkNew.SelStart = st - 1
InkNew.SelLength = lenn + 1
InkNew.SelColor = vbGreen
Else
st = InStr(st, InkNew.Value, txtnew(i))
lenn = Len(txtnew(i))
InkNew.SelStart = st - 1
InkNew.SelLength = lenn + 2
InkNew.SelColor = vbRed
End If
Next
End Sub

Regards,
Mahie