Results 1 to 6 of 6
  1. #1
    New Lounger
    Join Date
    Jan 2003
    Location
    Victoria, Br. Columbia, Canada
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can't set focus to form control (2002)

    After entering a Start and Stop date on a record in a subform, I want to validate that the Stop date is greater than the Start date. If it isn't, I want the use to be sent back to the Stop date to re-enter it correctly.

    This is the code I've used, however, after the message box OK button is clicked, the focus does not go to the STOPDATEcontrol, it goes to the next control in the tab sequence. I tried to trick it by first sending it to the STARTDATEcontrol, and then to the STOPDATEcontrol, and it works, but the date in the STOPDATEcontrol is not highlited, forcing me to delete it before entering a new date. If I send the focus to any other control on the subform, it works--it just won't work for STOPDATE.

    Why is it doing this to me? Is AfterUpdate not the correct event?

    Private Sub STOPDATE_AfterUpdate()
    'Check to make sure the Stop date is greater than the Start Date

    If Me![STOPDATE] < Me![START] Then
    MsgBox "The Stop date must be greater than the Start date. Please check your entry and try again.", vbOKOnly
    Me![STOPDATE].SetFocus
    End If

    End Sub

  2. #2
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Can't set focus to form control (2002)

    The key to this is that the control you are setting focus to is on a subform. First reference the form and then the subform such as

    Me.Form.Formname.form.subformname.StopDate.Setfocu s (... or something like this...)
    HTH
    Regards,

    Gary
    (It's been a while!)

  3. #3
    5 Star Lounger
    Join Date
    Jul 2002
    Location
    Toronto, Ontario, Canada
    Posts
    1,139
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't set focus to form control (2002)

    If you use the BeforeUpdate Event, you can cancel the update and the focus doesn't leave the control

    If Me![STOPDATE] < Me![START] Then
    MsgBox "The Stop date must be greater than the Start date. Please check your entry and try again.", vbOKOnly
    Cancel = True
    End If

    If you want to select the text in the textbox then add this code before the End If
    me![STOPDATE].SelStart = 0
    me![STOPDATE].SelLength = len(me![STOPDATE].Text)
    --
    Bryan Carbonnell - Toronto <img src=/S/flags/Ontario.gif border=0 alt=Ontario width=30 height=18> <img src=/S/flags/Canada.gif border=0 alt=Canada width=30 height=18>
    Unfortunately common sense isn't so common!!
    Visit my website for useful Word, Excel and Access code, templates and Add-Ins

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

    Re: Can't set focus to form control (2002)

    That would only be necessary if you were referring to the control on the subform from the parent form. The BeforeUpdate and AfterUpdate events of the control are in the subform's module, not the parent form's, so the Me!ControlName syntax is appropriate here.
    Charlotte

  5. #5
    New Lounger
    Join Date
    Jan 2003
    Location
    Victoria, Br. Columbia, Canada
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Can't set focus to form control (2002)

    Using the BeforeUpdate event worked great, thanks to all.

    J

  6. #6
    Silver Lounger GARYPSWANSON's Avatar
    Join Date
    Aug 2001
    Location
    Frederick, Maryland, USA
    Posts
    1,788
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Can't set focus to form control (2002)

    ... Thanks for keeping me on the straight and narrow.... <img src=/S/dizzy.gif border=0 alt=dizzy width=15 height=15>
    Regards,

    Gary
    (It's been a while!)

Posting Permissions

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