Solved

MS Access VBA - Exiting SUB from another form

Posted on 2008-06-13
3
1,081 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

706 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

14 Experts available now in Live!

Get 1:1 Help Now