ACC: Commands Not Available During BeforeUpdate Event

ID: Q128195


The information in this article applies to:


SYMPTOMS

Advanced: Requires expert coding, interoperability, and multiuser skills.

When you perform an action in the BeforeUpdate event, you receive the following error messages, even if you have canceled the event.

In Microsoft Access 7.0 and 97:

- Runtime error '2105'
You can't go to the specified record

- Runtime error '2115'
The macro or function set to the BeforeUpdate or ValidationRule property for this field is preventing Microsoft Access from saving the data in the field.

In Microsoft Access 2.0:
- Field or record can't be saved while it's being validated.

- Command not available: UndoCurrentRecord.

- Can't go to specified record.


CAUSE

Microsoft Access cannot perform the specified command because the BeforeUpdate event procedure is still running. This is true even if the Cancel parameter is set to True or if a CancelEvent macro attempts to cancel the action.

These error messages are generated by the following statements in the BeforeUpdate event procedure:

' Undo the current record. (Microsoft Access 2.0 only)

' NOTE: This behavior no longer occurs in Microsoft Access 7.0 and 97)

DoCmd DoMenuItem A_FORMBAR, A_EDITMENU, 1

' Goto a new record.
DoCmd.GoToRecord , , acNewRec '(Microsoft Access 7.0 and 97)
DoCmd GoToRecord , , A_NEWREC '(Microsoft Access 2.0 )

' Find a record
DoCmd.FindRecord "Smith", , True, acAll, True '(Microsoft Access 7.0 and 97)
DoCmd FindRecord "Smith", , True, A_ALL, True '(Microsoft Access 2.0)


RESOLUTION

Instead of setting the Cancel parameter to True, or using the CancelEvent action, use a SendKeys statement if that is an option with the command you are trying to use. The SendKeys statement sends keystrokes to the Microsoft Access buffer, where they wait until the BeforeUpdate event procedure is finished.


MORE INFORMATION

To go to a new record before the BeforeUpdate event procedure is finished, use the following code for the BeforeUpdate property's event procedure:


   Sub Form_BeforeUpdate (Cancel As Integer)
     Dim Msg As String

     Msg = "Would you like to go to a new record?"
     If MsgBox(Msg, 32 + 4) = 6 Then
       ' SendKeys "%egw" '(for Microsoft Access 7.0 and 97)
       ' SendKeys "%rgw" '(for Microsoft Access 2.0)
     End If

   End Sub 

NOTE: In some cases, you will need to include the CancelEvent action along with the SendKeys statement as in the following:

To undo the current record (in Microsoft Access 2.0 only), in place of the following line to undo the current record

   DoCmd DoMenuItem A_FORMBAR, A_EDITMENU, 1 

use the SendKeys statement along with the CancelEvent action:

   Docmd CancelEvent
   SendKeys "{ESC}{ESC}" 

To navigate to a new record, instead of the following code

   DoCmd.GoToRecord , , acNewRec  '(for Microsoft Access 7.0 and 97)
   DoCmd GoToRecord , , A_NEWREC  '(for Microsoft Access 2.0) 

use the following SendKeys statement:

   SendKeys "%egw" '(for Microsoft Access 7.0 and 97)
   SendKeys "%rgw" '(for Microsoft Access 2.0) 

For an additional example of how to use this technique, please see the following article in the Microsoft Knowledge Base:

Q115189 ACC2: How to Find a Record Using a Bound Control

Steps to Reproduce Behavior


  1. Open the sample database Northwind.mdb (or NWIND.MDB in Microsoft Access 2.0).


  2. Open the Customers form in Design view.


  3. Set the form's BeforeUpdate property to the following event procedure:
    
          Sub Form_BeforeUpdate (Cancel As Integer)
          Dim Msg As String
              Msg = "Would you like to go to a new record?"
              If MsgBox(Msg, 32 + 4) = 6 Then
                 ' User chose Yes so undo the current record.
                  DoCmd.GoToRecord , , acNewRec '(for Microsoft Access 97 only)
                 ' In Microsoft Access 2.0 use the following line instead:
                 ' DoCmd GoToRecord , , A_NEWREC
              End If
          End Sub 


  4. View the form in Form view.


  5. Change the data in any field, and then click Save Record on the Records menu.


  6. When you are prompted "Would you like to go to a new record?" click Yes. Note that you receive the following error message.

    In Microsoft Access 7.0 and 97:
    Runtime error '2105'
    You can't go to the specified record
    In Microsoft Access 2.0:
    Can't go to specified record



REFERENCES

For more information about SendKeys, search for "SendKeys Statement" using the Microsoft Access Help Index.


Keywords          : kberrmsg kbusage FmsEvnt 
Version           : 2.0 7.0 97
Platform          : WINDOWS 
Issue type        : kbprb 

Last Reviewed: April 9, 1999