Skip to content

Commit

Permalink
Advanced interface for external procedure
Browse files Browse the repository at this point in the history
  • Loading branch information
josef-poetzl committed Mar 20, 2024
1 parent 7921149 commit fc902e4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 19 deletions.
41 changes: 28 additions & 13 deletions Version Control.accda.src/modules/clsLog.cls
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,29 @@ Public Sub Error(eLevel As eErrorLevel, strDescription As String, Optional strSo

Dim strPrefix As String
Dim strDisplay As String
Dim bolLogToFile As Boolean
Dim bolDisplay As Boolean
Dim MsgBoxStyle As VbMsgBoxStyle
Dim MsgBoxPrefix As String

bolDisplay = (eLevel > eelWarning)

Select Case eLevel
Case eelWarning: strPrefix = "WARNING: "
Case eelError: strPrefix = "ERROR: "
Case eelCritical: strPrefix = "CRITICAL: "
Case eErrorLevel.eelError
strPrefix = "ERROR: "
MsgBoxPrefix = "Error"
MsgBoxStyle = vbExclamation
Case eErrorLevel.eelWarning, eErrorLevel.eelAlert
strPrefix = "WARNING: "
MsgBoxPrefix = "Warning"
MsgBoxStyle = vbInformation
Case eErrorLevel.eelCritical
strPrefix = "CRITICAL: "
MsgBoxPrefix = "Critical"
MsgBoxStyle = vbCritical
Case Else
strPrefix = "INFO: "
MsgBoxStyle = vbInformation
End Select

' Build the error message string.
Expand All @@ -189,15 +207,15 @@ Public Sub Error(eLevel As eErrorLevel, strDescription As String, Optional strSo
End If

' Log all errors, and display on the output screen anything higher than a warning
Me.Add vbNullString, (eLevel > eelWarning)
Me.Spacer (eLevel > eelWarning)
Me.Add strDisplay, (eLevel > eelWarning), , "red"
Me.Add vbNullString, bolDisplay
Me.Spacer bolDisplay
Me.Add strDisplay, bolDisplay, , "red"

' Log the additional error and source details to the log file
If Err Then .Add "Error ", Err.Number, ": ", Err.Description, " "
If strSource <> vbNullString Then .Add "Source: ", strSource
Me.Add .GetStr, False
Me.Spacer (eLevel > eelWarning)
Me.Spacer bolDisplay

' See if we are actively logging an operation
If Log.Active Then
Expand All @@ -208,12 +226,9 @@ Public Sub Error(eLevel As eErrorLevel, strDescription As String, Optional strSo
End If
Else
' Show message on any error level when we are not logging to a file.
Select Case eLevel
Case eelNoError: ' Do nothing
Case eelWarning: MsgBox2 "Warning", strDisplay, .GetStr, vbInformation
Case eelError: MsgBox2 "Error", strDisplay, .GetStr, vbExclamation
Case eelCritical: MsgBox2 "Critical", strDisplay, .GetStr, vbCritical
End Select
If eLevel > 0 Then
MsgBox2 MsgBoxPrefix, strDisplay, .GetStr, MsgBoxStyle
End If
End If
End With

Expand Down
9 changes: 5 additions & 4 deletions Version Control.accda.src/modules/modConstants.bas
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,11 @@ End Enum
' Error levels used for logging and monitoring the status
' of the current operation.
Public Enum eErrorLevel
eelNoError
eelWarning ' Logged to file
eelError ' Displayed and logged
eelCritical ' Cancel operation
eelNoError = 0
eelWarning = 1 ' Logged to file
eelAlert = 2 ' Displayed and , show Warning not Error
eelError = 3 ' Displayed and logged
eelCritical = 4 ' Cancel operation
End Enum

' Compare mode for cloning dictionary object
Expand Down
68 changes: 66 additions & 2 deletions Version Control.accda.src/modules/modImportExport.bas
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ End Sub
Private Function TryRunAddInProcedure(ByVal ProcedureName As String) As Boolean

Dim AddInFile As String
Dim ExternalReturnValue As Variant

If DebugMode(True) Then On Error GoTo 0 Else On Error GoTo ErrHandler
If DebugMode(True) Then On Error GoTo 0 Else On Error GoTo ErrHandler

ProcedureName = Replace(ProcedureName, "%appdata%", Environ("appdata"))

Expand All @@ -306,8 +307,36 @@ Private Function TryRunAddInProcedure(ByVal ProcedureName As String) As Boolean

TryRunAddInProcedure = True

Application.Run ProcedureName
' What could a generally usable interface look like?
'
' * Public Function ProcedureNameInAddIn(ByRef ReturnMessage As String) as Boolean
' * Public Function ProcedureNameInAddIn(ByRef ReturnMessage As String) as Long ' ... = eErrorLevel .. -1 for all ok?
' * Public Function ProcedureNameInAddIn() as String ... Returns:
' "Error: ErrorMessage" => Error log
' or "Warning: Warning Message" => displayed Warning log
' or vbNullstring ... show nothing, all success
'
'
'I decided to test this variant(s):
'
ExternalReturnValue = Application.Run(ProcedureName)

If VarType(ExternalReturnValue) = vbString Then
LogErrorMessage ExternalReturnValue, GetProcedureNameFromPath(ProcedureName)
ElseIf VarType(ExternalReturnValue) = vbBoolean Then
If Not ExternalReturnValue Then ' Cancel export
Log.Error eelCritical, GetProcedureNameFromPath(ProcedureName) & " failed", "modImportExport.TryRunAddInProcedure"
End If
End If

' This code allows:
' * Public Function ProcedureNameInAddIn() as String
' optional with eErrorLevel code like "Error: Error description" or "Warning: warining message to show"
' * Public Function ProcedureNameInAddIn() as Boolean
' => if false then Error (eelCritical)
'
' Note: I added Alert to eErrorLevel .. show this Alert/Warining in Dialog, bot don't stop export
'
ExitHere:
Exit Function

Expand All @@ -317,6 +346,41 @@ ErrHandler:

End Function

Private Sub LogErrorMessage(ByVal ErrorMessage As String, ByVal ErrorMessageSource As String)

Dim ErrorLevel As eErrorLevel
Dim ErrorLevelEndPos As Long

ErrorLevelEndPos = InStr(1, ErrorMessage, ":")
If ErrorLevelEndPos > 1 Then
Select Case Trim(Left(ErrorMessage, ErrorLevelEndPos - 1))
Case "Error"
ErrorLevel = eelError
Case "Warning", "Alert"
ErrorLevel = eelAlert
Case "Critical", "FATAL"
ErrorLevel = eelCritical
Case Else
ErrorLevel = eelAlert
ErrorLevelEndPos = 0 ' don't remove String before ":"
End Select
If ErrorLevelEndPos > 0 Then
ErrorMessage = Trim(Mid(ErrorMessage, ErrorLevelEndPos + 1))
End If
Else
ErrorLevel = eelAlert
End If

Log.Error ErrorLevel, ErrorMessage, ErrorMessageSource

End Sub

Private Function GetProcedureNameFromPath(ByVal FullProcedureName As String) As String
GetProcedureNameFromPath = Mid(FullProcedureName, InStrRev(FullProcedureName, "\") + 1)
End Function



'---------------------------------------------------------------------------------------
' Procedure : ExportSingleObject
' Author : Adam Waller
Expand Down

0 comments on commit fc902e4

Please sign in to comment.