Solved

MS Access VBA - Exiting SUB from another form

Posted on 2008-06-13
3
1,087 Views
Last Modified: 2008-07-07
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
0
Comment
Question by:andyb7901
3 Comments
 
LVL 16

Expert Comment

by:robinu
ID: 21776852
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?
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 21776914
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

0
 
LVL 16

Accepted Solution

by:
Rick_Rickards earned 500 total points
ID: 21896292
Normally if you open a form from a procedure in a module the code in that procedure will continue to execute until it is done.  If, however, the form is opened as a dialog form, or is modal then the calling procedure's code halts until the form is...

1) Closed
   or
2) Hidden

Given two ways to resume execution of the code in your calling procedure you have a way to exit the procedure under one of the two scenarios.  

Below are two procedures to illustrate how this would work.  The first one would be in some global module.  The second procedure would be behind the OnClick Event of a command button named cmdCloseHide.

How it works...

The Procedure SampleSub opens the Form named "frmTest" as a dialog form.  Thus all code within that procedure halts until "frmTest" is closed or hidden.

If the user clicks the cmdCloseHide button the OnClick event hides the form.  From the users perspective they will think it was closed.  At this point control goes back to SampleSub where you'll find the first line of code checks to see if "frmTest" is loaded.  If it is then it closes the form and exits the procedure.

If the form was closed, however, then code simply continues beyond the End If statement you see here.

In fact you could reverse the logic if you prefer, meaning you could exit the sub routine if the form was not loaded else continue the execution of code within the procedure if the form was merely hidden.  Just remember that if you hide the form that the very next thing you'll usually do after you've confirmed that that is the case is to close the form unless you have some reason for leaving it loaded and hidden.  

In short, you have the option to exit the calling procedure as soon as the form is hidden or closed (your choice) but remember that if the form is not opened as a dialog form that the code does not halt and thus it will most likely finish executing everything there long before you hide or close the form in the first place.

Rick


If the user closes the form then the code from SampleSub resumes

Sub SampleSub()

    DoCmd.OpenForm "frmTest", , , , , acDialog

    If CurrentProject.AllForms("frmTest").IsLoaded Then

        DoCmd.Close acForm, "frmTest"

        Exit Sub

    End If

    Debug.Print "Code continues after form has closed"

End Sub
 
 

Private Sub cmdCloseHide_Click()

    'This command doesn't actually close the form, it hides it.

    'If the form was called from another procedure as a dialog form (modal) then

    'Code from the calling procedure would halt until this form was closed or hidden.

    'By hiding it instead of closing it we give the calling procedure the option to decide

    'how to proceed.

    Me.Visible = False

End Sub

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now