OnCurrent Opening Existing Records as Read Only

The On Current event property triggers each and every time a record is accessed. Thus, adding a Procedure to this event will increase the load time for the user, so utilize this procedure with caution. If the user is accustomed to instantaneously browsing through multiple records, and you add a procedure that causes a noticeable delay, expect to receive a nasty e-mail.

For this example, suppose that you want to always start a record in a read-only state, and when the user is ready to edit the record, he or she must click a command button. The idea of this example is that you want to protect the user from accidentally making a change, or worse yet, an unauthorized deletion. This methodology could be implemented as a part of your security system, as only authorized users can see the Edit command button.

To do this, the On Current procedure can be used to set the form into the read-only state. A command button and its On Click procedure can be used to prepare the form to allow data modifications.

Private Sub Form_Current()

'When user navigates to an existing record, disable the 'ability to Add, Edit, and Delete.

'For new records, enable all modification abilities. Dim fStatus As Boolean

'Determine if user is on the NewRecord fStatus = Me.NewRecord

'Set the modification abilities for the Form Me.AllowEdits = fStatus Me.AllowDeletions = fStatus cmdEdit.Enabled = Not fStatus End Sub

This procedure would be associated to a Form's On Current event property. It would execute every time the user moves to a different record. The NewRecord property is examined to determine if the user is on the New Record. In Access all new records are added at the New Record. It can be accessed by clicking on the New Record button on the record navigation bar, or going one record past the last existing record.

The NewRecord property of the Me object returns a Boolean value that signifies if the user is currently on the New Record. If the user is on this record, then allow them to Add, Edit, or Delete the record. Otherwise, it would mean that they are on an existing record, so restrict their editing capabilities.

The AllowEdits and AllowDeletions are Form (Me) properties that specify whether or not the user can edit and delete records. Properties similar to these are AllowAdditions and DataEntry. The AllowAdditions property specifies if new records can be added. The DataEntry property sets the form into a mode in which the user can only enter new records or edit the records that they have just entered.

It stands to reason that for the completion of this example, there would need to be an Edit command button that would set the edit capabilities to allow edits to occur. (If you're not familiar with the On Click event, please review the sample code for it.) There isn't any logic needed for this event procedure, for a couple of reasons. First, if the user is on the New Record, then the Edit button is not enabled (or disabled). When the user clicks the Edit button, it is assumed that he or she only wants to do one thing, which is to gain the ability to edit the record.

Private Sub cmdEditRecord_Click() 'When user clicks this button

'Disable the Edit Button, Enable the Edit capabilities

'Set the modification abilities for the Form Me.AllowEdits = True Me.AllowDeletions = True

'It is not possible to disable the active control. 'Doing so will result in an error. To avoid the error, 'set the focus to any another control. txtFirstName.Setfocus cmdEdit.Enabled = False End Sub

This example requires that a field called txtFirstName exist on the form. When the user clicks the button, the AllowEdits and AllowDeletion properties would be turned on, and after passing the control to the First Name field, the Edit button would be disabled.

0 0

Post a comment