Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Database Form Navigation using command buttons for moveNext etc

Posted on 2003-12-05
3
Medium Priority
?
455 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 420 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller singl…
In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

773 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