Enable custom navigation buttons

B

BruceM

I have used Stephen Lebans' navigation buttons subform in order to allow the
autorepeat option when navigating the main form records. In brief, the
navigation buttons are on a subform. The Next button has the following
Click event:
DoCmd.GoToRecord acDataForm, Me.Parent.Name, acNext
Other navigation buttons are similarly coded. The navigation buttons
subform includes a sub to enable/disable navigation buttons, depending on
the record position (for instance, the Previous button is disabled at the
first record). The sub is called from the main form's Current event. I
wanted somewhat different enabled/disabled options for the buttons than was
in the code Stephen provided, so I altered the code to achieve that. My
adaptation is like this:

Public Sub EnableDisableButtons()
' Check and see which Buttons should be enabled/disabled
' based on the current row of the recordset.

On Error Resume Next

Dim lngCurrent As Long, lngTotal As Long

lngCurrent = Me.Parent.CurrentRecord

' Update our Nav info
Me.txtPos = lngCurrent

' If lblNumRecords is blank then this is the
' first time through and the recordset pointers
' may not be updated yet. We need to allow time
' for them to get updated.
If Me.lblNumRecords.Caption = "" Then
Me.Parent.RecordsetClone.MoveLast
DoEvents
End If

lngTotal = Me.Parent.RecordsetClone.RecordCount
DoEvents

' Are we on a NEW record
If Me.Parent.NewRecord = True Then
' Update our nav info.
Me.txtPos = lngTotal + 1
Me.lblNumRecords.Caption = "New Record"
Me.txtHidden.SetFocus
Else
Me.txtPos = lngCurrent
Me.lblNumRecords.Caption = " of " & lngTotal
End If

' The button can only be disabled if it doesn't have the focus
If lngCurrent = 1 Or lngCurrent = lngTotal Then Me.txtHidden.SetFocus

Me.cmdFirst.Enabled = Not lngCurrent <= 1
Me.cmdPrevious.Enabled = Me.cmdFirst.Enabled
Me.cmdNext.Enabled = (lngCurrent = 1 And lngTotal > 1) _
Or lngCurrent < lngTotal
Me.cmdLast.Enabled = Me.cmdNext.Enabled

End Sub

txtPos and lblNumRecords show the current and total records. txtHidden
satisifies the practical need to set the focus off of a control, but does
nothing else.

The code to call it from the main form's Current event is:
Call Me.fsubNavButtons.Form.EnableDisableButtons

The trouble is that Me.lblNumRecords.Caption = ""
never evaluates to True, so MoveLast never happens, so lngTotal is never
more than 1 when I open the form. I cannot find a way to reset the caption
to "" when closing (or whatever). With the caption having a value other
than "", MoveLast never happens. If I put MoveLast outside of that If
statement the buttons are enabled as intended, but I don't know if there
will be a performance hit from using MoveLast each time through the code.
 
C

Carl Rapson

BruceM said:
I have used Stephen Lebans' navigation buttons subform in order to allow
the autorepeat option when navigating the main form records. In brief, the
navigation buttons are on a subform. The Next button has the following
Click event:
DoCmd.GoToRecord acDataForm, Me.Parent.Name, acNext
Other navigation buttons are similarly coded. The navigation buttons
subform includes a sub to enable/disable navigation buttons, depending on
the record position (for instance, the Previous button is disabled at the
first record). The sub is called from the main form's Current event. I
wanted somewhat different enabled/disabled options for the buttons than
was in the code Stephen provided, so I altered the code to achieve that.
My adaptation is like this:

Public Sub EnableDisableButtons()
' Check and see which Buttons should be enabled/disabled
' based on the current row of the recordset.

On Error Resume Next

Dim lngCurrent As Long, lngTotal As Long

lngCurrent = Me.Parent.CurrentRecord

' Update our Nav info
Me.txtPos = lngCurrent

' If lblNumRecords is blank then this is the
' first time through and the recordset pointers
' may not be updated yet. We need to allow time
' for them to get updated.
If Me.lblNumRecords.Caption = "" Then
Me.Parent.RecordsetClone.MoveLast
DoEvents
End If

lngTotal = Me.Parent.RecordsetClone.RecordCount
DoEvents

' Are we on a NEW record
If Me.Parent.NewRecord = True Then
' Update our nav info.
Me.txtPos = lngTotal + 1
Me.lblNumRecords.Caption = "New Record"
Me.txtHidden.SetFocus
Else
Me.txtPos = lngCurrent
Me.lblNumRecords.Caption = " of " & lngTotal
End If

' The button can only be disabled if it doesn't have the focus
If lngCurrent = 1 Or lngCurrent = lngTotal Then Me.txtHidden.SetFocus

Me.cmdFirst.Enabled = Not lngCurrent <= 1
Me.cmdPrevious.Enabled = Me.cmdFirst.Enabled
Me.cmdNext.Enabled = (lngCurrent = 1 And lngTotal > 1) _
Or lngCurrent < lngTotal
Me.cmdLast.Enabled = Me.cmdNext.Enabled

End Sub

txtPos and lblNumRecords show the current and total records. txtHidden
satisifies the practical need to set the focus off of a control, but does
nothing else.

The code to call it from the main form's Current event is:
Call Me.fsubNavButtons.Form.EnableDisableButtons

The trouble is that Me.lblNumRecords.Caption = ""
never evaluates to True, so MoveLast never happens, so lngTotal is never
more than 1 when I open the form. I cannot find a way to reset the
caption to "" when closing (or whatever). With the caption having a value
other than "", MoveLast never happens. If I put MoveLast outside of that
If statement the buttons are enabled as intended, but I don't know if
there will be a performance hit from using MoveLast each time through the
code.

Maybe Me.lblNumRecords.Caption is Null instead of an empty string (""). Try
this and see if it makes a difference:

If IsNull(Me.lblNumRecords.Caption) Then ...


Carl Rapson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top