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

conditional cancel of Exit event 1

Status
Not open for further replies.

rfoye

Programmer
Oct 15, 2004
40
US
If the value of a combo box is 0, I want to cancel the OnExit event except if the user clicks on a button that closes the form. Can this be done? Is there any way to reference the control that will receive focus from within the Exit event of a control?

-------------------
Rob Foye
Database Management
Regions Bank
 
I think you could place in the 'OnExit' event something like this -
(air code, I am not sure if it should be combo1.value or combo1.selection)

Code:
If me.Combo1.Value = 0 Then
      Do Something
Else
      Do Something Else
End if

Then, you could just handle the 'Do Something else' part in the click event of the close button in question. (if Combo1 = 0)

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
I've already got this.
Code:
Private Sub cboDept_Exit(Cancel As Integer)
    If Me.cboDept = 0 Then
        Cancel = True
    End If
End Sub
What I need is a way to reference the control that the focus is moving to, something like:
Code:
Private Sub cboDept_Exit(Cancel As Integer)
    If me.cboDept = 0 AND {clicked control}.name<>"cmdClose" Then
        Cancel = True
    End If
End Sub


-------------------
Rob Foye
Database Management
Regions Bank
 
You know, I don't think its a good idea to have events fire like this. If you have users that click all over the place, it results in slower performance.

Anyway, you would probably want the OnLostFocus event if you are trying to see what control got the focus next.

What are you trying to cancel anyway? I don't see an 'else' there...

Hope this helps,

Alex

Ignorance of certain subjects is a great part of wisdom
 
How are ya rfoye . . .

Remove/disable the code in the [blue]On Exit[/blue] event and in the event your using for the [blue]Close Form Button[/blue] replace with the following:
Code:
[blue]   If Screen.PreviousControl.Name = "cboDept" And _
      Me!cboDept = 0 Then
      MsgBox "cboDept can't be zero! . . ."
      Me!cboDept.SetFocus
   Else
      [green]'Close form code here[/green]
   End If[/blue]
[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
rfoye . . .

Scratch my prior post. That will only work if the user clicks the form close button. It doesn't account for clicking other controls.

Working up something else . . .

Calvin.gif
See Ya! . . . . . .
 
OK rfoye . . .

Problem is you can't check where the focus is going while its in transit. Whats needed is time for this changing of focus to occur. Then the [blue]focus for the current and previous controls are available.[/blue] To accomplish this I use the forms timer in the following secnario:
[ol][li]The [blue]On Lost Focus[/blue] event of the combobox sets the forms timer to 1 millisecond. This is more than enough time for the focus transition to complete. The code for the [blue]On Lost Focus[/blue] event looks like:
Code:
[blue]   Me.TimerInterval = 1[/blue]
.[/li]
[li]The [blue]On Lost Focus[/blue] event completes as well as the focus transition in less than 1 millisecond.[/li]
[li]When one millisecond arrives the [blue]On Timer[/blue] event triggers with the following code:
Code:
[blue]   If Screen.ActiveControl.Name <> "[purple][b][i]CloseButtonName[/i][/b][/purple]" And _
      Me!cboDept = 0 Then
      MsgBox "cboDept can't be zero! . . ."
      Me!cboDept.SetFocus
   End If
   
   Me.TimerInterval = 0 'turn timer off[/blue]
[/li][/ol]
If the focus has not gone to the close button a message is displayed and focus is set back to cboDept!

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .
 
Thanks, AceMan. I was working toward something like you gave me, but didn't think about the timer events.

-------------------
Rob Foye
Database Management
Regions Bank
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top