Hide the Next Button

  • Thread starter Thread starter nerb61 via AccessMonster.com
  • Start date Start date
N

nerb61 via AccessMonster.com

How can I hide the Next and Previous buttons when there are no Next and
Previous records?
 
Which Next and Previous buttons, the built-in ones or ones that you've
created? The built-in ones will gray out if there is no next or previous
record, but you can't hide them individually. You can hide the built-in
navigation buttons but it hides all of them.
 
I'm still a newbie but maybe this is what you mean:

Open the form in design mode and select the properties of it. Then select No
by the Navigation Buttons.
 
As Wayne pointed out, you can't do that with the built in nav buttons. If
you are using custom nav buttons, it is fairly easy to do. Here is procedure
that will do that. It assumes you have four command butttons with the
following names:
cmdLastRec - Positions on the last record
cmdNextRec - Positions on the next record
cmdFirstRec - Positions on the first record
cmdPreviousRec - Positions on the previous record

You then call this routine from the click event of each of the buttons like
this:
SetNavButtons(Me)

Sub SetNavButtons(SomeForm As Form)

On Error GoTo SetNavButtons_Error

With SomeForm
If .CurrentRecord = 1 Then
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
.cmdNextRec.SetFocus
.cmdFirstRec.Enabled = False
.cmdPreviousRec.Enabled = False
ElseIf .CurrentRecord = .Recordset.RecordCount Then
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdPreviousRec.SetFocus
.cmdNextRec.Enabled = False
.cmdLastRec.Enabled = False
Else
.cmdFirstRec.Enabled = True
.cmdPreviousRec.Enabled = True
.cmdNextRec.Enabled = True
.cmdLastRec.Enabled = True
End If
End With

SetNavButtons_Exit:

On Error Resume Next
Exit Sub

SetNavButtons_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure SetNavButtons of Module modFormOperations"
GoTo SetNavButtons_Exit

End Sub
 
I tried the code and get a Compile error: Expected End Sub after the
SetNaveButtons (Me)
 
That line of code does not go with the rest.
I guess my instructions were incomplete. Put that line of code in all four
buttons. First, Previous, Next, and Last.

Put the rest of the code in a standard module. That way, you can use it on
any form. that is why you pass it the form.

Then, reread my earlier post on how the buttons should be named.

Now, here is the code for all the buttons:

Private Sub cmdFirstRec_Click()
On Error GoTo cmdFirstRec_Click_Error

On Error GoTo Err_cmdFirstRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acFirst
Call SetNavButtons(Me)
End If

Exit_cmdFirstRec_Click:
Exit Sub

Err_cmdFirstRec_Click:
MsgBox Err.Description
Resume Exit_cmdFirstRec_Click

cmdFirstRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdFirstRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdFirstRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdFirstRec_Click_Exit

End Sub

Private Sub cmdPreviousRec_Click()
On Error GoTo cmdPreviousRec_Click_Error

On Error GoTo Err_cmdPreviousRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acPrevious
Call SetNavButtons(Me)
End If
Exit_cmdPreviousRec_Click:
Exit Sub

Err_cmdPreviousRec_Click:
MsgBox Err.Description
Resume Exit_cmdPreviousRec_Click

cmdPreviousRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdPreviousRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdPreviousRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdPreviousRec_Click_Exit

End Sub

Private Sub cmdNextRec_Click()

On Error GoTo cmdNextRec_Click_Error

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acNext
Call SetNavButtons(Me)
End If

cmdNextRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdNextRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdNextRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdNextRec_Click_Exit

End Sub

Private Sub cmdLastRec_Click()
On Error GoTo cmdLastRec_Click_Error

On Error GoTo Err_cmdLastRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acLast
Call SetNavButtons(Me)
End If

Exit_cmdLastRec_Click:
Exit Sub

Err_cmdLastRec_Click:
MsgBox Err.Description
Resume Exit_cmdLastRec_Click

cmdLastRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdLastRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdLastRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdLastRec_Click_Exit

End Sub
 
And if there is only 1 record the buttons are not quite right?

I did it slightly different because filtered queries were not giving me
the proper counts all of the time
and also in this particular app they were NOT allowed to create NEW
records in this query.

==============================
at the top of the module I have...............................

Option Compare Database

Public saveCountAM As Integer
=============================
On form load.......................................


Private Sub Form_Load()

saveCountAM = DCount("[AM]", "Query - Summary for AM") '
count number of records in this particular filtered query

End Sub

============================
in the onCurrent event I have .........................................


Private Sub Form_Current()

If Me.Form.CurrentRecord = 1 Then
Me.backbutton.Visible = False
Else
Me.backbutton.Visible = True
End If

If Me.Form.CurrentRecord = saveCountAM Then
Me.forwardbutton.Visible = False
Else
Me.forwardbutton.Visible = True
End If

End Sub

============================================
I did not worry about the first and last record button since they will
not cause a problem if they are pressed and you are already there.

It may not be the classiest, but it is simple.

Ron
 
Thanks for pointing out the 1 record problem. That leads us to a 0 record
problem. Guess I need to address that. This a good case for why I do it in
one place in my app.
As to the filtered query thing. Are you saying that in a filtered query the
record count was not always accurate, or that you could not depend on
CurrentRecord being 1? Right now, I am not doing anything with filtered
queries, but that doesn't mean I will not in the future.
 
What was happening was that I was receiving a recordcount that was for
the entire query rather than just the filtered version.

Since I was putting my own buttons on the form I wanted to put my own
"n of nn" on the form also. And for whatever reason the recordcount I
was receiving was actually for the larger query and not the filtered
down one.

It was probably something I was doing but the approach I stumbled on
seemed to work fine and none of the queries in this app are dealing
with large volumes so I stuck with this rather than debugging why the
other was giving me what it did.

Ron
 
Thanks for your help. For the most part the code works fine. But I still get
a "Can't go to specified record sometimes" Doesn't seem to be any consistency
as to when it happens. I've also gotten an error 2110 ( App can't ove the
focus to the control cmdNextRec) in procedure SetNavButtons of Module
modFormOperations. Not sure what's happening. Any ideas?

Sub SetNavButtons(SomeForm As Form)

On Error GoTo SetNavButtons_Error

With SomeForm
If .CurrentRecord = 1 Then
.cmdNextRec.SetFocus
.cmdPreviousRec.Enabled = False
ElseIf .CurrentRecord = .Recordset.RecordCount Then
.cmdPreviousRec.Enabled = True
.cmdPreviousRec.SetFocus
.cmdNextRec.Enabled = False

Else
.cmdPreviousRec.Enabled = True
.cmdNextRec.Enabled = True

End If
End With

SetNavButtons_Exit:

On Error Resume Next
Exit Sub

SetNavButtons_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure SetNavButtons of Module modFormOperations"
GoTo SetNavButtons_Exit

End Sub
Private Sub Command14_Click()
On Error GoTo Err_Command14_Click

Screen.PreviousControl.SetFocus
DoCmd.DoMenuItem acFormBar, acEditMenu, 10, , acMenuVer70

Exit_Command14_Click:
Exit Sub

Err_Command14_Click:
MsgBox Err.Description
Resume Exit_Command14_Click

End Sub

Private Sub cmdNextRec_Click()

On Error GoTo cmdNextRec_Click_Error

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acNext
Call SetNavButtons(Me)
End If

cmdNextRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdNextRec_Click_Error:

MsgBox "Error "
GoTo cmdNextRec_Click_Exit

End Sub
Private Sub cmdPreviousRec_Click()

On Error GoTo cmdPreviousRec_Click_Error

On Error GoTo Err_cmdPreviousRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acPrevious
Call SetNavButtons(Me)
End If
Exit_cmdPreviousRec_Click:
Exit Sub

Err_cmdPreviousRec_Click:
MsgBox Err.Description
Resume Exit_cmdPreviousRec_Click

cmdPreviousRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdPreviousRec_Click_Error:

MsgBox "Error "
GoTo cmdPreviousRec_Click_Exit

End Sub





That line of code does not go with the rest.
I guess my instructions were incomplete. Put that line of code in all four
buttons. First, Previous, Next, and Last.

Put the rest of the code in a standard module. That way, you can use it on
any form. that is why you pass it the form.

Then, reread my earlier post on how the buttons should be named.

Now, here is the code for all the buttons:

Private Sub cmdFirstRec_Click()
On Error GoTo cmdFirstRec_Click_Error

On Error GoTo Err_cmdFirstRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acFirst
Call SetNavButtons(Me)
End If

Exit_cmdFirstRec_Click:
Exit Sub

Err_cmdFirstRec_Click:
MsgBox Err.Description
Resume Exit_cmdFirstRec_Click

cmdFirstRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdFirstRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdFirstRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdFirstRec_Click_Exit

End Sub

Private Sub cmdPreviousRec_Click()
On Error GoTo cmdPreviousRec_Click_Error

On Error GoTo Err_cmdPreviousRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acPrevious
Call SetNavButtons(Me)
End If
Exit_cmdPreviousRec_Click:
Exit Sub

Err_cmdPreviousRec_Click:
MsgBox Err.Description
Resume Exit_cmdPreviousRec_Click

cmdPreviousRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdPreviousRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdPreviousRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdPreviousRec_Click_Exit

End Sub

Private Sub cmdNextRec_Click()

On Error GoTo cmdNextRec_Click_Error

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acNext
Call SetNavButtons(Me)
End If

cmdNextRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdNextRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdNextRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdNextRec_Click_Exit

End Sub

Private Sub cmdLastRec_Click()
On Error GoTo cmdLastRec_Click_Error

On Error GoTo Err_cmdLastRec_Click

If Me.NewRecord Then
Me.Dirty = False
Else
DoCmd.GoToRecord , , acLast
Call SetNavButtons(Me)
End If

Exit_cmdLastRec_Click:
Exit Sub

Err_cmdLastRec_Click:
MsgBox Err.Description
Resume Exit_cmdLastRec_Click

cmdLastRec_Click_Exit:

On Error Resume Next
Exit Sub

cmdLastRec_Click_Error:

MsgBox "Error " & Err.Number & " (" & Err.Description & _
") in procedure cmdLastRec_Click of VBA Document
Form_frmAttributetable"
GoTo cmdLastRec_Click_Exit

End Sub
I tried the code and get a Compile error: Expected End Sub after the
SetNaveButtons (Me)
[quoted text clipped - 52 lines]
 
Back
Top