Results 1 to 10 of 10

Thread: Loading and Unloading forms (Resolved!)

  1. #1

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762

    Loading and Unloading forms (Resolved!)

    I have an app that starts in Sub Main by checking to see if the app is already running, and if not, it loads the splash form
    Code:
    Private Sub Main()
        If App.PrevInstance Then
            End
        Else
            frmSplash.Show
        End If
    End Sub
    the splash form will hide after 10 seconds or if the user clicks on it, and then Loads MainForm---

    Code:
    Private Sub Form_Load()
    
    'do a bunch of waiting and stuff
        Load Mainfrm
    End Sub
    and in the Load MainForm event I unload frmSplash
    Code:
     
    
    Private Sub Form_Load()
        Unload frmSplash
      
       'do a bunch more stuff
    
    End Sub
    my problem is that Sub Main never completes and when I unload frmSplash, I get an "Object was Unloaded error"
    How do I get around this, OnError Resume Next??????

    kevin
    Last edited by kebo; May 25th, 2004 at 09:58 AM.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  2. #2
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149
    Nope you just need to reorder your thinking.

    Sub Main will not finish while there is code blocking it (ie your splash screen).

    Here is the order your program will run in basically:

    Sub Main Begin
    frmSplash_Load Begin
    frmSplash_Load End
    frmSplash_Unload Begin
    frmMain_Load Begin
    frmSplash_Unload Begin ??? Here is one of your problems
    frmMain_Load End

    Once your splash screen unloads correctly!!! then your Sub Main will end properly.

    Try reordering when forms are displayed and unloaded.

  3. #3

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762
    thanks much JR. Here is what I am left with
    Code:
     
    
    Private Sub Main()
        If App.PrevInstance Then
            End
        Else
            Load frmSplash
            Unload frmSplash
            Load Mainfrm
        End If
    End Sub
    One more question though, is it OK to use END in this case, or is there a better method ?
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  4. #4
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149
    I never use the End statement here is what I do

    VB Code:
    1. Private Sub Form_Unload() 'Unload routine for main form
    2.      Dim frm as Form
    3.      For Each frm in Forms
    4.           Unload frm
    5.      Next frm
    6. End Sub

    This way you actually unload all of your forms and when all of your forms are unloaded then the program ends on it's own.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    Instead of End use a procedure

    VB Code:
    1. Public Sub Unload_AllForms()
    2. Dim udoForm As Form
    3.  
    4.    For Each udoForm In Forms
    5.       Unload udoForm
    6.    Next
    7. End Sub

    Also, make your startup relative to one procedure, eg Sub Main()

    VB Code:
    1. Public Sub Main()
    2.    If App.PrevInstance Then
    3.       Unload_AllForms
    4.       Exit Sub 'just in case
    5.    End IF
    6.    frmSplash.Show vbModal   'unloads after 10 secs or user click
    7.    'execution continues after frmSplash hhihdden or unloaded
    8.    frmMain.Show vbModeless
    9.    'Execution continues even if frmMain not hidden unloaded cause its shown modeless
    10.    'Other startup algo if applicable
    11. End Sub

    Showing frmMain modal would prevent Sub Main() from finishing.

    VB Code:
    1. Private Sub frmSplash_Load()
    2.    TimerUnload.Interval = 10000
    3.    TimerUnload.Enabled = True 'Set to false in design time
    4. End Sub
    5.  
    6. Private Sub frmSplash_Click()
    7.    Unload Me
    8. End Sub
    9.  
    10. Private Sub timerUnload_timer()
    11.    Unlaod Me
    12. End Sub

  6. #6

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762
    Thanks All,

    I agree END should not be used, and the proper method is to unload everything, but in this case, Sub Main is not in a form, its in a module and when the END needs to be executed, there are no forms loaded. How do I unload a subroutine in a module?
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  7. #7
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629
    If theres no object with a handle (hWnd) loaded then the app will terminate after finishing Sub Main().

    That's why you show frmMain, frmMain being a form has a handle.

  8. #8

    Thread Starter
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,762
    Thanks all
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  9. #9
    Addicted Member JRSofty's Avatar
    Join Date
    Jan 2004
    Location
    Somewhere in Germany
    Posts
    149
    Ok if you want to stop refering to a Class module just set its variable to Nothing

    Dim SomeVar as CSomeClass

    Set SomeVar = New CSomeClass

    Set SomeVar = Nothing

    However if you are talking about a normal Bas Module then once the User interfaces are unloaded the rest will as well.

    Sub Main doesn't keep running throughout your application's life. It only runs until it reaches the End Sub statement, as with All Sub routines.

    I hope that clarifies it for you.

  10. #10
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,632
    I would use:
    VB Code:
    1. Private Sub Main()
    2.     If Not App.PrevInstance Then
    3.         ShowSplash
    4.         ShowMain
    5.     Else
    6.        MsgBox "Application is already running"
    7.        'you could add code here that would find the window
    8.        'handle of the cuurently open application.
    9.        'Then using FindWindow and SetForeGroundWindow APIs
    10.        'you can make the app come to the front of the screen.
    11.     End If
    12. End Sub
    13.  
    14. Private Sub ShowSplash()
    15. Dim frmNew As frmSplash
    16.    Set frmNew = New frmSplash
    17.    Load frmNew
    18.    frmNew.Show vbModal
    19.    Set frmNew = Nothing
    20. End Sub
    21.  
    22. Private Sub ShowMain()
    23. Dim frmNew As frmMain
    24.    Set frmNew = New frmMain
    25.    Load frmNew
    26.    frmNew.Show
    27.    Set frmNew = Nothing
    28. End Sub
    there is no need to use the END command OR to unload forms, since you ain't loaded anything.

    Woka

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width