Compare 2 Code Modules (1 Viewer)

Status
Not open for further replies.

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 14:12
Joined
Sep 12, 2006
Messages
15,613
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) probably can be used to check 2 modules.
e) 1 catch. save changes before running this. I think running the code drops any unsaved edits


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
 [COLOR=red]'compare code, a character at a time, and report first difference[/COLOR]
        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
  
 [COLOR=red]'get the code from the forms module[/COLOR]
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
 
Last edited:

RuralGuy

AWF VIP
Local time
Today, 08:12
Joined
Jul 2, 2005
Messages
13,826
Thanks Dave. It will give us something to play with.
 
Status
Not open for further replies.

Users who are viewing this thread

Top Bottom