Skip to content

Compile Check #46

Description

@DecimalTurn

Right after the build, to check its integrity, it would be desirable to perform a "Compile Check" like the one that is performed via the menu option from the VBE.

Access allows to automate this process, but that's not possible in other VBA environements.

Possible approach:
https://stackoverflow.com/a/55613985/5958842


Potentially working approaches:

Access approach (reference):

Function IsProjectValid() As Boolean
    On Error GoTo CompileError
    
    ' Force Access to compile all modules
    DoCmd.RunCommand acCmdCompileAllModules
    
    ' Double check state via native property
    If Application.IsCompiled Then
        IsProjectValid = True
    Else
        IsProjectValid = False
    End If
    Exit Function

CompileError:
    ' If compilation fails, an error is generated and caught here
    IsProjectValid = False
End Function

Other VBA environements:

Function PerformCompileCheck() As Boolean
    Dim vbeCmdBar As Object
    Dim compileBtn As Object
    
    On Error GoTo ErrorHandler

    ' 1. Get access to the VBE interface
    Set vbeCmdBar = Application.VBE.CommandBars
    Set compileBtn = vbeCmdBar.FindControl(Type:=1, ID:=578) ' ID 578 = Debug -> Compile
    
    If compileBtn Is Nothing Then
        MsgBox "Cannot find Compile control. Ensure VBE window is accessible.", vbCritical
        PerformCompileCheck = False
        Exit Function
    End If
    
    ' 2. Check if the project is already compiled.
    ' If it is already greyed out (.Enabled = False), we must "dirty" the project 
    ' to ensure a new compilation test actually runs.
    If Not compileBtn.Enabled Then
        ' Add and immediately remove a temporary module to clear compiled state
        With Application.VBE.ActiveVBProject.VBComponents
            .Add(1).Name = "Temp_Compile_Check_Module" ' 1 = vbext_ct_StdModule
            .Remove .Item("Temp_Compile_Check_Module")
         With
    End If
    
    ' 3. Trigger the compilation
    On Error Resume Next
    compileBtn.Execute
    DoEvents ' Allow VBE processing time
    On Error GoTo ErrorHandler
    
    ' 4. Evaluate the button status
    ' If compilation succeeded, the button will be disabled (greyed out)
    If Not compileBtn.Enabled Then
        PerformCompileCheck = True
    Else
        PerformCompileCheck = False
    End If
    
    Exit Function

ErrorHandler:
    PerformCompileCheck = False
End Function

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions