Results 1 to 6 of 6
  1. #1
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286

    SET focus back to TEXTBOX if validation does NOT pass


    hello, i have a textbox with a validation in the "lost focus" property. if the text the user inputs in the textbox is incorrect, it clears out the textbox. but, for some reason after the validation message box appears for the user, the cursor moves to the next textbox. how do i get the cursor to go back to the original textbox with the validation? thanks

    basically, until the user enters a correct string, the cursor will not move to the next textbox. thanks!

  2. #2
    Join Date
    Nov 2011
    Location
    Hamilton, NZ
    Posts
    18
    sendkeys "+{TAB}"

  3. #3
    taimysho0 is offline Competent Performer
    Windows XP Access 2000
    Join Date
    Nov 2011
    Posts
    286
    sorry im not too familliar with vb code, so i can put that in the "lost focus" attribute of my textbox and it will bring the cursor back to the original textbox? for example

    if isnull(me.txtbox) then
    msgbox ("incorrect username")
    sendkeys "+{TAB}"

  4. #4
    Join Date
    Nov 2011
    Location
    Hamilton, NZ
    Posts
    18
    i usually use sendkeys when the cursor does not get back to a specified textbox
    If text1.setfocus does not work, you can try sendkeys.

    In your case,if the below code does not work, try Option 2

    Option1:

    if isnull(me.txtbox) then
    msgbox ("incorrect username")
    text1.setfocus
    endif

    Option2:
    if isnull(me.txtbox) then
    msgbox ("incorrect username")
    sendkeys "+{TAB}"
    endif

  5. #5
    nicknameoscar is offline Advanced Beginner
    Windows XP Access 2000
    Join Date
    Apr 2011
    Location
    Earlysville, VA
    Posts
    91
    If your validation consists of making sure your textbox is not null (as your comments above would indicate) then perhaps you could use the On Exit event instead of the On Lost Focus event. The On Exit event allows you to Cancel the event.

    If isnull(me.txtbox) Then
    MsgBox ("incorrect username")
    Cancel = True
    End If

    If you are using some other form of validation and you have a situation where you 'might' want to tab past this particular textbox then the below might be necessary:

    If (your validation condition here) = False Then
    MsgBox ("incorrect username")
    Cancel = True
    Else If IsNull(Me.YourTextBox) Then
    Cancel = False
    End If

    I did not test the above. Just thinking out loud.

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Validation is usually done in the BeforeUpdate event of a control and Cancel = True set to hold the focus in the control. The Exit event is useful as someone already stated, to catch people skipping by a control.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. On click textbox inserts value back on combobox
    By whitelockben in forum Forms
    Replies: 2
    Last Post: 10-09-2011, 05:23 AM
  2. Load textbox from another textbox
    By siddel77 in forum Programming
    Replies: 12
    Last Post: 08-30-2011, 01:46 PM
  3. Display PASS or FAIL using Unbound textbox
    By Shambler2 in forum Reports
    Replies: 7
    Last Post: 06-02-2011, 11:19 AM
  4. Textbox data validation rules.....
    By smorelandii in forum Access
    Replies: 1
    Last Post: 02-01-2011, 09:52 PM
  5. Replies: 1
    Last Post: 05-29-2010, 02:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums