Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.
VBE: Removing Error Coding
From Wiki
Please note that this is only lightly tested and could make a complete mess of your code.
See also: http://wiki.lessthandot.com/index.php/VBE:_Adding_Error_Coding
Here are some notes on removing standard error coding. This is generally in the form:
- Private Sub ACommand_Click()
- On Error GoTo Err_ACommand_Click
- [PROCEDURE_BODY]
- Exit_ACommand_Click:
- Exit Sub
- Err_ACommand_Click:
- MsgBox Err.Description
- Resume Exit_ACommand_Click
- End Sub
After running the code below, there is a reasonable chance that you will end up with something like this:
- Private Sub ACommand_Click()
- ''**: On Error GoTo Err_ACommand_Click
- [PROCEDURE_BODY]
- ''**Exit_ACommand_Click:
- ''** Exit Sub
- ''**
- ''**Err_ACommand_Click:
- ''** MsgBox Err.Description
- ''** Resume Exit_ACommand_Click
- ''**
- End Sub
It should not be too difficult to modify the code to change the wizard error coding to something that suits you better or to include the name of the procedure in the error message.
- Sub Main(DBPath As String, Optional DBPass As String = "")
- ''This is not suitable for running unattended or in the same
- ''database as the code to be modified. It is only very lightly
- ''tested and includes quite a few rigid assumption.
- Dim ap As New Access.Application
- Dim oVBE As Object
- Dim mdl As Object
- Dim ProcArray()
- Dim sProcName As String
- Dim sFind As String
- Dim lSLine As Long
- Dim lELine As Long
- Dim lEnd As Long
- Dim sLine As String
- Dim r As Long
- Dim i, j
- ''Database to be updated
- ap.Visible = True
- ap.OpenCurrentDatabase DBPath, False, DBPass
- ''Code project including forms and class modules
- Set oVBE = ap.VBE.ActiveVBProject.VBComponents
- ''The list returned is of modules containing this line
- sFind = "On Error GoTo Err_" '' & sProcName
- ''An array of procedures to change
- GetErrProcs oVBE, ProcArray, sFind
- ''ProcArray, 2 is the procedure name
- For i = 0 To UBound(ProcArray, 2)
- ''ProcArray(0, i) is the module name
- Set mdl = oVBE(ProcArray(0, i)).CodeModule
- 'The line beginning sub, function etc
- sProcName = ProcArray(1, i)
- lSLine = mdl.ProcBodyLine(sProcName, r)
- ''NB Not procbodyline, count is counted from procstartline, which includes
- ''the comments and blank lines before ProcBodyLine
- lELine = (mdl.ProcCountLines(sProcName, r) + mdl.ProcStartLine(sProcName, r))
- ''lSline and LEline are updated to find position
- If mdl.Find(sFind & sProcName, lSLine, 0, lELine, 0) Then
- ''line to change
- sLine = mdl.Lines(lSLine, 1)
- ''Skip previous run
- If Left(sLine, 4) <> "''**" Then
- ''Just in case this line is a combination of two lines
- If InStr(1, sLine, ": " & sFind & sProcName) > 0 Then
- sLine = Replace(sLine, ": " & sFind & sProcName, " ''**" & ": " & sFind & sProcName)
- Else
- sLine = Replace(sLine, sFind & sProcName, " ''**" & sFind & sProcName)
- End If
- ''Comment line out in a reversible way
- ''Find & Replace on code will easily replace ''**
- mdl.ReplaceLine lSLine, sLine
- End If
- End If
- ''This is the start of the standard error block inserted by add-ins
- sFind = "Exit_" & sProcName & ":"
- lSLine = mdl.ProcBodyLine(sProcName, r)
- lELine = (mdl.ProcCountLines(sProcName, r) + mdl.ProcStartLine(sProcName, r))
- ''Last line but one for the procedure
- lEnd = (mdl.ProcCountLines(sProcName, r) + mdl.ProcStartLine(sProcName, r)) - 2
- If mdl.Find(sFind, lSLine, 0, lELine, 0) Then
- ''lEline is no use here because it is updated by find.
- ''This comments out all lines from the Exit_ label
- ''to the last line.
- For j = lSLine To lEnd
- If Left(mdl.Lines(j, 1), 4) <> "''**" Then
- sLine = "''**" & mdl.Lines(j, 1)
- ''Debug.Print sLine
- ''Stop
- mdl.ReplaceLine j, sLine
- End If
- Next
- End If
- Next
- End Sub
- Function GetErrProcs(oVBE, ProcArray, sFind)
- ''A list of procedures with error coding
- Dim mdl As Object
- Dim aModLines As Variant
- Dim sModText As String
- Dim lELine As Long
- Dim i, j
- Dim k As Long, r As Long
- j = -1 ''First run
- sFind = "On Error GoTo Err_"
- For i = 1 To oVBE.Count
- Set mdl = oVBE(i).CodeModule
- lELine = mdl.CountOfLines
- If lELine > 2 Then
- sModText = mdl.Lines(1, lELine)
- If InStr(sModText, sFind) > 0 Then
- aModLines = Split(sModText, vbCrLf)
- For k = 0 To UBound(aModLines)
- If InStr(aModLines(k), sFind) > 0 Then
- If j = -1 Then
- j = 0
- ReDim Preserve ProcArray(1, 0)
- Else
- j = j + 1
- ReDim Preserve ProcArray(1, j)
- End If
- ProcArray(0, j) = mdl.Name
- ProcArray(1, j) = mdl.ProcOfLine(k, r)
- End If
- Next
- End If
- End If
- Next
- End Function



LTD Social Sitings
Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.