• SetFocus event

    Author
    Topic
    #352296

    I am stumped (already!).
    This is my first Access prog, my first form and my first field on that form. It is an unbound form, with 10 entry fields and 2 buttons, one to check/save the data.

    When the AfterUpdate event occurs for Field1 (key data), I am checking a)is the field blank (if so display msgbox to enter data) else if not blank, see if record already exists. (if so, msgbox informs user). Two additional events occur after: Exit and LostFocus.
    What I want to happen is: When the user needs to enter a number (a) or another number (, I want focus to return to (or remain in) Field1. To no avail, no matter where I place the Field1.SetFocus method in these events (LostFocus seems most likely place), the focus jumps to Field2, the next tab stop.
    What am I doing wrong?

    Viewing 0 reply threads
    Author
    Replies
    • #511902

      The event you need is the BeforeUpdate event of the control rather than the AfterUpdate. BeforeUpdate happens after something has been entered or changed but before the data is actually written to the field, and BeforeUpdate has a Cancel argument. So you do your test in the BeforeUpdate event and if the value entered is invalid, you set Cancel = True, and the focus will stay right there on that control. Just add a message box to let the user know what is wrong so they won’t think the thing is broken.

      Unfortunately, Access won’t let you set the focus to the control losing the focus, so the LostFocus event won’t work for testing the field for a null value and BeforeUpdate doesn’t apply if all they’re doing is tabbing to the next field. What you need instead is the OnExit event, which has a Cancel argument.

      • #512282

        Thanks Charlotte, setting Cancel=true in the BeforeUpdate event works.

        I chose not to use the OnExit event, because that makes the process too restrictive. Since Field1 (name for purpose of the post) gets focus when the form opens, the event is being triggered even if the user wishes to immediately close the form again (or fill any other field). In the spirit of event programming, the user should be able to roam around the form as desired or do nothing. Only when the user decides to SAVE the data, if incomplete, will he/she be directed to the offending field(s).

        Even the BeforeUpdate event is a problem as soon as you make the slightest change to the field. Since neither Null nor DataAlreadyExists is acceptable, you need to enter some dummy data to get past the tests (to close the form, say).

        I am going crazy….

        • #512300

          If you don’t want the user to change an existing record, you could use the NewRecord property to check for an existing record, display a message box to infrom the user, and then use the OldValue property to reset to original value in the OnChange Event.
          There’s probably better ways of doing this, but its the first thing that came to my mind.
          Neville

        • #512372

          In that case, use the BeforeUpdate event of the Form itself. This happens before you actually save any changes to a record and it’s a good place to call a routine that examines all your controls to see that all values are valid and all required fields are filled in. If one of them isn’t, Cancel the BeforeUpdate event, same as a control, set the focus to the problem control and pop a message box giving the user instructions. Here’s an example:

          Private Sub Form_BeforeUpdate(Cancel As Integer)
            If Not IsDate([txtShippedDate]) Then
              Cancel = True
              MsgBox "You must enter a Shipped Date"
              [txtShippedDate].SetFocus
            End If
          End Sub
    Viewing 0 reply threads
    Reply To: SetFocus event

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: