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

Lost Focus event 1

Status
Not open for further replies.

mrsbean

Technical User
Jul 14, 2004
203
US
I have a Lost focus event that I want to cancel if the user selects either cmdDeleteRecord button or cmdSaveRecord button. I looked at thread705-1014088, but the "name" part of this reference wasn't available to me.

At the beginning of the Lost Focus event, I wanted to do:

Code:
If Me.ActiveControl.name = "cmdDeleteRecord"  or Me.ActiveControl.name = "cmdSaveRecord" Then
Exit Sub
End If

Maybe I need an additional reference? I don't know what I need. It won't let me use "name".

Thanks in advance.

MrsBean


 
Do you get an error? I use the same thing and it works for me.
 
No error, jadams0173, it just doesn't do anything.

MrsBean
 
What is the result when you do

[tt]debug.print me.activecontrol.name[/tt]

and check the result in the immediate pane (ctrl+g)?

Roy-Vidar
 
You may also try to enclose both arguments in (). Long shot...but sometimes that works.
 
RoyVidar,

That was helpful - it showed that the text box for which the Lost Focus event was written has focus.

What I want to accomplish is to stop the Lost Focus event if the Delete button or the Save button was clicked.

Linda
 
I suspected you were trying to use a controls lost focus, and I don't think it will work (the referenced thread uses the forms lost focus event). The lost focus event of a control does not, as far as I know, have any concept at all about what triggered this event (what other controls is clicked, closing the form, hitting enter ...), only that the focus is about to leave the control, unless it is cancelled ...

You will probably have to find some other method of getting the results you're needing ...

Roy-Vidar
 
Try this. Use Screen instead of Me.

Screen.ActiveControl.Name

Here is an example that works for me.

Code:
[b]ActiveFormName = Screen.ActiveForm.Name[/b]
[b]ActiveControlName = Screen.ActiveControl.Name[/b]
If (PrevControlName = "") Or (PrevFormName = "") Or (ActiveFormName <> PrevFormName) Or (ActiveControlName <> PrevControlName) Then
PrevControlName = ActiveControlName
PrevFormName = ActiveFormName
.
.
.
 
jadams0173,

Thank you for the reply. Can you incorporate the code you gave me into the code I originally posted? I don't understand.

MrsBean
 
Yes, but after playing with it some, I think Roy is correct :) . I don't think access knows what control gets the focus until after lost focus event has ran. The code I posted above was not from a lost focus event just an example of using Screen instead of Me.
 
You may try this in the cmdDeleteRecord and cmdSaveRecord Click event procedures:
If Screen.PreviousControl.Name = "yourControl" Then
Me![yourControl].SetFocus
Exit Sub
End If
...

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
How are ya mrsbean . . .

During the [blue]LostFocus[/blue] event of a control, [purple]that control is still the Active Control![/purple] Even after the LostFocus event completes, [blue]its still the active control until another control receives the focus[/blue] . . . and it has no way of knowing where the focus is going!

I suspect the focus is initially on some control before you click cmdDeleteRecord or cmdSaveRecord. Is this correct?

In any case, look to the [blue]OnExit[/blue] event which can be canceled with the [blue]Cancel[/blue] arguement.

Calvin.gif
See Ya! . . . . . .
 
Thanks AceMan1 ... I think that will serve quite nicely.

MrsBean
 
So - the on exit event of the control, as opposed to the on lost focus event of the control, knows which other control is hit, then? In the on exit event, the control isn't still the active control? Hmm - interesting - could you parhaps give a small sample of that?

Else, one might try out PHV's suggestion (screen.previouscontrol), perhaps in conjunction with the OldValue property of it? But I'd still recommend rethinking the form setup.

Roy-Vidar
 
Howdy RoyVidar . . .
RoyVidar said:
[blue]So - the on exit event of the control, as opposed to the on lost focus event of the control, knows which other control is hit, then?[/blue]
Of course not! . . . but the [blue]OnExit[/blue] event is the one that can be canceled . . . a function sought by [blue]mrsbean[/blue] . . .

Calvin.gif
See Ya! . . . . . .
 
From the initial qustion:

"I have a Lost focus event that I want to cancel if the user selects either cmdDeleteRecord button or cmdSaveRecord button"

We probably read this differently. To me, this question is not only about which control events can be cancelled, but how to determine such cancelling based on what action invoked the leaving of the control - if the event was triggered by clicking one of two specific buttons, the event should be cancelled.

To this I have answered with a recommandation of rethinking the whole approach, as I've found those events rather unaware of what triggers them, but you have answered to use the on exit event.

Could we please see a sample?

Roy-Vidar
 
I think PHV's suggestion is to the right direction.
Code:
Private Sub cmdDeleteRecord_Click()
    If Screen.PreviousControl.Name = "CboDistributors" Then
        Exit Sub
    Else
        'Do what you want to do
    End If
End Sub
so with "cmdSaveRecord" ....

________________________________________________________
Zameer Abdulla
Help to find Missing people
All cats love fish but fear to wet their paws..
 
RoyVidar . . .
RoyVidar said:
[blue]To me, this question is not only about which control events can be cancelled, but [purple]how to determine such cancelling based on what action invoked the leaving of the control[/purple] . . .[/blue]
[purple]I explain exactly why this can't be done[/purple] in my post, particularly in attempts to use the [blue].ActiveControl[/blue] method.
TheAceMan1 said:
[blue]The control the LostFocus event is tied too, is still the active control during rutime of [blue]LostFocus[/blue].[/blue]
I don't know what I'm missing here, but I see on this point we agree it can't be bone . . . at least not in this way.
RoyVidar said:
[blue] as I've found those events rather unaware of what triggers them, [purple]but you have answered to use the on exit event.[/purple][/blue]
Since part of what [blue]mrsbean[/blue] wants is to [blue]cancel an event[/blue], I simply indicated she should at least look to [blue]OnExit[/blue], which can be cancelled. I never said nor indicated this was the solution, espcially under current code examples, but yes . . . if she wants to cancel an event, why not here . . .


Calvin.gif
See Ya! . . . . . .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top