Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2004
    Location
    Walnut Cove, NC
    Posts
    101

    Unanswered: AfterUpdate and set focus

    I have a text box in a form called Date Required. I am checking the date to make sure it is not in the past by using an AfterUpdate procedure. I have an If statement that checks to see if the date is in the past. If it is, I display a message box, blank out the incorrect date and set focus on the same field so that the cursor stays in the field for them to re-enter the correct date. However, the cursor jumps to the next field instead of staying in the date required field. How can I get the cursor to go back to the date field?


    Thanks,

    SBR

  2. #2
    Join Date
    Mar 2004
    Location
    Nashville, TN
    Posts
    557

    Since you havent posted your code...

    Since you have posted any code up here I would have to say you need to remove the part about setting the focus.

    When you pop up your message box and the user clicks okay, the focus stays set to the current box. So when you are setting the focus it must send it to the next since it was already set on that txt box.

    If removing that part doesnt help then please post your code on here and we will try and help to the best of our knowledge.

    Thanks,
    JS
    Have you ever thought about thinking on purpose?

    Jarvis Stubblefield
    Patriot Designs
    Web/Database Development and Consulting
    The-Patriot.net by Patriot Designs

  3. #3
    Join Date
    Jan 2004
    Location
    Walnut Cove, NC
    Posts
    101

    Re: Since you havent posted your code...

    I tried it without set focus also. The code is very simple. It looks like this:

    Private Sub txtDateReq_AfterUpdate()
    'Test date

    If txtDateReq < Date Then
    MsgBox "Date Required cannot be in the past", vbOKOnly, "Date Error"
    txtDateReq = ""
    End If

    End Sub

    Is it because I type the date in the field and press the tab key to go to the next field. The Afterupdate checks the date and if it is in the past displays my message, sets the field to blanks, and then performs the tab going to the next field? If that's the problem how can I perform a shift tab in the AfterUpdate procedure? Or should I use another method to check the date?

    Thanks for your help

    SBR

  4. #4
    Join Date
    Mar 2004
    Location
    Nashville, TN
    Posts
    557
    Okay, I have done some similar stuff... I will post it here.

    Code:
    Private Sub yourfieldname_Change()
        If txtDateReq < Date Then
            MsgBox "Date Required cannot be in the past", vbOKOnly, "Date Error"
            txtDateReq=""
        End If
    End Sub
    See if that works, because it doesnt allow you to leave the field yet. (I think).

    If that doesnt work we will figure something else out.



    JS
    Have you ever thought about thinking on purpose?

    Jarvis Stubblefield
    Patriot Designs
    Web/Database Development and Consulting
    The-Patriot.net by Patriot Designs

  5. #5
    Join Date
    Jan 2004
    Location
    Walnut Cove, NC
    Posts
    101

    AFTERUPDATE AND SET FOCUS

    I tried using the Change() instead of AfterUpdate, but it doesn't work. I also tried using BeforeUpdate and it doesn't work either.

    Is there a way to have it shift tab back into the field?


    SBR

  6. #6
    Join Date
    Apr 2004
    Location
    Derbyshire, UK
    Posts
    789
    Provided Answers: 1

    Afterupdate setfocus

    Hi SBR

    I have had many an hour sorting data validation and usualy come up with different solutions every time !!

    I think you can use the beforeupdate as follows

    Private Sub txtDateReq_BeforeUpdate(Cancel As Integer)

    If IsNull(txtDateReq) Then Exit Sub

    'Test date
    If CDate(txtDateReq) < Date Then
    MsgBox "Date Required cannot be in the past", vbOKOnly, "Date Error"
    Cancel = True
    'txtDateReq = ""
    End If


    End Sub

    By setting Cancel=True the update is cancelled and focus remains where it is.

    However you cannot set the date to blank IN this event (makes it recusive ?)

    You can cancel the new date entered by the Escape key to return to the value that existed before the new date was entered (if that makes sense) but then you need to check for null value if this leaves the date blank.

    On a personal note, if there are more than a couple of data entries to be checked/validated I usually do all this in a separate function calle by some other event(s) such as form beforeupdate or Close button/save button etc. and set the focus to what ever is not valid. Just an idea.

    Hope this helps.

    MTB

Posting Permissions

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