Disable navigation button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • didajosh
    New Member
    • Aug 2008
    • 47

    Disable navigation button

    I have made a form and added navigation buttons using the wizard.
    When the focus is on the first Record, I want the "Previous Record Button" to disable.
    So in the code in Click event I want to write something like:
    Code:
    If Current Record = BOF
    then Command82.Enabled = False 
    End if
    How do I write this...?
    Last edited by NeoPa; Apr 20 '10, 01:02 PM. Reason: Please use the [CODE] tags provided.
  • patjones
    Recognized Expert Contributor
    • Jun 2007
    • 931

    #2
    Hi -

    If this is a bound form, you could try putting this in the On Current event for the form:

    Code:
    Dim rst As Recordset
    Set rst = Me.RecordsetClone
    
    If rst.BOF Then
         cmdPreviousRecord.Enabled = False
    Else
         cmdPreviousRecord.Enable = True
    End If

    A shorthand way of writing the If construct is:

    Code:
    cmdPreviousRecord.Enabled = Not rst.BOF

    Pat

    Comment

    • NeoPa
      Recognized Expert Moderator MVP
      • Oct 2006
      • 32664

      #3
      I have had problems with this logic before, which I must admit I never really got to the heart of. I forget under which circumstances I found .BOF & .EOF to be unreliable, but they were what I tried first.
      Code:
      With Me
          'Bug where .AbsolutePosition can be 0 even when on NewRecord
          .cmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or _
                                       (.NewRecord))
      ...
      End With

      Comment

      • patjones
        Recognized Expert Contributor
        • Jun 2007
        • 931

        #4
        I have successfully used .BOF and .EOF in various contexts before; however in looking at what I wrote yesterday I don't think putting the code

        Code:
        Dim rst As Recordset
        Set rst = Me.RecordsetClone

        in with the rst.BOF test will work because the assignment of rst will, I think, automatically place the bookmark on the first record; so rst.BOF would just always evaluate to true.

        I'm going to make a little mock-up form and play with this, pending completion of some matters here at work.

        Pat

        Comment

        • didajosh
          New Member
          • Aug 2008
          • 47

          #5
          Hi,
          Thank you for your time and suggestions.
          I tried to implement what zepphead80 told me.

          I don't think rst.BOF would just always evaluate to true, on the contrary its always evaluating as False. I put a couple of msg boxed to check if the condition is being satisfied or not (following code) but turns out "True" msgbox never appears.
          Code:
             Dim rst As Recordset
             Set rst = Me.RecordsetClone
             If rst.BOF Then
             MsgBox "True"
             CmdPrevButton.Enabled = False
             Else
             MsgBox "False"
             CmdPrevButton.Enabled = True
             End If
          The button remains enabled.

          Dipali
          Last edited by NeoPa; Apr 21 '10, 12:08 PM. Reason: Please use the [CODE] tags provided.

          Comment

          • patjones
            Recognized Expert Contributor
            • Jun 2007
            • 931

            #6
            Dipali -

            The button will in fact turn gray.

            As I mentioned in my post this morning, I didn't think this code would work properly. I have a mock database set up to test out how to do this and I'll let you know what I come up with, shortly.

            Pat

            Comment

            • didajosh
              New Member
              • Aug 2008
              • 47

              #7
              Hi,
              I guess I found a solution.
              Code:
               Dim rst As Recordset
                 Set rst = Me.Recordset
                 If rst.AbsolutePosition = 0 Then
                 Command28.Enabled = False
                 Else
                 Command28.Enabled = True
                 End If
              It works perfectly. I just stumbled on "AbsolutePositi on".
              I am not sure if its alright to use it, or if this is the right way.

              -Dipali
              Last edited by NeoPa; Apr 21 '10, 12:08 PM. Reason: Please use the [CODE] tags provided.

              Comment

              • patjones
                Recognized Expert Contributor
                • Jun 2007
                • 931

                #8
                Hi Dipali -

                That is in fact what I basically did as well. Upon researching BOF I remembered an important fact, which is that it doesn't get set to false until you are actually on the first record AND try to move to a previous record (same thing for EOF, at the last record).

                What I ended up doing was declaring a recordset visible to the entire form module, "rst". I then coded the Form Open event, and Click events for the two command buttons as follows:

                Code:
                Private Sub cmdNext_Click()
                
                Me.cmdPrevious.Enabled = True
                
                If rst.AbsolutePosition < rst.RecordCount - 1 Then
                
                    rst.MoveNext
                    Me.Bookmark = rst.Bookmark
                
                    If rst.AbsolutePosition = rst.RecordCount - 1 Then
                        Me.cmdPrevious.SetFocus
                        Me.cmdNext.Enabled = False
                    End If
                    
                End If
                
                End Sub
                
                Private Sub cmdPrevious_Click()
                
                Me.cmdNext.Enabled = True
                
                If rst.AbsolutePosition > 0 Then
                    
                    rst.MovePrevious
                    Me.Bookmark = rst.Bookmark
                    
                    If rst.AbsolutePosition = 0 Then
                        Me.cmdNext.SetFocus
                        Me.cmdPrevious.Enabled = False
                    End If
                        
                End If
                
                End Sub
                
                Private Sub Form_Open(Cancel As Integer)
                
                Set rst = Me.RecordsetClone
                rst.MoveLast
                rst.MoveFirst
                Me.Bookmark = rst.Bookmark
                Me.cmdPrevious.Enabled = False
                
                End Sub

                Let me know what you think.

                Pat

                Comment

                • didajosh
                  New Member
                  • Aug 2008
                  • 47

                  #9
                  Hi Pat,
                  Thank you for your generous help. I really appreciate it.
                  I need to try this and will get back with you.

                  Dipali

                  Comment

                  • NeoPa
                    Recognized Expert Moderator MVP
                    • Oct 2006
                    • 32664

                    #10
                    Hi Dipali.

                    From your comments I assume you didn't notice my post #3 from earlier. .AbsolutePositi on is good, but as you'll see from that post, it's not perfect on its own. You would also need to test .NewRecord if you want the code to work logically.

                    Comment

                    • didajosh
                      New Member
                      • Aug 2008
                      • 47

                      #11
                      Hi,
                      Thank you so much for the help.I am very grateful to you guys.
                      I clubbed the two solutions suggested to me.
                      It works perfectly.
                      Its in the Current event of the form. And its for previous, next, first and last buttons.
                      Code:
                      With Me
                        CmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
                        CmdNextRecord.Enabled = ((.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1) Or (.NewRecord))
                        CmdFirstRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
                        CmdLastRecord.Enabled = ((.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1) Or (.NewRecord))
                      End With
                      Last edited by NeoPa; Apr 21 '10, 08:13 PM. Reason: Please use the [CODE] tags provided

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32664

                        #12
                        That's very good thinking. You have clearly grasped the point and built on it.

                        I would consider slight changes though, to support the logic :
                        Code:
                        With Me.
                          .CmdPreviousRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
                          .CmdNextRecord.Enabled = (Not .NewRecord)
                          .CmdFirstRecord.Enabled = ((.Recordset.AbsolutePosition > 0) Or (.NewRecord))
                          .CmdLastRecord.Enabled = ((.Recordset.RecordCount > 0) And (.Recordset.AbsolutePosition <> .Recordset.RecordCount - 1))
                        End With

                        Comment

                        Working...