• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 465
  • Last Modified:

How do I end a vb program in an orderly fashion?

Hi-

I have VBA, VBS and VBSCRIPT programs that I sometimes need to exit from all the subroutines/ functions and go to a specific subroutine to end the program.  I suspect in order for the program to end in an orderly fashion, I want the call stack cleared (We used to call this "popping the call stack" when I programmed back in the '70s... the old dinosaur that I am).

How do I accomplish this?  Are the commands different for VBA, VBS and VBSCRIPT?

Thanks for your help.  With Regards-
Sam
0
SAbboushi
Asked:
SAbboushi
  • 9
  • 9
1 Solution
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
In VBA just close all your forms and set objects to nothing, I think the same holds true for VBS.

Bob
0
 
SAbboushiAuthor Commented:
Hi Bob-
I only have one form.  I am asking about subroutines - e.g. the main routine calls a subroutine that calls a subroutine that calls a function... and while I am 6 levels deep in program execution (requiring 6 "End Subs" or "End Functions" to return me to the main routine), I want to "pop the stack" and have the program execution forget that I am 6 levels deep.
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
If I understand your question, all you need to do is call the main routine from the function or sub that is executing, VB takes care of the stack house keeping.

Bob
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
SAbboushiAuthor Commented:
Thanks for your response Bob - by doing what you suggest, the call stack is still filled with the previous calling routines.  The next "End Sub" will return execution to


A calls subroutine B (Call stack shows A)
B calls subroutine C (Call stack shows A, B)
C calls subroutine D (Call stack shows A, B, C)
Subroutine D is completed and "End Sub" is executed - this returns execution to subroutine C (Call stack shows A, B)

Within Subroutine D, I want to GOTO Subroutine E and have the call stack empty.  When Subroutine E hits the End Sub statement, I want to program to stop because there is nothing in the call stack.
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
> popping the call stack
if you want to get into the innards of this, see http://www.thevbzone.com/secrets.htm

Bob
0
 
SAbboushiAuthor Commented:
Thanks Bob-

From what I saw, the link is more an explanation of how memory and pointers are used as relates to the call stack - but this does not address my question...
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
That is what pointers do, point to memory addresses that are stored ont the stack.
excerpts from the linked article. ......................... C and C++ programmers actually have to create all that object creation, message handling, and object destruction code by hand (or have a template of it generated).

Visual Basic's ability to do the "basics" for you like this is a powerful thing to programmers who know how to correctly use VB as a development tool, but also puts a lot of power into the hands of people that don't know much about programming.....................

Using Pointers In Visual Basic:

I was once asked in a job interview a question that I now realize was a TRICK QUESTION.  The question was, "Does Visual Basic have or use 'pointers' ?"  The obvious answer to anyone that uses Visual Basic is "NO".  You don't see pointer declarations and macros in VB like you do in C/C++... and that's what I think the interviewer was getting at.  She accepted my answer with that reasoning.   However, the correct answer should have been "YES".

Visual Basic (like just about every other programming language) does use pointers... EXTENSIVELY.  The difference is, Visual Basic hides them from you whenever possible, or calls them something different so as to not burden you with the formalities and protocols required when using them.

I will proceed to explain how you can use pointers to access information held in variables directly (VarPtr / StrPtr / ObjPtr), pass information to functions by pointers (ByRef / ByVal), and retrieve and pass pointers to functions (AddressOf).

^ TOP ^

VarPtr, StrPtr, and ObjPtr:

The VB Functions "VarPtr" (Variable Pointer), "StrPtr" (String Pointer), and "ObjPtr" (Object Pointer) are UNDOCUMENTED, UNSUPPORTED functions that Microsoft has made available in Visual Basic 5.0, and 6.0.  These functions (along with many others) are no longer available in VB.net.  These functions allow you to get the address in memory where VB variables (pointers) are, as well as the address in memory where the actual data that the variables point to are.   The reason these functions are so useful is because if you know the memory address of data, you can manipulate it, copy it, or pass it around directly instead of relying on VB to do it for you.  This is MUCH faster and (in some cases) gives you the ability to do things that VB on it's own can't do.

This is what Microsoft's MSDN says about "VarPtr":

This function can be used to get the address of a variable or an array element.   It takes the variable name or the array element as the parameter and returns the address. However, you should be aware that unlocked Dynamic Arrays may be reallocated by Visual Basic, so you must be very careful when you use VarPtr to get the address of an array element.

The following example gets the address of a variable:
Dim lngVariableAddress As Long
Dim dblMyVariable As Double
lngVariableAddress = VarPtr(dblMyVariable)

This example gets the address of the fourth element of an array:

Dim lngElementAddress As Long
Dim lngArrayOfLongs(9) As Long
' The following will get the address of the 4th element in the array
lngElementAddress = VarPtr(lngArrayOfLongs(3))

Limitations: The VarPtr function cannot be used to get the address of an array...
 

This is what Microsoft's MSDN says about "StrPtr":

The point is not specificall addressed in this but the way VB handles these objects on the stack is once there is no longer a reference to them, it destroys them . Not strictly Garbage Collection as in Java and .Net, bet the same effect never the less.

Bob
0
 
SAbboushiAuthor Commented:

Bob - I appreciate your summary, but I do not see how this information helps me to do what I have asked  ; )
0
 
SAbboushiAuthor Commented:
OK - I think I've figured out what I need to do.  I can use Exit statements (e.g. Exit Sub) which would be complicated for what I want to do; but the End statement is the solution I am looking for.

From Sub D, I simply call Sub E; within Sub E, I do whatever I need to do and then use an "End" statement. which terminates the program in an orderly fashion.

It seems like such a simple solution that I feel the description of what I was looking for must have been lacking...

With Regards-
Sam
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
You are kidding right?

Bob
0
 
SAbboushiAuthor Commented:
Hi Bob-
Sorry for my delay in responding.  Re: kidding... what do you mean?
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
> How do I accomplish this?

close all your forms and set objects to nothing.

Bob
0
 
SAbboushiAuthor Commented:
Bob-
Your original answer was not helpful to me.  I explained the solution that I needed:  I was not aware of the "End" statement
0
 
Bob LambersonSoftware EngineerCommented:
Sam,
This seems like we are not communicating very well here, and maybe I am misunderstanding what you are asking. What you are referring to by popping the stack, is now done in VB by setting an object reference that is on the stack to Nothing. VB deletes the reference to the object (on the stack), and frees the memory space when there are no more valid references to that object. This is an inherrent process in VB and you do not have to write a line of code to "pop" the object off of the stack. Just as you don't need to write a line of code that "pushes" an object/address onto the stack.

From the help files.........(the quotation mark emphasis is mine)
Microsoft® Visual Basic® Scripting Edition
Nothing   Language Reference
Version 1
 
Description
The "Nothing keyword in VBScript is used to disassociate an object variable from any actual object". Use the Set statement to assign Nothing to an object variable. For example:
Set MyObject = Nothing

Several object variables can refer to the same actual object. When Nothing is assigned to an object variable, that variable no longer refers to any actual object. When several object variables refer to the same object, "memory and system resources associated with the object to which the variables refer are released only after all of them have been set to Nothing", either explicitly using Set, or implicitly after the last object variable set to Nothing goes out of scope.

Further .....

All procedures, whether a function or a sub, must end with End - you can't have written a function without using End Function or End Sub.

Hope this is helpful, but if you still would like clarification, email the page editor, that is part of the job to help with unanswered questions.

Bob
0
 
SAbboushiAuthor Commented:
Thanks for the clarification Bob-

I think we are trying to answer two different questions - and my reference to popping the call stack seems to be the issue causing confusion.  In the VBS debugger, there is a list referred to as the "call stack".

I am not referring to an End Function or End sub statement, I am referring to an End statement within a sub.  An End Sub or End Function statement will return execution to the statement of the calling routine - as outlined in the call stack.  This is what I meant by:

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Thanks for your response Bob - by doing what you suggest, the call stack is still filled with the previous calling routines.  The next "End Sub" will return execution to

A calls subroutine B (Call stack shows A)
B calls subroutine C (Call stack shows A, B)
C calls subroutine D (Call stack shows A, B, C)
Subroutine D is completed and "End Sub" is executed - this returns execution to subroutine C (Call stack shows A, B)

Within Subroutine D, I want to GOTO Subroutine E and have the call stack empty.  When Subroutine E hits the End Sub statement, I want to program to stop because there is nothing in the call stack.
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

In order to "GOTO Subroutine E and have the call stack empty", I call Subroutine E and within Subroutine E, there is an End statement (this is in ADDITION to the End Sub statement).  That "End" statement is what stops the program execution.

Sorry we had seemed to have been miscommunicating-

With Regards
Sam
0
 
Bob LambersonSoftware EngineerCommented:
SAbboushi,
Well, ...... The End statement does not "pop a reference off the stack", instead it abruptly shuts down the entire application by deleting, or causing the deletion, of "all" of the entries in the stack. Two different things.

Here is a quote from the VB help file on the End statement. Note that this is NOT allowing the "program to end in an orderly fashion";

"Note   The End statement stops code execution abruptly, without invoking the Unload, QueryUnload, or Terminate event, or any other Visual Basic code. Code you have placed in the Unload, QueryUnload, and Terminate events offorms andclass modules is not executed. Objects created from class modules are destroyed, files opened using the Open statement are closed, and memory used by your program is freed. Object references held by other programs are invalidated.

The End statement provides a way to force your program to halt. For normal termination of a Visual Basic program, you should unload all forms. Your program closes as soon as there are no other programs holding references to objects created from your public class modules and no code executing."

Bob
0
 
SAbboushiAuthor Commented:
Thanks for all your work Bob - sorry for the confusion.
0
 
Bob LambersonSoftware EngineerCommented:
Sam,
Thanks and I hope it was somewhat helpful.


Bob
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now