Results 1 to 8 of 8

Thread: Tab Stop

  1. #1
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts
    I want the tdate control to have the focus after I press OK from the message box. Instead the next control in the tab order gets the focus after I press OK. Am I missing something in my code?
    Private Sub TDate_AfterUpdate()
    If [tdate] < Date - 30 Then
    MsgBox "Date is not between today's date and 1 month ago", vbInformation
    Cancel = True
    Me.tdate.Value = Null
    Me.tdate.SetFocus
    Exit Sub
    Else
    If [tdate] > Date Then
    MsgBox "Date is in the Future"
    Me.tdate.SetFocus
    Cancel = True
    Me.tdate.Value = Null
    Exit Sub
    End If
    End If
    End Sub
    Thanks for looking.
    John

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Cancel is not an argument in the After Update event procedure. You should use the Before Update procedure instead:

    Private Sub TDate_BeforeUpdate(Cancel As Integer)

    Just setting Cancel to True is sufficient to leave the focus on the TDate control, you shouldn't use SetFocus.

  3. #3
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='761443' date='25-Feb-09 12:01']Cancel is not an argument in the After Update event procedure. You should use the Before Update procedure instead:

    Private Sub TDate_BeforeUpdate(Cancel As Integer)

    Just setting Cancel to True is sufficient to leave the focus on the TDate control, you shouldn't use SetFocus.[/quote]
    Ok, that is better, but it would not allow me to set the control to null. I get an error stating that macro is preventing Access from saving the data in the field.
    I tried .undo but that didn't clear the field either. Is there another way to clear the control?
    Thanks.

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    You can't change the value of a control in its Before Update event. I'd simply remove the line that sets the value to Null, and leave it to the user to correct (or clear) the value.

  5. #5
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts
    [quote name='HansV' post='761476' date='25-Feb-09 12:22']You can't change the value of a control in its Before Update event. I'd simply remove the line that sets the value to Null, and leave it to the user to correct (or clear) the value.[/quote]
    OK. Thank you for your help.

  6. #6
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Since you're incorrigible, I thought you might still harbor a secret urge to set the control's value to Null.

    One possible (more complicated) approach that might work for you would be to define a form-level variable (at the top of the form module), like this:
    [pre]Dim fRefocusOnTDate As Boolean[/pre]
    Then, in the TDate_AfterUpdate event, if the date's no good, (1) show the MsgBox, (2) set TDate to Null, and also (3) add this line:
    [pre]fRefocusOnTDate = True[/pre]
    Then, in the GotFocus event of whatever control follows TDate in the tab order, put the following:
    [pre]If fRefocusOnTDate = True Then
    fRefocusOnTDate = False
    Me!TDate.SetFocus
    End If[/pre]

  7. #7
    2 Star Lounger
    Join Date
    Nov 2002
    Location
    Mt. Olive, North Carolina, USA
    Posts
    170
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Post

    Thanks for the suggestion. I printed it out so I can take a look at it and try it later.
    And YES I am still incorrigible. LOL

  8. #8
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts
    As a follow-up, note that my approach leads to a less-than-perfect result if the AfterUpdate event is triggered by the user clicking on another control (other than the next control in the tab series) -- rather than hitting the Enter or Tab key or clicking on the next-tab-stop control. In that case your message will still display and the value of TDate will still be set to Null, but the focus won't revert back to TDate. (Also: in that scenario the focus will jump to TDate the next time the user does anything to put the focus on the next-tab-stop control.)

    To fix this, you could take the code for the GotFocus event of the next-tab-stop control and also add it to the GotFocus event of all the other controls on the form (other than TDate).

Posting Permissions

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