Advent Day 18 Ms Access Gurus

VBA > Form > Go to Previous

Go to the Previous record on a form.

Screen shot

The (orange) Previous Record command button on a form has a Control Tip property that pops up as you hover the mouse over it. The command button Caption is &Previous ... so Alt-P is the HotKey, which is nice for those who like using the keyboard.

The Property sheet shows that the On Click event says:

=RecordPrev([Form])

By putting a call to a function directly on the Property Sheet, it will be copied when the button is copied too. In code behind the form, you reference the form as "Me". In expressions for controls, it is [Form].

Generally, it is a good idea to keep code behind the form, and not on the property sheet. However, with form navigation buttons, it is easier to copy buttons with logic when function calls are on the property sheet.

Embedded macros are also copied when buttons are copied, but they are harder to document.

Examples


on Property Sheet

=RecordPrev( [Form] )

in code behind the form:

Call RecordPrev( Me )

in code behind the form: also set focus to the contact note control (control name = "NoteCtc"):

Call RecordPrev( Me, "NoteCtc" )

Logic

The return data type is Byte, but it is not set, or intended to be used. This is an indicator that this function is also designed to be called from a property sheet (since Byte is small and rarely used).

Skip errors.

If a form reference is not passed, use the Screen.ActiveForm

If the record has unsaved changes, save the record (Dirty = False)

If pFirstControlName was specified, then move focus to that control name specified. This is done before actually finding the record to be in the 'With pF' block.

Keep track of the current record so we can know if it is the first one.

If records are showing (RecordCount > 0), and the current record is not already the first one, then move to the Previous record. Otherwise, cycle to the last record. If you don't want to do this, then comment the MoveLast statement.

Move # is used instead of MovePrevious so if you wanted to make the number of records to skip a parameter, you could.

Parameters

Optional:

If no parameters are passed, move to the Previous record on the active(main) form.

Code

'*************** Code Start *****************************************************
' Purpose  : Go to the Previous record on a form.
' Author   : crystal (strive4peace)
' Return   : Byte (not set)
' License  : below code
' Code List: www.MsAccessGurus.com/code.htm
'-------------------------------------------------------------------------------

' RecordPrev

'------------------------------------------------------------------------------- '
Function RecordPrev(Optional pF As Form _ , Optional pFirstControlName As String = "") As Byte ' crystal (strive4peace) '3-20-09... 160819, 181216 ' example useage: Click [Event Procedure] for a Go To Previous Record command button ' RecordPrev ' Call RecordPrev(Me) ' Call RecordPrev(Me, "Controlname") 'can also be assigned on the property sheet 'so logic is copied when buttons are copied ' =RecordPrev([Form]) Dim nCurrentRecord As Long On Error Resume Next 'if form reference was not passed, use the active form If pF Is Nothing Then Set pF = Screen.ActiveForm 'with the referenced form ... With pF 'if there have been changes to the current record, save them If .Dirty Then .Dirty = False DoEvents 'do it now 'set focus to first control, if specified If pFirstControlName <> "" Then .Controls(pFirstControlName).SetFocus End If 'keep track of the current record nCurrentRecord = .CurrentRecord With .Recordset 'if there are records ... If .RecordCount > 0 Then 'if not at the first record ... If nCurrentRecord <> 1 Then 'move up 1 .Move -1 'could also use MovePrevious Else 'at top -- move to last record 'COMMENT if you don't want to do this .MoveLast End If End If End With 'pF.Recordset End With'pF End Function ' LICENSE ' You may freely use and share this code ' provided this license notice and comment lines are not changed; ' code may be modified provided you clearly note your changes. ' You may not sell this code alone, or as part of a collection, ' without my handwritten permission. ' All ownership rights reserved. Use at your own risk. ' ~ crystal (strive4peace) www.MsAccessGurus.com '*************** Code End *******************************************************

Notes


on Property Sheet

Picture property

When you look at the property sheet for a command button, you can change the Picture property PropertySheet-CommandButton-Format

When you click in the Picture property, you can click on the Builder button (...) or press Ctrl-F2, to popup the Picture Builder:

PictureBuilder

and then find the picture you want, or Browse... to specify a file on your drive.

Share

Share with others ... here's the link to copy:
secure: https://MsAccessGurus.com/VBA/Code/Form_GotoPrevious.htm
or
unsecure: http://MsAccessGurus.com/VBA/Code/Form_GotoPrevious.htm

Reference

Docs / Office client development / Access / Access desktop database reference / Microsoft Data Access Objects reference / Recordset / Methods / MovePrevious method

Help: Recordset.MovePrevious method (DAO)

Docs / Office client development / Access / Access desktop database reference / Microsoft Data Access Objects reference / Recordset / Methods / Move method

Help: Recordset.Move method (DAO)