Printable Version of Topic

Click here to view this topic in its original format

UtterAccess Forums _ Access Code Archive _ Compare Code In 2 Modules

Posted by: gemmathehusky Nov 19 2015, 07:35 AM

I just had a situation where I wanted to check whether code had been changed in two versions of a form. It's a pain doing it by inspection - so I put together this little code snippet.

a) It doesn't check that both forms exist.
b) when it finds a difference it shows 100 characters before and after the difference, and I haven't tried to debug this for bof/eof.
c) easy to modify to check reports.
d) I am sure the idea can also be used to check 2 code or class modules.
e) one catch. I think reading the modules must cause any unsaved changes to be dropped - so save changes before trying it.
f) also, it's not smart enough to ignore tabs, line feeds, spacing etc, so it will flag layout differences

Code:

CODE
Sub compare2forms()
Dim frm1 As String
Dim frm2 As String
Dim strg1 As String
Dim strg2 As String
Dim x As Long
Dim ch1 As String
Dim ch2 As String
  
    frm1 = "testform1"
    frm2 = "testform2"
    
checkit:
    strg1 = getcodefrm(frm1)
    strg2 = getcodefrm(frm2)
    
    If strg1 = strg2 Then
        MsgBox ("Compared forms: " & vbCrLf & vbCrLf & _
            frm2 & vbCrLf & _
            frm1 & vbCrLf & vbCrLf & _
            "code is the same")
    Else
'compare code, a character at a time, and report first difference
        x = 1
nextch:
        ch1 = Mid(strg1, x, 1)
        ch2 = Mid(strg2, x, 1)
        If ch1 <> ch2 Then
            MsgBox ("Compared forms: " & vbCrLf & vbCrLf & _
                frm2 & vbCrLf & _
                frm1 & vbCrLf & vbCrLf & _
                "code different at char " & x & vbCrLf & _
                Mid(strg1, x - 100, 200))
        Else
            x = x + 1
            GoTo nextch
        End If
    End If
    
End Sub
  
  
Function getcodefrm(fname As String) As String
  
'get the code from the forms module
Dim x As Long
Dim lines As Long
Dim strg As String
  
     DoCmd.OpenForm fname, acDesign
     If Forms(fname).HasModule Then
        lines = Forms(fname).Module.CountOfLines
        strg = Forms(fname).Module.lines(1, lines)
        getcodefrm = strg
     End If
     DoCmd.Close acForm, fname, acSaveNo
End Function