How to disable a cmd button when on the first and last record?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ryno Bower
    New Member
    • Nov 2010
    • 76

    How to disable a cmd button when on the first and last record?

    Hello.
    I've got a form with buttons on it to go through records. When I click the button to go a previous record and it reached the first record I want it to disable the button when it has reached the first record in the form/table.

    This is what I tried.

    I have coded it correctly to go to previous records but when it reached the first record I get a vba message sayig that I can't go to the selected record. I don't want to see that message and I want to disable the button when on the first record. The codes I tried just didn't work.

    Can someone please help?

    Ryno
  • malcolmk
    New Member
    • Sep 2010
    • 79

    #2
    Look into using the forms oncurrent event to enable or disable your button.First on form open event I would
    Code:
    public last as long
    DoCmd.GoToRecord , , acLast
    last= me.currentrecord
    'that gives you the last record number
    DoCmd.GoToRecord , , acFirst
    'go back to first record
    Now in the forms oncurrent event test to see if at first or last record and enable or disable your button accordingly.
    Code:
    Private Sub Form_Current()
    Dim current As Long
    current = Me.CurrentRecord
    End Sub

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      What you are referring to is a system of 'Smart' Navigation Buttons that will work in tandem with each other. Rather that post the Code, I'm Attaching a Demo that will clearly illustrate how this is done. Look closely at the Code in the Form's Current(0 Event. It is well documented, and should clearly explain what is going on. Any questions, please feel free to ask.
      Attached Files

      Comment

      • mshmyob
        Recognized Expert Contributor
        • Jan 2008
        • 903

        #4
        Code like so is very simple. It is in the ONClick event of the button (this is for the previous button).

        Code:
        On Error GoTo cmdPrevious_Click_Err
        
            On Error Resume Next
            DoCmd.GoToRecord , "", acPrevious
            If (MacroError <> 0) Then
                Beep
                MsgBox MacroError.Description, vbOKOnly, ""
            End If
        
        
        cmdPrevious_Click_Exit:
            Exit Sub
        
        cmdPrevious_Click_Err:
            MsgBox Error$
            Resume cmdPrevious_Click_Exit
        The "On error resume next" statement will eliminate that message.

        No need to disbale the button. It just won't do anything now when on the first record.

        cheers,

        Comment

        • ADezii
          Recognized Expert Expert
          • Apr 2006
          • 8834

          #5
          I sort of like the Visual Cue.

          Comment

          Working...