Cancel Exit Event with a Form Button

RUdebugged

New member
Local time
Yesterday, 19:06
Joined
Nov 28, 2011
Messages
5
Hello,
This is my first post - forgive me if I get the etiquette wrong.

I have a form with two controls that have exit events coded. Both Exit events move the focus based on some conditions to controls on next records. I added a public sub in a module to backup the database to 2 different locations and that sub is attached to a button on the form. So when I click the button I want the exit code to stop immediately or at some prescibed point like at the end of the record's data entry and carry out the button's 'backup' sub. I'm not sure how to proceed - the procedure in the exit event is necessary but maybe not on that event...

Any help is appreciated. I'll post the exit event code for one of the controls here. BTW I have confirmed that all this code compiles and works as expected separately; I just can't get the one sub to interrupt the exit subs.

Private Sub tbFinalCondition_Exit(Cancel As Integer)
'SetID is dimensioned as a static variable outside the subs
If SetID = False Then
Exit Sub
End If
tbFinalCondition.SetFocus
While SetID = True
If Not (tbFinalCondition.Text = "undamaged") Then DoCmd.GoToRecord acDataForm, "frm_FinalFilters", acNext, 1
End if
If tbFilterID.Value Then
If tbFinalFilterWt.Value Then
Forms!frm_FinalFilters.Recordset.MoveNext
Else
tbFinalFilterWt.SetFocus
Exit Sub
End If
Else
SetID = False
End If
Wend
End Sub

'this is not an issue at the *end* of the form's recordset
 
so your basically saying that once you exit the control "do some code" unless the button is hit, then instead do the code from the button?

im not sure if this is possible on an Exit Event, ive been trying the following but cannot get it to work

Code:
    Dim ctlCurrentControl As Control
    Set ctlCurrentControl = Me.ActiveControl
    
    If ctlCurrentControl.Name = "testBtn" Then
    MsgBox "test1"
    Else: MsgBox "test2"
    End If

which should on exit check the ative control and if that control's name is the name of my button then run code if not then run some other code...
I have also tried changing the tag of the button to symbolize it has focus and cannot get that to work on the Exit event either.

hopefully someone else has some ideas on maybe changing to a different event
 
So when I click the button I want the exit code to stop immediately or at some prescibed point like at the end of the record's data entry

Access / VBA is not a multi-threaded programming environment, so within a loop somewhere you will have to place perhaps a call to

Code:
DoEvents

but I am not sure that even that will be enough to cause the running code to stop and begin processing the other Button Click event.

I found that adding the call to DoEvents in a LONG running loop does keep the form UI responsive enough that Windows will not declare Access "Not Responding".

When in a long running loop with DoEvents coded, grabbing the Form title bar with the mouse has the loop code pause at the DoEvents until the title bar is released. So while it is aware of other events, I am not sure it can act upon push button events as Access / VBA is not multi-threaded.
 
I think I found a kind of solution...

I added a MouseMove event with a msgbox to run the button code:

Private Sub PanComplete_MouseMove( _
Button As Integer, Shift As Integer, X As Single, Y As Single)
MsgBox "Run PanComplete Code?", vbYesNo
If vbYes Then
DoEvents
PanComplete_Click 'calls my button's Sub
End If
End Sub

'this seems to run the button code except it gives me an error (#2487) saying the "Object Type argument for the action or method is blank or invalid" in the Exit event so apparently interrupting the code needs to happen a little more carefully, but I call it progress!

Thanks all.
 

Users who are viewing this thread

Back
Top Bottom