Everyone knows the default form navigation buttons
but what if we wanted to create our own controls?
In reality, it is very simple to create our own navigation buttons, so let see how it’s done!
Why Create Our Own Navigation System?
There are a possible number of reasons why you might wish to replace the default navigation buttons:
- Perhaps you don’t like the look/feel
- Perhaps you don’t like their location
- Perhaps you want to translate the content
- Perhaps you want to implement some security to the feature
- etc…
It doesn’t truly matter why, just that you wish to and that it is possible to do so.
Here are a few samples of what could be achieved:



and these are but a few examples, you can truly customize things as you please, use any text, images, … you wish and make it your own.
Custom Navigation Buttons
So let us start off by examining how we can create navigation button and code everything directly in the form module.
Now, as I said earlier, you can design things as you see fit, but the approach I took was to create a series of command buttons, a text box (for the counter) and place them within the borders of an option group. I found the option group framed the controls nicely and offered me the opportunity to add a label.
These are the names I gave my controls (so you can follow the code):
- optGrp_RecNav -> Option Group
- lbl_RecNav_Header -> Option Group Label
- cmd_RecNav_New -> New Entry button
- cmd_RecNav_First -> Goto the 1st record button
- cmd_RecNav_Previous -> Goto the previous record button
- cmd_RecNav_Next -> Goto the next record button
- cmd_RecNav_Last -> Goto the last record button
- txt_RecNav_Counter -> Text box used to display the position relative to the total number of records
Control Events
The first thing to do next is create the necessary Click events for each of the Command Buttons:
Private Sub cmd_RecNav_New_Click()
RunCommand acCmdRecordsGoToNew
End Sub
Private Sub cmd_RecNav_Previous_Click()
RunCommand acCmdRecordsGoToPrevious
End Sub
Private Sub cmd_RecNav_Next_Click()
RunCommand acCmdRecordsGoToNext
End Sub
Private Sub cmd_RecNav_Last_Click()
RunCommand acCmdRecordsGoToLast
End Sub
Private Sub cmd_RecNav_First_Click()
RunCommand acCmdRecordsGoToFirst
End Sub
As you can see, the code is very simple as we simply exploit the various RunCommand commands!
Form Events
Next we need to setup a couple form events to setup and update the control’s as we navigate through the recordset and adjust also depending on the form properties. Since the events all perform the exact action, I felt it best to create a form procedure and simply call that. Just simplifies coding and makes it a little easier to maintain.
So I created the following form events:
Private Sub Form_AfterUpdate()
Call SetupNav
End Sub
Private Sub Form_Current()
Call SetupNav
End Sub
and then created the actual SetupNav() procedure:
Public Sub SetupNav()
Dim rs As DAO.Recordset
Dim lCurrentRecord As Long
Dim lRecordCount As Long
'Get the values for the Text Box
lCurrentRecord = Me.CurrentRecord
Set rs = Me.RecordsetClone
rs.MoveLast
lRecordCount = rs.RecordCount
'Hide all the buttons
cmd_RecNav_New.Enabled = False
cmd_RecNav_First.Enabled = False
cmd_RecNav_Previous.Enabled = False
cmd_RecNav_Next.Enabled = False
cmd_RecNav_Last.Enabled = False
'Show button depending on the case
If lCurrentRecord > lRecordCount Then
'On a new entry
If lRecordCount <> 0 Then
'There are other records available (so not the 1st entry)
cmd_RecNav_First.Enabled = True
cmd_RecNav_Previous.Enabled = True
cmd_RecNav_Last.Enabled = True
End If
ElseIf lCurrentRecord = lRecordCount Then
'On the last available record
cmd_RecNav_New.Enabled = (Me.AllowAdditions And Me.RecordsetType <> 2)
If Me.RecordsetClone.RecordCount > 1 Then
cmd_RecNav_First.Enabled = True
cmd_RecNav_Previous.Enabled = True
End If
ElseIf lCurrentRecord = 1 And lRecordCount > 1 Then
'On the 1st record and there are multiple records for the form
cmd_RecNav_New.Enabled = (Me.AllowAdditions And Me.RecordsetType <> 2)
cmd_RecNav_Next.Enabled = True
cmd_RecNav_Last.Enabled = True
Else
cmd_RecNav_New.Enabled = (Me.AllowAdditions And Me.RecordsetType <> 2)
cmd_RecNav_First.Enabled = True
cmd_RecNav_Previous.Enabled = True
cmd_RecNav_Next.Enabled = True
cmd_RecNav_Last.Enabled = True
End If
'Update record counter control
Me.txt_RecNav_Counter.ControlSource = "=""Record " & lCurrentRecord & " Of " & lRecordCount & """"
Set rs = Nothing
End Sub
With this in place, you should now have a function navigation system that you can customize as you see fit:
- Add images
- Add Tooltips
- Adjust colors, fonts, …
- Adjust positioning
Error Handling
I have tried to keep the code light in this article on purpose, but as always, best practices should apply and proper error handling should be implemented throughout to avoid any potential runtime surprises!
Customizations!
Note, you can customize each button and the option group substantially to get the exact look you are going for.
You can add captions to your command buttons which could be Words: First, Last, … or you could input symbols instead <, >, >>, … or you could instead choose to add picture to them. There are lots of choices available to you to bring your vision to life, but now you have the basics which you can further build upon.
Demo Database
Feel free to download a 100% unlocked copy of the sample database (tested on Win10/Acc365 & Win10/Acc2013). This sample provides multiple variations to illustrate textual button, button using symbols or images. I provide both the standard implementation and the Sub Classing of forms.
Download “Custom Navigation Buttons” Form_SubClassing-Navigation.zip – Downloaded 7704 times – 257.40 KBNotice About Content/Downloads/Demos
Disclaimer/Notes:
If you do not have Microsoft Access, simply download and install the freely available runtime version (this permits running MS Access databases, but not modifying their design):Microsoft Access 2010 Runtime
Microsoft Access 2013 Runtime
Microsoft Access 2016 Runtime
Microsoft 365 Access Runtime
In no event will Devhut.net or CARDA Consultants Inc. be liable to the client/end-user or any third party for any damages, including any lost profits, lost savings or other incidental, consequential or special damages arising out of the operation of or inability to operate the software which CARDA Consultants Inc. has provided, even if CARDA Consultants Inc. has been advised of the possibility of such damages.
What’s Next?
The next logical step at this point, because it is something we are likely to apply in multiple forms, is to use a Class Module for the behavior and use form Sub Classing techniques to greatly simplify our coding and facilitate long term maintenance of the code. If you want to learn more then check out:
Page History
| Date | Summary of Changes |
|---|---|
| 2024-02-28 | Initial Release |
