Using LostFocus Events with Command Button Properties

If you’re using LostFocus events but would also like to set the Defaultor Cancel properties for command buttons, you’ll run into a confusing situation.Those properties trigger the Click event when the user presses either Enteror Escape without ever transferring focus to the command button.To trigger the “missing” LostFocus, you need to explicitly transferfocus yourself, and then call DoEvents to resequence to a VB chain of events.(Don’t be scared by the naysaying over DoEvents–it’s extremely usefulin this situation and can cause no ill effects!) Use code similar to this:

 Sub Command1_Click () Command1.SetFocus DoEvents If Not (ActiveControl Is Command1) Then 'Focus was transfered elsewhere 'by validation code Exit Sub Else 'Proceed with Click event code End If End Sub 
Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: