Results 1 to 6 of 6
  1. #1
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659

    Question VBA disable Next and Last Buttons


    I am using this code to disable my Next and Last buttons. The code works when I step through the code. I set a break point on the code in the event, trigger the event. And it works as I hope. However when I run without the breakpoint, the buttons just stay disabled.


    Code:
    Private Sub Form_Current()
        Me.Activities_Last_Button.Enabled = Not Recordset.AbsolutePosition = Recordset.RecordCount - 1
        Me.Actvities_Next_Button.Enabled = Not Recordset.AbsolutePosition = Recordset.RecordCount - 1
    End Sub
    Note, that the spelling of the next button is correct to the control, just noticed. but compiles lol.
    Thoughts?

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    In Access, the = operator can act as an assignment operator or as an evaluation operator. Maybe you can get everything into one line using parenthesis.
    Code:
    Me.Activities_Last_Button.Enabled = Not (Recordset.AbsolutePosition = Recordset.RecordCount - 1)

    Perhaps this returns what you want. But, I would try an approach that is easier to read.

    Maybe you can evaluate the recordcount and use the value of the property to determine what to assign Me.Activities_Last_Button.Enabled.

    Code:
    If Recordset.AbsolutePosition = Recordset.RecordCount - 1 then
    'Do this
    else
    'Do something else
    end if
    Last edited by ItsMe; 09-17-2016 at 12:22 PM. Reason: Removed one comment

  3. #3
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    Tons of googling has shown that i need that parenthesis also. I thought that vb forcedleft handed evaulation and wouldnt need it. Thanks for nudging me back in the right direction.

    But,
    Code:
    If Not (Recordset.AbsolutePosition = Recordset.RecordCount - 1) Then
        Me.Activities_Last_Button.Enabled = True
        Me.Actvities_Next_Button.Enabled = True
    Else
        Me.Activities_Last_Button.Enabled = False
        Me.Actvities_Next_Button.Enabled = False
    End If
    yields the same issue. When i step through using break points. the code block evaluates correctly. but when running without breakpoint it does not. =\

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    In my testing, both of these code snippets worked (watch the spelling if the control name )
    Notice the 2 pair of parentheses.... (in blue)
    Code:
    Private Sub Form_Current()
        '   Me.Activities_First_Button.Enabled = Not (Recordset.AbsolutePosition = 0)
        '   Me.Activities_Previous_Button.Enabled = Not (Recordset.AbsolutePosition = 0)
    
        Me.Actvities_Next_Button.Enabled = Not (Recordset.AbsolutePosition = (Recordset.RecordCount - 1))
        Me.Activities_Last_Button.Enabled = Not (Recordset.AbsolutePosition = (Recordset.RecordCount - 1))
    
    End Sub
    or
    Code:
        If Not (Recordset.AbsolutePosition = (Recordset.RecordCount - 1)) Then
            Me.Activities_Last_Button.Enabled = True
            Me.Actvities_Next_Button.Enabled = True
        Else
            Me.Activities_Last_Button.Enabled = False
            Me.Actvities_Next_Button.Enabled = False
        End If
    After making the code changes, be sure to put the form in design view, the go back to normal view before testing the code changes.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 8 Access 2013
    Join Date
    Aug 2013
    Posts
    7,862
    @Steve
    I like the first example. It seems the most intuitive when reading it.

  6. #6
    Perceptus's Avatar
    Perceptus is offline Expert
    Windows 7 64bit Access 2007
    Join Date
    Nov 2012
    Location
    Knoxville, Tennessee
    Posts
    659
    I ended up having to do
    Code:
        With Recordset
            If .AbsolutePosition = .RecordCount - 1 Then
                If .RecordCount - 1 = 0 Then
                    blnNextLastState = True
                End If
            Else
                blnNextLastState = Not .AbsolutePosition = .RecordCount - 1
            End If
    Im not sure why the record count was 1 when it was never only 1. But it works now. Thank you for the help =)
    Last edited by Perceptus; 09-19-2016 at 05:57 AM. Reason: posted solution

Please reply to this thread with any new information or opinions.

Similar Threads

  1. MSG Box Disable
    By McArthurGDM in forum Programming
    Replies: 3
    Last Post: 02-25-2015, 04:26 PM
  2. Disable certain keys
    By data808 in forum Forms
    Replies: 5
    Last Post: 03-01-2014, 10:47 PM
  3. Replies: 1
    Last Post: 08-15-2013, 08:12 PM
  4. Replies: 10
    Last Post: 09-18-2012, 02:00 PM
  5. Disable ALT+TAB key using VBA
    By Yance in forum Programming
    Replies: 8
    Last Post: 08-27-2009, 10:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums