How to: Open forms from the navigation bar
Use a WklNavBar on a separate navigation form (a normal form window or a pop-up) that opens the
application's forms in their own windows, instead of swapping the page in a subform.
This page shows the code-based approach for a bar with designed entries. A bar bound to a navigation table opens form targets and follows the active form by itself, without the code below; see How to: Fill the navigation bar from a table.
Requires WKL Forms Object Library 1.24 or later.
Solution overview
The bar doesn't know the forms it leads to: Navigate passes the key of the chosen entry, and the
application opens the form with DoCmd.OpenForm (which also activates a form that's already
open). Three parts of the application keep the bar and the windows in sync:
- The selection follows the active form. Each target form sets the bar's
SelectedKeyin itsForm_Activateevent. SettingSelectedKeyin code doesn't raise an event, so this can't start a navigation, however the user reached the form (the bar, Ctrl+F6, the window menu, or a click on the window). - The navigation form might be closed. A target form checks that the navigation form is open before it touches the bar; without the check,
Forms("frmNavigation")fails with error 2450. - Unsaved changes are checked in the form of the selected entry.
BeforeNavigatechecksDirtyon the form whose keySelectedKeyholds (the target the user worked in last), not onScreen.ActiveForm: on a pop-up navigation form, the active form is the navigation form itself.
Implementation
In the module of the navigation form frmNavigation (the entry keys are the form names):
Private Sub navMain_BeforeNavigate(ByVal Key As String, Cancel As Integer)
Dim current As String
current = Me!navMain.Object.SelectedKey
If Len(current) = 0 Or current = Key Then Exit Sub
If Not CurrentProject.AllForms(current).IsLoaded Then Exit Sub
If Forms(current).Dirty Then
MsgBox "Please save or discard the changes in """ & Forms(current).Caption & """ first.", vbExclamation
DoCmd.SelectObject acForm, current
Cancel = True
End If
End Sub
Private Sub navMain_Navigate(ByVal Key As String)
DoCmd.OpenForm Key
End Sub
In the module of each target form (frmOrders, frmCustomers, and so on):
Private Sub Form_Activate()
' The bar marks this form's entry; code raises no BeforeNavigate or Navigate.
If CurrentProject.AllForms("frmNavigation").IsLoaded Then
Forms!frmNavigation!navMain.Object.SelectedKey = Me.Name
End If
End Sub
Remarks
- Tabbed documents and overlapping windows, in ACCDB and ACCDE files and in the Access Runtime, behave the same way for both a normal and a pop-up navigation form:
- choosing an entry opens the target, and choosing an open target again activates it;
- activating a target in code or with Ctrl+F6 moves the selection without
BeforeNavigateorNavigate; - a target with unsaved changes cancels the choice, and the selection stays where it is;
- the bar works with a maximized target and with the navigation form minimized;
- after the navigation form has been closed, activating a target raises no error with the
IsLoadedcheck, and error 2450 without it.
- Ctrl+F6 can activate the navigation form itself. A normal navigation form takes part in the Access window cycle like any other form; the selection then stays on the last target, which is the form the user returns to. A pop-up form isn't part of the cycle.
- The shortcut belongs to the bar's form.
ToggleShortcut(Ctrl+B) works while the navigation form has the focus, but not in a target form, where the key stays with Access. Offer the toggle button, or callTogglefrom your own key in the target forms. - When unpinned, the bar opens over its own form. The expanded overlay lies over the navigation form's window, even while a target form is active; it isn't a window over the targets.
- Where the focus stays after a cancel. On a normal navigation form, the target keeps the focus; on a pop-up form, the navigation form has it.
DoCmd.SelectObjectin the sample brings the form with the changes to the front in both cases. - Why this is application code for a designed bar. Only the application knows which form a key opens, with which
OpenArgs, filter, or window mode, and what counts as unsaved (a dirty record, an open dialog box, a running import). The bar's part is to let code follow the windows silently and to let a choice be canceled.
Related topics
- How to: Fill the navigation bar from a table: form targets without code
- How to: Stay on a page with unsaved changes: the same check for a page subform