What happen when you exit sub in vb6

What happen when you exit a sub routine in vb6 where does the program goes after you exit sub when there is no more task to run.
Who is Participating?
Since your program is event driven (assuming it's a form-based program), waiting happens naturally when you exit from your event handlers (such as Form_MouseUp).  Just let each event handler run until completion and your program will then wait for the next event.

If you have some other kind of organization (eg, you run everything from your Form_Load event or you have a button handler that never exits), you can use a function like the one in the box, or you can use Sleep.

Use a timer control if you want to perform some task periodically without having to worry about when to run it in other parts of your program.
    'Yield to other processes for tSecs seconds
Public Sub YieldFor(tSecs As Long)
    Dim t0 As Long
    t0 = Timer   ' Set start time
    DoEvents     ' Yield at least once
    Do While Timer <= t0 + tSecs
        DoEvents   ' Yield to other processes
End Sub

Open in new window

As VB6 is an event-driven environment, your program will return to an idle state until an event is triggered, such as a button being clicked, a timer fires, etc.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
"exit sub" will do the same as when the code would have reached the "end sub"

if the "sub" is the event handler for example of a button_click/menu_click etc, the program will go back to the "idle waiting".
if its the "sub main" startup, it will basically end the application unless at least 1 form is loaded (which will keep the application running also).
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

Brook BraswellApplication Development ManagerCommented:
The subroutine will return to where it was called

private sub Main
   msgbox "Here I am"
   msgbox "I am Done"
End Sub

public sub MySub
   msgbox "I am here now"
End Sub

you would get three message boxes
Here I am
I am Here now
I am Done

then program would end.

if you had a user interface then the app waits for an event like button click
if you do not have a user interface and the app has run the last sub the the app should end.

If you want your program to terminate, you can do
for every form that is open, especially your main form.

Or you could do
    PostMessage MainForm.hwnd, WM_SYSCOMMAND, SC_CLOSE, 0

For the latter, put these lines at the top of the file in which you use PostMessage
    Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Const WM_SYSCOMMAND = &H112
    Public Const SC_CLOSE = &HF060

1030071002Author Commented:
if you want your program to wait what would you do :- timer or sleep and what is the difference and which is better
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.