Option Compare Database
' Verify Access' external references.
' Returns True if all references are valid.
' Will run quietly, if called with parameter Quiet as True.
' Is intended to be called as the first command from the AutoExec macro
' followed by a call to a function - for example CompileAndSave - that will
' "compile and save all modules".
' 2018-07-10. Cactus Data ApS, CPH.
Public Function VerifyReferences( _
Optional ByVal Quiet As Boolean) _
' Settings for message box.
Const Title As String = "Missing Support Files"
Const Header As String = "One or more supporting files are missing:"
Const Footer As String = "Report this to IT support." & vbCrLf & "Program execution cannot continue."
Const Buttons As Long = VbMsgBoxStyle.vbCritical + VbMsgBoxStyle.vbOKOnly
Dim Reference As Access.Reference
Dim Item As Integer
Dim Guid As String
Dim Major As Long
Dim Minor As Long
Dim Prompt As String
Dim Broken As Boolean
Dim SecondRun As Boolean
Dim Result As Boolean
' Loop (a second time) the references and build a list of those broken.
Broken = False
Prompt = ""
For Each Reference In Access.References
If Reference.BuiltIn Then
' Nothing to check.
ElseIf IsBrokenExt(Reference) Then
Broken = True
Prompt = Prompt & Reference.Guid & " - " & Reference.Name & vbCrLf
If SecondRun Then
' Only shuffle the references once.
ElseIf Not Broken Then
' All references have been verified.
' Try to remove the last non-broken reference and add it back.
' This will shuffle the Reference collection and may or may not
' cause a broken reference to be added back.
Item = Access.References.Count
Set Reference = Access.References.Item(Item)
If Not Reference.BuiltIn Then
If Not IsBrokenExt(Reference) Then
' Record the reference's identification before removal.
Guid = Reference.Guid
Major = Reference.Major
Minor = Reference.Minor
' Remove this reference.
' Add back the removed reference.
Access.References.AddFromGuid Guid, Major, Minor
Item = Item - 1
' Exit loop when a built-in reference is met.
' These are always the top ones.
Loop Until Reference.BuiltIn
SecondRun = True
Loop Until Not Broken
Result = Not Broken
If Result = False And Quiet = False Then
Prompt = Header & vbCrLf & vbCrLf & Prompt & vbCrLf & Footer
VBA.MsgBox Prompt, Buttons, Title
Set Reference = Nothing
VerifyReferences = Result
' Performs an extended check if a reference is broken, as the
' IsBroken property doesn't check for unregistered files. Thus,
' an unregistered reference may fail even if not marked MISSING.
' 2018-07-09. Gustav Brock. Cactus Data ApS.
Public Function IsBrokenExt( _
ByRef Reference As Access.Reference) _
Dim NotBroken As Boolean
On Error GoTo Err_IsBrokenExt
' If the reference is not registered, calling property FullPath will fail.
' Even if the file exists in the Virtual File System, GetAttr will find it.
If (VBA.GetAttr(Reference.FullPath) And vbDirectory) <> vbDirectory Then
' FullPath is valid.
NotBroken = Not Reference.IsBroken
IsBrokenExt = Not NotBroken
' Ignore non-existing servers, drives, and paths.
Select allOpen in new window
' Compile and save all (other) modules.
' Is intended to be called from the AutoExec macro after having verified the references.
' After tampering with the references, the application may appear to be compiled, which it is not.
' This function will not fail - even if the application is compiled or appears to be - if it is
' kept on it own in a separate module.
' It is not a subfunction as only functions can be called from the AutoExec macro.
' 2018-07-06. Cactus Data ApS, CPH.
Public Function CompileAndSave()
' The command:
' Application.RunCommand acCmdCompileAndSaveAllModules
' can not be used, as no module is open when the AutoExec macro runs.
' Thus, use this undocumented SysCmd() call.
Call SysCmd(504, 16483)
- VBA Extensibility. I don't have any idea what this does and therefore the potential impact of removing it.
- ADO Recordset Library. Mostly I am opening / closing Databases / Recordsets. However I do have code that does relinking.
- Early Binding. I use Late Binding to import Excel Files as I have worked with EE Experts and determine that this is my best option.
What are .bas files?
© 1996-2023 Experts Exchange, LLC. All rights reserved. Covered by US Patent