Solved

Database Form Navigation using command buttons for moveNext etc

Posted on 2003-12-05
3
437 Views
Last Modified: 2008-03-06
Hello,
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
0
Comment
Question by:sy2180
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 2

Expert Comment

by:bagger99
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.

HTH
0
 
LVL 30

Expert Comment

by:nmcdermaid
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
  Else
     cmdMovePrevious.Enabled = True
  End If
 
  If Me.Recordset.AbsolutePostion = Me.Recordset.RecordCount - 1 Then
     cmdMoveNext.Enabled = False
  Else
     cmdMoveNext.Enabled = True
  End If
End Sub
0
 
LVL 2

Accepted Solution

by:
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
   'etcetera
End Sub
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

As technology users and professionals, we’re always learning. Our universal interest in advancing our knowledge of the trade is unmatched by most industries. It’s a curiosity that makes sense, given the climate of change. Within that, there lies a…
A company’s centralized system that manages user data, security, and distributed resources is often a focus of criminal attention. Active Directory (AD) is no exception. In truth, it’s even more likely to be targeted due to the number of companies …
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…

756 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