Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Force a text box to retain focus

Status
Not open for further replies.

osera

Technical User
May 15, 2006
35
US
thread702-1559904

Good afternoon. I am having an issue very much like (perhaps the same) as in the referenced thread.

My form has 'yes' or 'no' option buttons in a frame. If the user checks the 'no' button, focus is sent to a text box (me.MER_Comments) where the user has to enter comments. If the user tries to leave the comments text box without entering something, I want the form to force the focus back to that text box.

Here's my code:

Code:
If Me.Frame_Primary_MER.Value = 2 Then
    If IsNull(Me.MER_Comments.Value) Then
        MsgBox "Please enter your comments.", , "Explanation"
        Cancel = True
        Me.MER_Comments.SetFocus
    End If
End If

I tried putting this in the BeforeUpdate event, as suggested in the original thread, but I get the same result. No message box and the focus moves on to whatever was selected next. If I put the code in the LostFocus event, I get the message box, but the focus doesn't stay in the comments text box.

Thank you in advance for guiding me to the correct path.
 
How are ya osera . . .

In the thread you reference, its the forms [blue]Before Update[/blue] event thats used. This allows the user to edit and navigate freely until an attempt to save a record is made ... [blue]this is when the event triggers![/blue] Its also the place where most field validation is done.

However in your code I'd change:
Code:
[blue]   If Trim(Me.MER_Comments & "") = "" Then[/blue]
If you absolutely must lock focus on [blue]MER_Comments[/blue] then try the following in the [blue]On Exit[/blue] event of [blue]MER_Comments[/blue]:
Code:
[blue]   If Me.Dirty Then [green]'Record in Edit Mode![/green]
      If Me.Frame_Primary_MER.Value = 2 Then
          If Trim(Me.MER_Comments & "") = "" Then
              MsgBox "Please enter your comments.", , "Explanation"
              Cancel = True
          End If
      End If
   End If[/blue]
Be sure to rem out or remove the code in the [blue]Before Update[/blue] event before trying [blue]On Exit[/blue].

See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Thanks, TheAceMan1

Putting the "If me.dirty...." code in the On Exit event is giving me the function I want. I'd prefer to have my users commenting right after they check the 'no' box, in hopes that their reasons will be forefront in their minds.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top