Results 1 to 3 of 3

Thread: Unload form in Form_Load()

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Unload form in Form_Load()

    Hi,
    I have two forms:
    - frmMain
    - frmPreview

    I call frmPreview from frmMain using
    Code:
    ' This button is on frmMain
    Private Sub Command1_Click()
      '
      ' some code 
      '
      frmPreview.Show vbModal
      '
      if globalVar = True then
        ' some other code
      endif
      '
    End Sub
    In the Form_Load() of frmPreview I'd like to be able to stop loading the form, unload it and go back to frmMain and execute the more code.

    Can I unload a form from its Form_Load()?

    Something like this... that actually works
    Code:
    ' This is my frmPreview
    Private Sub Form_Load()
      If something then
        globalVar = True
        Unload me
      endif
    End Sub
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Unload form in Form_Load()

    You will get an trappable error indicating the form is already unloaded and it will highlight the .Show statement on the calling form. You can get around that though, by trapping it
    Code:
    'on form1
    Private Sub Command1_Click()
    On Error GoTo ErrTrap
    Form2.Show
    Label1.Caption = "Hey there"
    Exit Sub
    ErrTrap:
    If Err.Number = 364 Then 'object was already unloaded
       Resume Next
    Else
       MsgBox Err.Number & " " & Err.Description
    End If
    End Sub
    
    'on Form2
    Private Sub Form_Load()
    If blnStopLoad = True Then
       blnStopLoad = False
       Unload Me
    End If
    End Sub
    blnStopLoad is a global boolean I created for toggling whether or not to load the form.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Unload form in Form_Load()

    probably better to test the value of something before loading the form, or just set it to visible =false
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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