Database Form Navigation using command buttons for moveNext etc

Posted on 2003-12-05
Last Modified: 2008-03-06
I am using Access 2002 and VBA

I have added buttons to a form to navigate through the records using the four common options moveNext MovePrevious, MoveLast, MoveFirst and AddNewRecord.
linking the onClick event procedure like this:
Private Sub cmdMoveNext_Click()
 DoCmd.GoToRecord , , acNext
End Sub

This works fine to allow me to work through the records, but I need to make the coding more robust to prevent errors appearing when:
 trying to move to the record before the first record
 trying to move to the record after the add new record

The coding needs to incorporate options to disable the buttons
 i.e. disable the buttons using cmdMovePrevious.enabled = false when sitting on the first record

The coding must work using ADO.
I will be grateful for any tips and advice
Question by:sy2180
  • 2

Expert Comment

ID: 9881732
Mayber an easier solution than ADO :
You can disable the command button after a user has clicked it :

Private Sub cmdMoveFirst_Click()
    DoCmd.GoToRecord , , acFirst
    Me!EmployeeID.SetFocus 'set focus to a field on the form, otherwise you can't disable the button
    Me.cmdMoveFirst.Enabled = False
End Sub

Also set the default enabled value of this button on 'false', and back to 'true' whenever a user clicks on one of the other buttons :
Private Sub cmdMoveNext_Click()
 DoCmd.GoToRecord , , acNext
 Me.cmdMoveFirst.Enabled = True
End Sub

Apply the same logic to the cmdMoveLast button.

LVL 30

Expert Comment

ID: 9881744
Is there any reason you aren't using the standard form navigation methods (Form property Navigation Buttons = Yes)

If you want to check if you are at the start/end of the recordset, assuming you are using a bound form (ADO  or DAO),

Me.Recordset.AbsolutePostion = 0 when at the start of the recordset
Me.Recordset.AbsolutePostion = Me.Recordset.RecordCount-1 when at the end of the recordset

Private Sub Form_Current()
  If Me.Recordset.AbsolutePostion = 0 Then
     cmdMovePrevious.Enabled = False
     cmdMovePrevious.Enabled = True
  End If
  If Me.Recordset.AbsolutePostion = Me.Recordset.RecordCount - 1 Then
     cmdMoveNext.Enabled = False
     cmdMoveNext.Enabled = True
  End If
End Sub

Accepted Solution

bagger99 earned 140 total points
ID: 9881855
Since the AbsolutePosition property is new to me, I tried it out, but get the error message "Object doesn't support this property".

Private Sub Form_Current()

    If Me.Recordset.AbsolutePostion = 0 Then
        MsgBox "0"
    End If
End Sub

The Recordsetclone object does't support it either.

You'll have to slightly adapt the code :

Private Sub Form_Current()
    Dim rst As dao.Recordset
    Set rst = Me.RecordsetClone
    If rst.AbsolutePosition = 0 Then
         cmdMovePrevious.Enabled = False
    End If
End Sub

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
Many companies are looking to get out of the datacenter business and to services like Microsoft Azure to provide Infrastructure as a Service (IaaS) solutions for legacy client server workloads, rather than continuing to make capital investments in h…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now