CBlog(handziuk)

Bradley Handziuk's blog

Things to do when Access starts up

Even if you compiled your Access app it probably doesn't start up exactly how you want it to look. i.e. users might have minimized the ribbon but you want it the other way around or usually (for me) you want the navigation pane hidden. There are two ways of automatically executing code when your app starts
  1. AutoExec macro (a macro literally named AutoExec which runs as the first thing that happens when your app starts)
  2. Default form

Things get funky and they step on each other when you have both of these set.

You can use the AutoExec macro by creating anew macro and naming it AutoExec. Then paste this code in there. It is just the RunCode macro which calls a function (it has to be a function not a sub. A sub will not work) from some static module (not a class module).

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<UserInterfaceMacros xmlns="http://schemas.microsoft.com/office/accessservices/2009/11/application">
	<UserInterfaceMacro MinimumClientDesignVersion="14.0.0000.0000">
		<Statements>
			<Action Name="RunCode">
				<Argument Name="FunctionName">
DoThisOnStartup()
				</Argument>
			</Action>
		</Statements>
	</UserInterfaceMacro>
</UserInterfaceMacros>

This is the code I typically run on startup. You can substitute ACCDE for ADE or MDE (whatever you are using). Note that you cannot just use the Applicaiton.IsCompiled property because that just indicates whether or not the code is in a temporarily compiled state, not the saved file type.

Public Function DoThisOnStartup()
    Dim fso As New FileSystemObject
    Dim ErrorValue As String
    'only do this stuff if the file is compiled
    On Error GoTo DoThisOnStartup_Error

    If fso.GetExtensionName(CurrentProject.FullName) = "ACCDE" Then
    
        'MsgBox CurrentProject.FullName
        
        'hide the navigation pane
        ErrorValue = "DoCmd.SelectObject acForm, , True"
        DoCmd.SelectObject acForm, , True

        On Error GoTo DoThisOnStartup_Error
        DoCmd.NavigateTo "acNavigationCategoryObjectType": DoCmd.RunCommand acCmdWindowHide
        
        DoCmd.NavigateTo "acNavigationCategoryObjectType" ', "Forms"
        
        'incase they unhide it
        ErrorValue = "DoCmd.LockNavigationPane True"
        DoCmd.LockNavigationPane True

       ' CommandBars.ExecuteMso "MinimizeRibbon"  ' another, less robust way to hide the ribbon
        If Application.CommandBars("Ribbon").Height >= 150 Then
            SendKeys "^{F1}"
        End If

    End If

    VeryifyBackendIsInPlace

Exit_DoThisOnStartup:
    DoCmd.OpenForm "Dashboard"
   On Error GoTo 0
   Exit Function

DoThisOnStartup_Error:
    ' logging if you can
       Resume Next  ' continue trying to start up
    
End Function

Loading