X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
   Reply to this topicStart new topic
> Compare Code In 2 Modules, Any Version    
post Nov 19 2015, 07:35 AM

UtterAccess VIP
Posts: 4,432
Joined: 5-June 07
From: UK

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


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"
    strg1 = getcodefrm(frm1)
    strg2 = getcodefrm(frm2)
    If strg1 = strg2 Then
        MsgBox ("Compared forms: " & vbCrLf & vbCrLf & _
            frm2 & vbCrLf & _
            frm1 & vbCrLf & vbCrLf & _
            "code is the same")
'compare code, a character at a time, and report first difference
        x = 1
        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))
            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
Go to the top of the page

Custom Search
RSSSearch   Top   Lo-Fi    13th December 2017 - 02:00 AM