Link to home
Create AccountLog in
Avatar of andyb7901
andyb7901

asked on

MS Access VBA - Exiting SUB from another form

Hi,

I have a form which loaded from a module. When I press the cancle button on my form is there anyway to exit the sub in which the code is currently running?

Thanks
Avatar of Robin Uijt
Robin Uijt
Flag of Netherlands image

When you press the cancel button on your form, and execute

DoCmd.Close

the form will close, and you will 'return' to the sub.

Is that what you meant?
If you have a looping procedure which takes a long time and you want to be able to stop it, you will need to have it test a variable each time that it goes through the loop and to have DoEvents in the loop to allow the variable to be updated.
Your Cancel button would change the value of the variable. The (VB6) snippet illustrates


Option Explicit
Dim bStop As Boolean
 
Private Sub Command1_Click()
Do
    Text1.Text = (Val(Text1.Text) + 1) Mod 30000
    DoEvents
    If bStop Then
        bStop = False
        Exit Sub
    End If
Loop
End Sub
 
Private Sub Command2_Click()
    bStop = True
End Sub

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Rick_Rickards
Rick_Rickards
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer