Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigation Buttons 1

Status
Not open for further replies.

GelC

Technical User
Oct 3, 2006
93
US
Hi all,
I need help with a custom navigation buttons.
When I go to the very first or very last record, there is a pop-up message "You can't go to the specific record."
How can I make it not showing anymore.
Below is the code. Thank you in advanced.
Code:
Private Sub cmdNextRecord_Click()
On Error GoTo Err_cmdNextRecord_Click
    
    DoCmd.GoToRecord , , acNext
    Me.cmdFirstRecord.Enabled = True
    Me.cmdPreviousRecord.Enabled = True
    
Exit_cmdNextRecord_Click:
    Exit Sub

Err_cmdNextRecord_Click:
    MsgBox Err.Description
    Resume Exit_cmdNextRecord_Click
End Sub

Private Sub cmdPreviousRecord_Click()
On Error GoTo Err_cmdPreviousRecord_Click
    
    DoCmd.GoToRecord , , acPrevious
    Me.cmdNextRecord.Enabled = True

Exit_cmdPreviousRecord_Click:
    Exit Sub

Err_cmdPreviousRecord_Click:
    MsgBox Err.Description
    Resume Exit_cmdPreviousRecord_Click

End Sub
 
Typically you would disable the previous and next buttons so the code can't run. Otherwise, for future reference, display the error number so you can trap for it:
Code:
Private Sub cmdNextRecord_Click()
On Error GoTo Err_cmdNextRecord_Click
    
    DoCmd.GoToRecord , , acNext
    Me.cmdFirstRecord.Enabled = True
    Me.cmdPreviousRecord.Enabled = True
    
Exit_cmdNextRecord_Click:
    Exit Sub

Err_cmdNextRecord_Click:
    MsgBox [b][red]Err.Number & ": " & [/red][/b]Err.Description
    Resume Exit_cmdNextRecord_Click
End Sub
Once you have the error number you can change the code like:
Code:
Private Sub cmdNextRecord_Click()
On Error GoTo Err_cmdNextRecord_Click
    
    DoCmd.GoToRecord , , acNext
    Me.cmdFirstRecord.Enabled = True
    Me.cmdPreviousRecord.Enabled = True
    
Exit_cmdNextRecord_Click:
    Exit Sub

Err_cmdNextRecord_Click:
  Select Case Err.Number
    Case [b][red]### your displayed error number ###[/red][/b]
        Resume Next
    Case Else
         MsgBox Err.Number & ": " & Err.Description
  End Select 
    Resume Exit_cmdNextRecord_Click
End Sub


Duane
Hook'D on Access
MS Access MVP
 
If you are going to spend the time making custon navigation buttons I would recommend making them as a subform. Then you can reuse that subform by placing it into the footer of any form. Mostly wherever you see "Me" in the code you replace that with "Me.Parent".
 
Hi! As opposed to starting a new thread I have decided to ask an extended question within this thread as I too have done what GelC did and have now taken the advice from dhookom and disabled my navigation buttons as appropriate.
However, I wish to know how to use the BOF and EOF functions when moving the record pointer to the first or last record and subsequently disabling the appropriate navigation buttons (e.g. if moving from the second record to the first record, how do I test for BOF in a subform displayed as a datagrid)?
I hope GelC that you don't mind my asking the question in your thread and hope it may help you and others.
Once I have perfected my navigation buttons I will also implement them as per MajP's suggestion.
Thanking you
Peter
 
It's okay to me as long as the mods are okay with it.

I just modify my code per dhookom for now.

Thanks guys!
 
You can add code in the On Current event of your form like:
Code:
Private Sub Form_Current()
    'this worked for me
    Me.cmdPrev.Enabled = Me.CurrentRecord <> 1
    Me.cmdNext.Enabled = Me.CurrentRecord <> _
          Me.RecordsetClone.RecordCount
End Sub

Duane
Hook'D on Access
MS Access MVP
 
Thank you for the quick reply.
Duane,
I have a form with a subform "sfrCustomerBrowser" which is a datasheet view of customer details (name & address only)
I use the datasheet view to quickly find the relevant customer by scrolling down by surname. Once the relevant customer is identified the user double clicks the record which opens up another window on that customer.
The navigation buttons are located on the main form "frmCustomerBrowser" and as such I don't think the "Me." is correct but I don't know how to point to the records in the datasheet on the subform.
My navigation buttons work fine as follows:
Code:
Private Sub btnGoToLastRecord_Click()
On Error GoTo Err_btnGoToLastRecord_Click

    Forms![frmpatientBrowser]![ctrlPatientBrowser].SetFocus
    DoCmd.GoToRecord , , acLast
    Me.btnGoToNextRecord.Enabled = False
    Me.btnGoToPreviousRecord.Enabled = True
    
Exit_btnGoToLastRecord_Click:
    Exit Sub

Err_btnGoToLastRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnGoToLastRecord_Click

End Sub
I just wish to disable the relevant buttons if the selected record is the first or last ones.

Thanks in advance for your assistance.
Peter
 
I would think this works. What part does not work?

Your control buttons are on the main form

You set the focus to your subform

When you use the docmd without arguments it should move your subform since it has the focus

That should all work

Since your buttons are on the Main form and the code is on the Main form "Me." should be correct. Me used within a forms module refers to the form associated with the module. In other words "Me" within the Main forms module refers to the Main form. So that should work.

A subform can refer to the Main form by "Me.Parent"
 
Sorry for the time to reply but have had a sleep and a full days work - thanks for the replies.
I sadly did not make myself clear with my code example so have attached the code with further explanation and highlighted in red the line that I need help with.
Code:
Private Sub btnGoToPreviousRecord_Click()
[COLOR=green]' Can only be run if btnGoToPreviousRecord is enabled[/color]
Dim Msg As String

On Error GoTo Err_btnGoToPreviousRecord_Click

    Forms![frmPatientBrowser]![ctrlPatientBrowser].SetFocus [COLOR=green]' set focus to subform [/color]
    DoCmd.GoToRecord , , acPrevious  [COLOR=green]' move cursor to previous record [/color]
    
    [COLOR=green]' since we have been able to move cursor to previous record,
    ' we must be able to move cursor to next record (i.e. where we were)
    ' and move to last record (which may only be one record forward
    ' therefore enable both GoToNext & GoToLast navigation buttons[/color]
    Me.btnGoToNextRecord.Enabled = True [COLOR=green]' This works fine [/color]
    Me.btnGoToLastRecord.Enabled = True [COLOR=green]' This works fine [/color]
    
[COLOR=red]    If Me.CurrentRecord = 1 Then [/color]  [COLOR=green]' Testing for Me.BOF or Me.CurrentRecord = 1 does not work [/color]
    
        [COLOR=green]' If by moving to the previous record, we have moved to the first record (BOF)then
        ' disable the GoToPrevious & GoToFirst navigations buttons as
        ' we are unable to move in that direction any further [/color]
        Me.btnGoToPreviousRecord.Enabled = False [COLOR=green]' I expect this to work [/color]
        Me.btnGoToFirstRecord.Enabled = False [COLOR=green]' I expect this to work [/color]
    End If
            
Exit_btnGoToPreviousRecord_Click:
    Exit Sub

Err_btnGoToPreviousRecord_Click:
    MsgBox Err.Description
    Resume Exit_btnGoToPreviousRecord_Click
End Sub
In answer to Duane's question on why customising my own navigation buttons, I guess I don't like the look of the system navigation buttons.
I hope I have explained my self better this time.
Thanks
Peter
 
Since you are referring not to the current record on the main form, but the subform you need to reference the subform within the subform control.

Me.NameOfSubFormControl.Form.propery

Me.[ctrlPatientBrowser].form.currentRecord
 
Thank you very much - problem fixed.
Regards
Peter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top