Function adhEnableButtons(frm As Form)
'
' Attached to the specified form's Current event.
'
' This function enables and disables buttons as
' necessary, depending on which is the current
' record on the form.
'
' This function counts on buttons named cmdFirst,
' cmdPrev, cmdNext, cmdLast and cmdNew. One
' could code around this, but it seemed like
' overkill for this example.
'
' In:
' frm: A reference to the form in question.
' Out:
' Nothing
Dim rst As Recordset
Dim fAtNew As Integer
Dim fUpdatable As Integer
frm!txtCurrRec = frm.CurrentRecord
Set rst = frm.RecordsetClone
' Sooner or later, Access will figure out
' how many rows there really are!
frm!txtTotalRecs = rst.RecordCount + IIf(frm.NewRecord, 1, 0)
' Check to see whether or not you're on the new record.
fAtNew = frm.NewRecord
' If the form isn't updatable, then you sure
' can't go to the new record! If it is, then
' the button should be enabled unless you're already
' on the new record.
fUpdatable = rst.Updatable And frm.AllowAdditions
frm!cmdNew.Enabled = IIf(fUpdatable, Not fAtNew, False)
If fAtNew Then
frm!cmdNext.Enabled = False
frm!cmdLast.Enabled = True
frm!cmdFirst.Enabled = True And (rst.RecordCount > 0)
frm!cmdPrev.Enabled = True And (rst.RecordCount > 0)
Else
' Sync the recordset's bookmark with
' the form's bookmark.
rst.Bookmark = frm.Bookmark
' Move backwards to check for BOF.
rst.MovePrevious
frm!cmdFirst.Enabled = Not rst.BOF
frm!cmdPrev.Enabled = Not rst.BOF
' Get back to where you started.
rst.Bookmark = frm.Bookmark
' Move forward to check for EOF.
rst.MoveNext
frm!cmdNext.Enabled = Not (rst.EOF Or fAtNew)
frm!cmdLast.Enabled = Not (rst.EOF Or fAtNew)
End If
End Function