Solved

Database Form Navigation using command buttons for moveNext etc

Posted on 2003-12-05
3
428 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
  • 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Introduction: Often, when running a query with joins, the results show up "duplicates", and often, those duplicates can be "eliminated" in the results using DISTINCT, for example. Using DISTINCT is simple: just add it after the SELECT keyword, an…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
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…

758 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

19 Experts available now in Live!

Get 1:1 Help Now