ACC1x: Checking for Last or First Record in a Macro

Last reviewed: April 2, 1997
Article ID: Q95807
The information in this article applies to:
  • Microsoft Access versions 1.0, 1.1

SUMMARY

In a macro, you cannot check the result of an action just performed. As a result, you can run into a problem when using a macro to move to the next record or the previous record. There is no record previous to the first record and no next record following the last record. If you attempt to go to the previous or last record when there is none, Microsoft Access displays error messages that may confuse users of a custom application.

This article assumes that you are familiar with Access Basic and with creating Microsoft Access applications using the programming tools provided with Microsoft Access. For more information on Access Basic, please refer to the "Introduction to Programming" manual.

MORE INFORMATION

You can work around this on a form by using Access Basic functions to move from record to record. For example, use the following custom MoveToNext() function to move to the next record or previous record on a form:

   Option Explicit
   Function MoveToNext (DisplayMsg$, MoveForward%)
      Dim MyDyna As Dynaset
      Set MyDyna = Screen.ActiveForm.Dynaset

      MyDyna.Bookmark = Screen.ActiveForm.Bookmark
      If MoveForward% Then
         MyDyna.MoveNext
      Else
         MyDyna.MovePrevious
      End If
      If MyDyna.EOF Or MyDyna.BOF Then
         MsgBox DisplayMsg$
      Else
         Screen.ActiveForm.Bookmark = MyDyna.Bookmark
      End If
   End Function

To use the MoveToNext() function, specify the following:
  • The name of the function.
  • The message you want displayed if the current record is at the end or the beginning of the record set.
  • "True" if you want to move to the next record. "False" if you want to move to the previous record.

For example:

   OnPush: =MoveToNext("You are at the end of the record set", True)
 

	
	


Keywords : kbusage McrOthr
Version : 1.0 1.1
Platform : WINDOWS
Hardware : X86
Issue type : kbinfo


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: April 2, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.