MS Access - Record navigation

Mike McCracken
Mike McCracken used Ask the Experts™
on
Is it possible to trap or detect that the record navigation buttons have been clicked with the mouse?

On a few of the forms  I need to know when the user navigates to a NEW record through either the > or >* button.

I haven't found a way to detect they have been used.  The users are used to being able to navigate through the records with the buttons but in a few cases a new record needs some initialization before the user can enter data.

If I can't detect the click can I prevent the user from navigating to a new record?
The forms have an ADD RECORD button that provides the initialization through code before the user can enter data.

mlmcc
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Setting the AllowAdditions property of the form to No will prevent them from navigating to a new record.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007
Commented:
In the Current event of a Form, you can test for a New Record:

If Me.NewRecord = True then
   ' do something
Else
   'do something else
End If

mx
Alternatively, you can add a check on the form's OnCurrent event like so:
If NewRecord Then 
    Call Initialize routine
End If

Open in new window

11/26 Forrester Webinar: Savings for Enterprise

How can your organization benefit from savings just by replacing your legacy backup solutions with Acronis' #CyberProtection? Join Forrester's Joe Branca and Ryan Davis from Acronis live as they explain how you can too.

Dale FyeOwner, Dev-Soln LLC
Most Valuable Expert 2014
Top Expert 2010
Commented:
As MX and IrogSinta have said, to detect a new record, use the Form_Current event  and the NewRecord property of the Form.

Occassionally, you will find that you need some functionality to occur before the user leaves the current record.  If there have been no changes to that record then the Form_AfterUpdate event will not fire, so you may end up creating your own set of navigation buttons.  I usually put mine in the Forms footer and turn off the default navigation buttons.

You will also find this technique handy if you do any work with unbound forms.
Hamed NasrRetired IT Professional

Commented:
Try this:
declare a global variable in a module
Set global variable to true if button clicked
In Form_Current event check for global variable and use your initialization code if true, then set to false

Global bln_navigation As Boolean ' in a module

Private Sub Command22_Click() 'the button to navigate or to add
    bl_navigation = True
    DoCmd.GoToRecord , , acNext
End Sub

Private Sub Form_Current()
    If bln_navigation = True Then 'got here through button
        'your code here
        bln_navigation = False
    End If
End Sub 

Open in new window

Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Author

Commented:
IrogSinta - A simple solution.  I just had to add code to the ADD/New record button to allow additions and turn it off when they saved the record.

 DatabaseMX - Your method would work but a little more involved than I wanted to do.

 fyed -  Good additional information.

increased points to 500

mlmcc
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
Top Expert 2007

Commented:
" but in a few cases a new record needs some initialization before the user can enter data."
Which is exactly what I posted ... so, I guess I'm confused.
Mike McCrackenSenior Consultant
Most Valuable Expert 2011
Top Expert 2013

Author

Commented:
I just found it easier to do all the code behind the ADD/NEW record button rather than adding code to the form's OnCurrent event.

mlmcc

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial