[ Team LiB ] |
Recipe 6.2 Determine if You're on a New Record in a Form6.2.1 ProblemOften, 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? 6.2.2 SolutionYou 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:
To see an example, load and open frmContacts from 06-02.MDB. Notice that the picture in the upper-left corner of the form changes to indicate whether you are editing an existing record (Figure 6-3) or adding a new record (Figure 6-4). In addition, when you save a newly added record, a message box is displayed that reminds you to log the new record (Figure 6-4). The message box does not appear when you save changes to an existing record. Figure 6-3. The sample form indicates that you are editing an existing recordFigure 6-4. The sample form indicates that you are adding a record6.2.3 DiscussionThe NewRecord property is simple: its value is True when adding a new record and False otherwise. This property is True from the moment the pending new record becomes current until the moment the record is saved. NewRecord is reset to False right after the BeforeUpdate event; it is False during both the AfterUpdate and AfterInsert events. The image control used to display the add/edit icon uses a trick to change its picture quickly. Rather than loading a bitmap image from a disk file, which would be slow, it copies the picture from one of two hidden "source" image controls on the form. To do this, set the image control's PictureData property to the value of the PictureData property of another image control. Chapter 9 discusses the PictureData property in more detail. |
[ Team LiB ] |