Consulting

Results 1 to 9 of 9

Thread: Show userform from another userform

  1. #1
    VBAX Regular
    Joined
    Feb 2014
    Posts
    53
    Location

    Show userform from another userform

    I am showing a userform with some settings in it to control the main portion of my code. This works properly. I wish to add a 2nd userform that will show a progress bar as the code runs. The problem is that this userform refuses to appear until the code is complete. It also is having trouble unloading.

    First I simply show my menu:

    Sub Format(control As IRibbonControl)
        FormattingOptions.Show
    End Sub
    There is a Private Sub UserForm_Initialize(), but it basically just sets some values to combo boxes etc. Also in this menu, there is a button that calls:

    Private Sub cmdFormat_Click()
    
        '~~code that sets some public variables
    
        Unload Me
        Progress.Show 'userform does not appear until its initialize code has completed for some reason
        'Unload Progress 'does not unload - causes "error 1004: that name is already taken"
    
    
    End Sub
    The Progress userform is intended to show an updating progress bar. But it refuses to appear at this point. It DOES however properly run the simple initialization code:

    Private Sub UserForm_Initialize()
        Call MainFormat
    End Sub
    The idea is that my MainFormat code will run and continuously update the progress bar. Then the Progress userform should close.

    Public Sub MainFormat()
        '~~a lot of code that manipulates some data
        'Unload Progress 'causes error 91 on Progress.Show within Sub cmdFormat_Click()
    End Sub
    At this point the Progress userform finally appears. I don't understand why it doesn't appear when I first show it.
    Also, as you may have noticed from my commented lines above, I have also tried to specify where the Progress userform should unload. Both location produce an error.

    Any ideas?

  2. #2
    VBAX Expert Leith Ross's Avatar
    Joined
    Oct 2012
    Location
    San Francisco, California
    Posts
    552
    Location
    Hello cplindem,

    By default, UserForms are displayed modally. A modal form disables input to it's owner window (in this case Excel) and the owner's child windows until the UserForm is dismissed.

    However, you can change the modallity of the UserForm either by changing the ShowModal property in the Properties Window to False or explicit setting the Show method Modal argument to False at run-time.

    Example of Changing the UserForm's Modality at Run-time
        UserForm2.Show Modal:=False
    Sincerely,
    Leith Ross

    "1N73LL1G3NC3 15 7H3 4B1L17Y 70 4D4P7 70 CH4NG3 - 573PH3N H4WK1NG"

  3. #3
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    But why don't you add the progressbar (that will slow down your code) in the first userform itself ?

  4. #4
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Or at least put the formatting code in its Activate event. Never a good idea to unload a form before it finishes loading.
    Be as you wish to seem

  5. #5
    VBAX Regular
    Joined
    Feb 2014
    Posts
    53
    Location
    Thanks for all the assistance.

    I tried adding Modal:=False, but the form still did not appear. However, I do see the purpose.

    As for snb's suggestion to simply create the progress bar in the original form - that's a good idea. The only issue is that one of the setting options in the form actually opens yet another form with additional settings, before continuing on with the remaining code. So if the user selects that option, I guess I would have to build a progress bar in that form as well. Obviously, only having one instance of the progress bar is the way to go.

    However, that got me thinking more about why I am able to successfully get that additional from to appear. It gets shown near the beginning of Sub MainFormat() so I went ahead and added the Progress form directly after that. That worked. Adding Modal:=False produced an error though - error 446: Object doesn't support named arguments. I tried changing the ShowModal property to false and that did work. Does vbModeless do the same thing?

    Now I just have to build the progress bar itself and get it to update. I should be able to handle that.

    As for Aflatoon's suggestion, I actually had all the formatting code in the form's Initialize event (I'm assuming Activate wouldn't be any different in this case), but I moved it to a module when I began my progress bar journey. I was under the impression that this was better anyway - am I wrong? Also, wouldn't that mean that the Progress form gets shown while the FormattingOptions form is still open? I'm not quite sure what you mean by unloading a form before it finishes loading. Where am I doing that?

    Thanks again.

  6. #6
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    Why would you assume the opposite of what I said? Did you try moving it to the activate event?
    Be as you wish to seem

  7. #7
    VBAX Regular
    Joined
    Feb 2014
    Posts
    53
    Location
    By opposite, do you mean removing the formatting code from the initialize event? If so, I did that before I even started this thread.

    I have actually gotten the Progress form to show up at the proper time, but it only appears white. The bar updates as I have coded it to, but it only displays those updates once the code has finished. I am using Progress.Repaint to try to update it at various points in the formatting code, but it refuses to show anything but white. I'm guessing this is why I need to have the formatting code in the Progress form's activate event?

  8. #8
    VBAX Master Aflatoon's Avatar
    Joined
    Sep 2009
    Location
    UK
    Posts
    1,720
    Location
    The Initialize event occurs when the form is loaded into memory and before it is shown on screen. The activate event occurs when it is actually shown.
    Be as you wish to seem

  9. #9
    VBAX Regular
    Joined
    Feb 2014
    Posts
    53
    Location
    I have moved the majority of my code inside Progress.Activate() and using Repaint now works properly.

    Many thanks

Posting Permissions

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