Determine if You’re on a New Record in a Form

Problem

Often, you need to do different things depending on whether the current row is the “new” row on a form. For example, you might want to display a certain message box only when adding records. How can you do this?

Solution

You can use a form’s NewRecord property to determine if you are on a new record by checking its value from an event procedure attached to the OnCurrent event property or some other event property of the form.

Follow these steps to implement this functionality in your own forms:

  1. Create a new form or modify the design of an existing form.

  2. Create an event procedure for the form’s Current event. In that event procedure, create an If...Then statement that will branch based on the value of the form’s NewRecord property. The code of the event procedure should look like this:

    Private Sub Form_Current(  )
        If Me.NewRecord Then
            ' Do something for a new record.
        Else
            ' Do something for an existing record.
        End If
    End Sub
  3. You may wish to alter some visual cue on the form to indicate whether you are on a new record. For example, you might change the text of a label, the text of the form’s titlebar, or the picture of an image control. In the sample form, we changed the picture of an image control in the form’s header, imgFlag, by copying the picture from one of two hidden image controls that are also located on the form. The final Current event procedure looks like this:

    Private Sub Form_Current( ) ' Determine if this is a ...

Get Access Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.