Advent Day 17 Ms Access Gurus

VBA > Form > Go to Next

Go to the Next record on a form.

Screen shot

The (orange) Next 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 &Next ... so Alt-N is the HotKey, which is nice for those who like using the keyboard.

The Property sheet shows that the On Click event says:

=RecordNext([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

=RecordNext( [Form] )

in code behind the form:

Call RecordNext( Me )

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

Call RecordNext( 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 last one.

If records are showing (RecordCount > 0), and the current record is less than the last one (RecordCount), then move to the Next record. Otherwise, cycle back to the first record. If you don't want to do this, then comment the MoveFirst statement.

Move # is used instead of MoveNext 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 next record on the active(main) form.

Code

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

' RecordNext

'------------------------------------------------------------------------------- '
Function RecordNext(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 Next Record command button ' RecordNext ' Call RecordNext(Me) ' Call RecordNext(Me, "Controlname") 'can also be assigned on the property sheet 'so logic is copied when buttons are copied ' =RecordNext([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 already at the bottom ... If nCurrentRecord < .RecordCount Then 'move down 1 .Move 1 'could also use MoveNext Else 'at end -- move to first record 'COMMENT if you don't want to do this .MoveFirst 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_GotoNext.htm
or
unsecure: http://MsAccessGurus.com/VBA/Code/Form_GotoNext.htm

Reference

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

Help: Recordset.MoveNext 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)