+ Reply to Thread
Results 1 to 5 of 5

set focus on txt field

  1. #1
    Forum Contributor
    Join Date
    05-04-2005
    Posts
    136

    Question set focus on txt field

    I have a form with a txt box named txtAuditor. on the exit event, i have some validation checks.
    i want that if the validation fails, the focus will go back to that txt box.

    i currectly have

    if variable=false then
    txtauditor.value=""
    txtauditor.setfocus
    endif

    this did not work!
    Any suggestions??

  2. #2
    Registered User
    Join Date
    09-26-2005
    Posts
    12
    hm... i think you may have to preselect the value that's already there-
    i know you've set it to " ", but maybe you have to select it?
    give this a go-

    if variable=false then
    txtauditor.value="" 'resets incorrect value - may not be necessary?
    txtauditor.setfocus 'sets the focus to the text box
    txtauditor.selStart = 0 'starts the text selection at the start of the incorrect value
    txtauditor.selLength = 1000 'indicates the last character of the incorrect value (really big, so youre sure you have it- although with the top value being reset- maybe you could error on the side of some smaller number?)
    endif

    lemme know if it works,
    good luck
    pim

  3. #3
    Forum Contributor
    Join Date
    05-04-2005
    Posts
    136
    txtauditor.setfocus 'sets the focus to the text box

    this is the line that doesn't work.

    i got around it by setting the cancel to true but i cant figure out why it's not letting me use the setfocus method.

  4. #4
    Tom Ogilvy
    Guest

    Re: set focus on txt field

    You said you are using the Exit event, so use the functionality provided:

    if variable=false then
    Cancel = True
    End if

    causes focus to remain on the textbox.

    --
    Regards,
    Tom Ogilvy


    "tkaplan" <[email protected]> wrote in
    message news:[email protected]...
    >
    > txtauditor.setfocus 'sets the focus to the text box
    >
    > this is the line that doesn't work.
    >
    > i got around it by setting the cancel to true but i cant figure out why
    > it's not letting me use the setfocus method.
    >
    >
    > --
    > tkaplan
    > ------------------------------------------------------------------------
    > tkaplan's Profile:

    http://www.excelforum.com/member.php...o&userid=22987
    > View this thread: http://www.excelforum.com/showthread...hreadid=471137
    >




  5. #5
    Jana
    Guest

    Re: set focus on txt field

    TK:

    The SetFocus doesn't work because txtAuditor already HAS the focus and
    is in the middle of leaving. It doesn't actually lose the focus until
    AFTER the exit event has finished. The order of events when you change
    data in a control is as follows:

    BeforeUpdate
    AfterUpdate
    Exit
    LostFocus

    Note that you don't lose the focus until AFTER the event has happened.
    You have to use the Cancel = True method because that cancels the exit
    event, thus you don't leave the txtAuditor field & the focus never
    changes to the next field. Does that make sense??

    What you should be using for data validation is the 'Before Update'
    event. This will let you check the data BEFORE it gets written to the
    record. Then, because the record hasn't been changed yet, you just use
    the Undo method for your field if it fails your validation
    requirements, rather than setting it to "".

    Here's the finished code:
    Private Sub txtAuditor_BeforeUpdate(Cancel as Integer)
    'Your Validation Code Goes Here, which sets the value of variable
    If variable = false then
    Cancel = True
    Me!txtAuditor.Undo
    End If
    End Sub

    Now, make sure that your validation code tells the user WHY you aren't
    allowing them to change the data and you're all set!

    Good luck,
    Jana


+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1