
Creating custom Navigation Buttons
There are several reasons why you might wish to replace the standard Access Navigation Buttons with ones of your own:
- To fit in with other controls on your form;
- To be enabled/disabled individually according to the record being shown.
Start by creating five Command Buttons in the footer of the form, called cmdFirst, cmdPrevious, cmdNext, cmdLast and cmdAdd, and add the required code to navigate between records using DoCmd.GotoRecord. As it is, this code will raise an error if we try to move previous to the first record. Although we could just trap the error, it looks better to enable/disable the Command Buttons as necessary.
Therefore we need some way of checking firstly if this is a new record, when we will be at the end of the records, so we should only be able to move to the last record, the first record, or move to the previous record. We can do this by checking the Form's NewRecord property. If this isn't the case, then we wish to know whether the currently displayed record is either the first record, which will affect whether we can move before that record, or the last record, which affects whether we can move after that record. We can do this by using the EOF and BOF properties of the Form's RecordsetClone. Below is the code that I would suggest for all of this, which will be run in the Form's OnCurrent event:
Private Sub Form_Current()
Dim rsClone As Recordset
Set rsClone = Me.RecordsetClone
If Me.NewRecord = True Then
Me!cmdAdd.Enabled = False
Me!cmdNext.Enabled = False
Me!cmdPrevious.Enabled = True
Me!cmdFirst.Enabled = True
Me!cmdLast.Enabled = True
ElseIf rsClone.Bookmarkable = True Then
Me!cmdAdd.Enabled = True
rsClone.Bookmark = Me.Bookmark
rsClone.MovePrevious
cmdFirst.Enabled = Not (rsClone.BOF)
cmdPrevious.Enabled = Not (rsClone.BOF)
rsClone.MoveNext
rsClone.MoveNext
cmdNext.Enabled = Not (rsClone.EOF)
cmdLast.Enabled = Not (rsClone.EOF)
rsClone.MovePrevious
End If
rsClone.Close
End Sub |
Before setting the position of the RecordsetClone to that of the Form, by using the Bookmark property, I check that this is going to work by first checking the Bookmarkable property. For Access tables, there shouldn't be any problems with Bookmarks.
Top
HOME |
NEW |
TABLES |
QUERIES |
FORMS |
REPORTS |
GENERAL |
API |
DOWNLOADS |
TUTORIAL |
RESOURCES
E-MAIL
Copyright & Disclaimer
|