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: Adding 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?title=VBE:_Removing_Error_Coding
Some notes on adding error coding to procedures. The code requires a reference to Microsoft Visual Basic for Applications Extensibility 5.3.
It should insert something on the lines of:
- Private Sub ACommand_Click()
- On Error GoTo Err_ACommand_Click
- [PROCEDURE_BODY]
- ''**INSERTED BY CODE 06/04/2012 00:31:42
- Exit_ACommand_Click:
- Exit Sub
- Err_ACommand_Click:
- MsgBox Err.Description
- LogError "ACommand_Click, " & Err.Number & ", " & Err.Description
- Err.Clear
- Resume Exit_ACommand_Click
- End Sub
The code start here.
- Sub LogError(strError)
- Const ForAppending = 8
- Dim sPath As String
- Dim fs As Object
- Dim ts As Object
- sPath = CurrentProject.Path
- Set fs = CreateObject("Scripting.FileSystemObject")
- If fs.FileExists(sPath & "\ErrorLog.txt") = True Then
- Set ts = fs.OpenTextFile(sPath & "\ErrorLog.txt", ForAppending)
- Else
- Set ts = fs.CreateTextFile(sPath & "\ErrorLog.txt")
- End If
- ts.WriteLine Now & " " & strError
- ts.Close
- End Sub
- Sub Main(DBPath As String, Optional DBPass As String = "")
- ''Microsoft Visual Basic for Applications Extensibility 5.3.
- Dim ap As New Access.Application
- Dim oCode As VBIDE.CodeModule
- Dim oVBE As VBIDE.VBComponents
- Dim pk As vbext_ProcKind
- Dim bSkipClassMods As Boolean
- Dim bSkipFormReportMods As Boolean
- Dim bSkipMods As Boolean
- Dim bAddErrCode As Boolean
- Dim sCodeName As String
- Dim sProcName As String
- Dim sProcType As String
- Dim ProcArray() As Variant
- Dim lProcStartLine As Long
- Dim lProcEndLine As Long
- Dim lStartLine As Long
- Dim lEndLine As Long
- Dim sLine As String
- Dim sFind As String
- Dim sErrLine As String
- Dim sErrCode As String
- Dim i
- ''Module Types
- ''Class module VBIDE = 2 ; Access = 1
- ''Form / Report (prefix form_ or report_) VBIDE = 100 ; Access = 1
- ''Ordinary module VBIDE = 1 ; Access = 0
- ''
- ''VBA / Access : acClassModule = 1 ; acStandardModule = 0
- bSkipClassMods = False '' Type 2
- bSkipFormReportMods = False '' Type 100
- bSkipMods = False '' Type 1
- ''Database to be updated
- ap.Visible = True
- ap.OpenCurrentDatabase DBPath, False, DBPass
- Set oVBE = ap.VBE.ActiveVBProject.VBComponents
- ''Procarray 0 = Module name
- '' 1 = Proc name
- GetProcs oVBE, ProcArray
- For i = UBound(ProcArray, 2) To 0 Step -1
- sCodeName = ProcArray(0, i)
- sProcName = ProcArray(1, i)
- bAddErrCode = True
- Set oCode = oVBE(sCodeName).CodeModule
- If (bSkipClassMods = True And oVBE(sCodeName).Type = 2) _
- Or (bSkipFormReportMods = True And oVBE(sCodeName).Type = 100) _
- Or (bSkipMods = True And oVBE(sCodeName).Type = 1) Then
- LogError sCodeName & ", " & sProcName & ", " & oVBE(sCodeName).Type
- Else
- 'The line beginning sub, function etc
- lProcStartLine = oCode.ProcBodyLine(sProcName, pk)
- ''NB Not procbodyline, count is counted from procstartline, which includes
- ''the comments and blank lines before ProcBodyLine
- lProcEndLine = oCode.ProcCountLines(sProcName, pk) + oCode.ProcStartLine(sProcName, pk)
- sLine = oCode.Lines(lProcStartLine, 1)
- If InStr(sLine, "Sub " & sProcName) = 0 _
- And InStr(sLine, "Function " & sProcName) = 0 Then
- ''Something odd-ish
- LogError sCodeName & ", " & sProcName & ", " & sLine
- Else
- sFind = "On Error GoTo"
- ''Looking at various possibilities
- ''On Error GoTo <label> - this is a deal-breaker, because it means that
- '' error handling has been added already.
- ''<comment>On Error GoTo - also a deal-breaker in this version
- ''On Error GoTo 0 - Not a problem
- ''On Error Resume - Different pattern
- lStartLine = lProcStartLine
- lEndLine = lProcEndLine
- ''lSline and LEline are updated to find position
- If oCode.Find(sFind, lStartLine, 0, lEndLine, 0) Then
- ''line to change
- sLine = oCode.Lines(lStartLine, 1)
- If sLine <> "On Error GoTo 0" Then
- LogError sCodeName & ", " & sProcName & ", " & sLine
- bAddErrCode = False
- End If
- End If
- If bAddErrCode = True Then
- ''Proc type, not avaible from pk, which classifies
- ''both sub and function as the same
- If InStr(sLine, "Sub " & sProcName) > 0 Then
- sProcType = "Sub"
- Else
- sProcType = "Function"
- End If
- sErrLine = "On Error GoTo Err_" & sProcName & vbCrLf
- sErrCode = vbCrLf & "''**INSERTED BY CODE " & Now() & vbCrLf _
- & "Exit_" & sProcName & ":" & vbCrLf _
- & vbTab & "Exit " & sProcType & vbCrLf & vbCrLf _
- & "Err_" & sProcName & ":" & vbCrLf _
- & vbTab & "MsgBox Err.Description" & vbCrLf _
- & vbTab & "LogError """ & sProcName _
- & ", "" & Err.Number & "", "" & Err.Description" & vbCrLf _
- & vbTab & "Err.Clear" & vbCrLf _
- & vbTab & "Resume Exit_" & sProcName & vbCrLf
- oCode.InsertLines lProcEndLine - 1, sErrCode
- oCode.InsertLines lProcStartLine + 1, sErrLine
- End If
- End If
- End If
- Next
- ap.Quit
- Set ap = Nothing
- End Sub
- Function GetProcs(oVBE, ProcArray)
- ''A list of procedures
- Dim oCode As VBIDE.CodeModule
- Dim aModLines As Variant
- Dim sModText As String
- Dim sProcName As String
- Dim lLineCount As Long
- Dim lCodeObjectCounter As Long
- Dim pa As Long
- Dim lLine As Long
- Dim pk As vbext_ProcKind
- pa = -1 ''First run
- ''Yep, 1, not zero
- For lCodeObjectCounter = 1 To oVBE.Count
- Set oCode = oVBE(lCodeObjectCounter).CodeModule
- lLineCount = oCode.CountOfLines
- lLine = 1
- Do While lLine < lLineCount
- sProcName = oCode.ProcOfLine(lLine, pk)
- If sProcName <> "" Then
- ' Found a procedure. Get its details, and then skip
- ' to the end of the procedure.
- If pa = -1 Then
- pa = 0
- ReDim Preserve ProcArray(1, 0)
- Else
- pa = pa + 1
- ReDim Preserve ProcArray(1, pa)
- End If
- ProcArray(0, pa) = oCode.Name
- ''Proc name
- ProcArray(1, pa) = oCode.ProcOfLine(lLine, pk)
- lLine = lLine + oCode.ProcCountLines(sProcName, pk)
- Else
- ' This line has no procedure, so go to the next line.
- lLine = lLine + 1
- End If
- Loop
- 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.