Results 1 to 5 of 5

Thread: SetFocus event

  1. #1
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Winnipeg, Canada
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    SetFocus event

    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?

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: SetFocus event

    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.
    Charlotte

  3. #3
    2 Star Lounger
    Join Date
    Jan 2001
    Location
    Winnipeg, Canada
    Posts
    109
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SetFocus event

    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....[img]/w3timages/icons/scream.gif[/img]

  4. #4
    New Lounger
    Join Date
    Dec 2000
    Location
    Rockhampton, Queensland, Australia
    Posts
    23
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: SetFocus event

    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

  5. #5
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: SetFocus event

    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:

    <pre>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</pre>

    Charlotte

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •