Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > Move Keys < What are Macros & Modules? | Move Up Down >
Back to Move Up Down Using Keyboard    Comments List
Upload Images   @Reply   Bookmark    Link   Email  
Move Past First Last Record
Colin Eastaugh 
      
14 months ago
The following VBA code is worth the cost of membership if a lot of time is spent in continuous forms sorting out inconsistencies of data:

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
' ' If KeyCode = vbKeyDown Then
'DoCmd.GoToRecord , , acNext
' ' ElseIf KeyCode = vbKeyUp Then
DoCmd.GoToRecord , , acPrevious
' ' End If
End Sub

Comes from the Move Up / Dn Keys video from 3 years'ago.

Thank you Richard.

The only problem with the'code is that if you are at the edge of the continuous form, either at the first record or the last record, and you try to move back before the first record or move forward from the last record. (It happens when having'fun with the arrow keys.) Up pops an error message that has to be actioned.' Is there a way to avoid an error message?' Answering my own question, perhaps a workaround is to turn off error messages but is there a more elegant solution that uses IF THEN ELSE for instance?

A more general question, is there a way for the position of the cursor in a continuous form to be recorded and then actioned in a VBA program - first record, last record, record number say 100?
Adam Schwanz  @Reply  
            
14 months ago
I haven't tested this, but I would assume you can just use recordset EOF and BOF

If Recordset.BOF Then 'First Record
    If KeyCode = vbKeyDown Then
        DoCmd.GoToRecord , , acNext
    ElseIf KeyCode = vbKeyUp Then
        'Do Nothing
    End If
ElseIf Recordset.EOF Then 'Last Record
    If KeyCode = vbKeyDown Then
        'Do Nothing
    ElseIf KeyCode = vbKeyUp Then
        DoCmd.GoToRecord , , acPrevious
    End If
Else
    If KeyCode = vbKeyDown Then
        DoCmd.GoToRecord , , acNext
    ElseIf KeyCode = vbKeyUp Then
        DoCmd.GoToRecord , , acPrevious
    End If
End If
Adam Schwanz  @Reply  
            
14 months ago
For your second question, what are you trying to do exactly?

By default the record that the cursor is in is already the selected record and can be actioned on with vba.
Kevin Yip  @Reply  
     
14 months ago
This is a somewhat tricky problem to solve.  First of all, BOF and EOF don't work with DoCmd.GoToRecord.  BOF and EOF are like separate "records" of their own -- BOF is the "record" before the first record, and EOF is the "record" after the last record:

     BOF --> 1st record --> 2nd record ... ... 2nd last rec --> last rec --> EOF

DoCmd.GoToRecord can only go to "real records," and can never go to BOF or EOF, so it will never trigger the BOF and EOF states.

Only Recordset.MoveFirst, MoveLast, MovePrevious, and MoveNext can trigger BOF and EOF.  But here lies another tricky problem.  When BOF is triggered, Access is pointing at BOF, not the first record.  So while the user is at the first record, he has to go down *twice* to reach the 2nd record -- first to get from BOF to 1st, then from 1st to 2nd.  Same situation is with going upward from EOF.  So how do we prevent the user from having to go up or down twice just to reach the adjacent record?  Your code has to check preemptively if BOF or EOF is reached, and move the user back to the first or last record.  The resulting VBA code is below.  It's not too cumbersome, but just tricky.

    If KeyCode = vbKeyUp Then
        If Not Recordset.BOF Then
            Recordset.MovePrevious
            If Recordset.BOF Then Recordset.MoveFirst
        End If
    End If
    
    If KeyCode = vbKeyDown Then
        If Not Recordset.EOF Then
            Recordset.MoveNext
            If Recordset.EOF Then Recordset.MoveLast
        End If
    End If
Richard Rost  @Reply  
          
14 months ago
Ah, yet another problem solved with some simple error handling...

Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)

    On Error GoTo CantMove

    If KeyCode = vbKeyDown Then
        DoCmd.GoToRecord , , acNext
    ElseIf KeyCode = vbKeyUp Then
        DoCmd.GoToRecord , , acPrevious
    End If
    Exit Sub
    
CantMove:
    Beep
    KeyCode = 0
    
End Sub


Don't overthink it. :)
Colin Eastaugh OP  @Reply  
      
14 months ago
A touch of the forelock to Adam and Kevin, as we say in ye olde England.  Many thanks for your comments.

And yep, it's a fair comment, don't overthink the problem.  Thanks Richard.
Richard Rost  @Reply  
          
14 months ago
And yes, I'm sure you could put together a more elegant solution that doesn't involve just ignoring the error, but if it ain't broke, don't fix it. It works perfectly fine. Unless you can come up with a valid reason why you need to do it another way, I would do it this way.
Garry Smith  @Reply  
    
14 months ago
Good Solution Richard

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Move Up Down Using Keyboard.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 7/14/2025 6:02:36 PM. PLT: 1s