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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Onlostfocus - how to override/defeat 1

Status
Not open for further replies.

toccata

Programmer
Jun 26, 2002
65
0
0
US
I have a text box that sets focus to one of two controls on a subform on losing focus. The form also has a cmd button (to exit the form). If it is clicked when the text box has focus, the onlostfocus property moves focus to the subform control rather than exitting. Any way to defeat the onlostfocus?
 
Yes, that is the fly in the ointment with the lost focus event. A better way to go might be to trap the Enter and Tab keystrokes in the text box where you have placed the lost focus code. Here's how I would do it:

1) Set the "Key Preview" property for the form to "yes".

2) Create an event procedure in the "On Key Down" event of your text box. It will look something like this:

Code:
Private Sub txtMyTextbox_KeyDown(KeyCode As Integer, Shift As Integer)
If KeyCode = vbKeyTab Or KeyCode = vbKeyReturn Then
    Select Case Me!txtMyTextbox.Text
        Case "the value"
            'set focus in some field
        Case "the other value"
            'set focus in some other field
        Case Else
            'maybe an error MsgBox
            KeyCode = 0   'throw away the keystroke
    End Select
End If
End Sub

See how that works for you...

Ken S.
 
Alternatively,
You could 'hide' the command button until such time as the 'Lostfocus' event has finished. This would prevent the user from clicking it to begin with.

I very often hide my 'control box' and 'close' buttons until I am happy that the user has done everything they ought.

Just a suggestion.

PassingBy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top