Home All Groups Group Topic Archive Search About

Lost_Focus event of controls doesn't fire

Author
21 Oct 2005 1:09 PM
Abdhul Saleem
Hi,

In my form there is one Combo box, text box and Command button.

Command button's caption is "&Save". I have also given a hot key "F9" to the
button.

In the lost focus event of Combo & text boxes I am validating the values
given from the respective controls.

When I click Save button using mouse, the lost focus event of the controls
fires and the validation is done properly.

But When I press F9 or ALT+S to save the details, the lost focus event of
the controls doesn't fire and the validation is not done.

In the Form_KeyDown event I trapped F9 key and found the Active Control and
called the Lost_Focus event of the active control and did the validation.

But I could not trap ALT+S to the validation.

Please give a solution to solve this scenario.

Regards,
M. Abdhul Saleem.

Author
21 Oct 2005 2:13 PM
Jan Hyde
"Abdhul Saleem" <AbdhulSal***@discussions.microsoft.com>'s
wild thoughts were released on Fri, 21 Oct 2005 06:09:02
-0700 bearing the following fruit:

>Hi,
>
>In my form there is one Combo box, text box and Command button.
>
>Command button's caption is "&Save". I have also given a hot key "F9" to the
>button.
>
>In the lost focus event of Combo & text boxes I am validating the values
>given from the respective controls.

That's an unrliable and user unfriendly way of doing
validation.

>When I click Save button using mouse,

This is the point where I would do my validation.

>the lost focus event of the controls
>fires and the validation is done properly.

Some of the time....

>But When I press F9 or ALT+S to save the details, the lost focus event of
>the controls doesn't fire and the validation is not done.

That's why MS added a validate event in VB6, just take my
advice and write a validation routine that you call when the
user clicks save.



Jan Hyde (VB MVP)

--
If you have a lot of tension and you get a headache,
do what it says on the aspirin bottle:
Take two and keep away from children.  (Barbie Jo)

[Abolish the TV Licence - http://www.tvlicensing.biz/]
Author
21 Oct 2005 2:20 PM
Ken Halter
Show quote Hide quote
"Abdhul Saleem" <AbdhulSal***@discussions.microsoft.com> wrote in message
news:2CC0EAC9-45C4-4418-A7A6-60AFE55649B9@microsoft.com...
> Hi,
>
> When I click Save button using mouse, the lost focus event of the controls
> fires and the validation is done properly.
>
> But When I press F9 or ALT+S to save the details, the lost focus event of
> the controls doesn't fire and the validation is not done.
>
> But I could not trap ALT+S to the validation.
>
> Please give a solution to solve this scenario.
>
> Regards,
> M. Abdhul Saleem.

Have you experimented with the Validate event? You can force that to fire,
regardless of which control has focus by calling the ValidateControls
method....

This needs a new project with a couple of textboxes and a command button.
'=============
Option Explicit

Private Sub Command1_Click() 'this could be the Save button
   ValidateControls
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   On Error Resume Next
   ValidateControls
   If Err.Number <> 0 Then
      Cancel = True
   End If
   Err.Clear
End Sub

Private Sub Text1_Validate(Cancel As Boolean)
   If Text1.Text <> "Valid" Then
      MsgBox "Text1 contents not valid"
      Cancel = True
   End If
End Sub


Private Sub Text2_Validate(Cancel As Boolean)
   If Text2.Text <> "Valid" Then
      MsgBox "Text2 contents not valid"
      Cancel = True
   End If
End Sub
'=============

Another way is to validate all controls when the user hits Save. That's
actually better imo. Let the user type whatever they want and tell them
where they went wrong before saving.


--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm
Please keep all discussions in the groups..
Author
21 Oct 2005 3:17 PM
Charlie
"Ken Halter" wrote:

>
> Another way is to validate all controls when the user hits Save. That's
> actually better imo. Let the user type whatever they want and tell them
> where they went wrong before saving.
>

I always preferred the "validate-as-you-go" myself.  I thought it annoying
to hit Save, get the "fix this" message, hit Save again, get the "now fix
that", etc., especially when the choices further down the form depend on
what's been entered prior.  But it really depends on the application.
Author
21 Oct 2005 4:02 PM
Jan Hyde
"Charlie" <Char***@discussions.microsoft.com>'s wild
thoughts were released on Fri, 21 Oct 2005 08:17:03 -0700
bearing the following fruit:

>
>"Ken Halter" wrote:
>
>>
>> Another way is to validate all controls when the user hits Save. That's
>> actually better imo. Let the user type whatever they want and tell them
>> where they went wrong before saving.
>>
>
>I always preferred the "validate-as-you-go" myself.  I thought it annoying
>to hit Save, get the "fix this" message, hit Save again, get the "now fix
>that", etc., especially when the choices further down the form depend on
>what's been entered prior.  But it really depends on the application.

Your a mouse user.



Jan Hyde (VB MVP)

--
The man who invented the Hokey-Pokey died over a month ago, but the undertaker is still trying to get him into the coffin.
He puts his right foot in..."

[Abolish the TV Licence - http://www.tvlicensing.biz/]