'The Application starts with frmLogin and 
'that form will open the next form and  
'then hide its self so it stays open
'while the program is running. The code
'below is used to keep track of how many
'forms are open and will close the program
'when the only form open is the hidden
'login form.

Public Class frmAccount
    Inherits System.Windows.Forms.Form
    Private UBP1 As New UBProcs1()

'Windows Form Designer generated code

    Private Sub frmAccount_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        UBP1.gsFrmNum += 1
    End Sub
    Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
        UBP1.gsFrmNum -= 1
        UBP1.CloseScreenMaybeApp()
    End Sub

End Class

Public Class UBProcs1

    'Number of Forms Open
    '   frmLogin should always be open but hidden after login,
    '   so when  this number reaches 1 then all visible forms are closed
    '   so the application needs to be closed.
    Public Shared gsFrmNum As Integer

    Public Sub CloseScreenMaybeApp()
        If gsFrmNum < 2 Then
            Application.Exit()
        End If
    End Sub

End Class