Userform - how do I return values without hiding it?

squidgeny

Board Regular
Joined
Jul 26, 2011
Messages
130
Hi everyone,

I have a piece of code which (highly simplified) looks like this:

Code:
Sub mySub()

myform.Show

Select Case myform.Tag
    Case 1
        mysecondform.Show
    Case 2
        mythirdform.Show
End Select

End Sub
This code displays a form (myform) with a couple of buttons, which when pressed display other forms of their own (mysecondform and mythirdform)

The code for myform is simply this:

Code:
Private Sub ShowMySecondForm_Click()

    Tag = 1
    Hide

End Sub

Private Sub ShowMyThirdForm_Click()

    Tag = 2
    Hide

End Sub
What I want is for mysecondform and mythirdform to appear without myform being hidden. So if, for example, I clicked a Cancel button on mysecondform (which would hide it), myform would still be displayed. However, when I try stripping out the "Hide" method from the above code, the buttons don't work.

It seems as if hiding the form is necessary for mySub to continue running its code (and thus do the Select Case stuff). A workaround would be to put mysecondform.Show into the code of ShowMySecondForm_Click, etc, but for reasons I won't go into, I can't do this.

So is there any way to get around this? Or have I backed myself into a corner?

Thanks!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Shouldnn't that be:
Code:
Sub mySub()

myform.Show

End Sub
and
Code:
Private Sub ShowMySecondForm_Click()

            mysecondform.Show
            Me.Hide

End Sub

Private Sub ShowMyThirdForm_Click()

    mythirdform.Show
    Me.Hide


End Sub
 
Last edited:
Upvote 0
Well that's the thing, there's a lot of other code involved which won't enable me to do that.

However, I just had a thought - can I use a GoTo thing? So my code would be:

Code:
Sub mySub()

StartAgain:

myform.Show

Select Case myform.Tag
    Case 1
        mysecondform.Show
             Select Case
                  Case 0 '(which would occur if, say, the Cancel button was clicked)
                       GoTo StartAgain
             End Select
    Case 2
        mythirdform.Show
             Select Case
                  Case 0
                       GoTo StartAgain
             End Select
End Select

End Sub
I have not used GoTos before so I'm not sure if this'll do the trick though. Worth trying?
 
Upvote 0
When a userform runs (is shown), all executing code from the calling procedure stops until the form's either hidden, or (often better) unloaded. The calling procedure then carries on with any other code.
If, however, you run userforms modelessly, the calling procedure's code will continue to run.

This can be demonstrated by you running these two subs:
Code:
Sub mySub()

myform.Show
MsgBox "Test. This message didn't display 'til after you closed the userform."
End Sub

...and
Code:
Sub mySub()

myform.Show modeless
MsgBox "Test. This message displayed whilst the userform was still active and visible."
End Sub

...........armed with this, you should be able to sort yourself out.
 
Upvote 0
....................oh, and Welcome to Mr. Excel!!
 
Upvote 0
......To put it all into context.........
Code:
Private Sub ShowMySecondForm_Click()

            mysecondform.Show

End Sub

Private Sub ShowMyThirdForm_Click()

    mythirdform.Show
 


End Sub

........you may have to mess about with the modeless properties if you get an error message, but it'll get you on the right track............
 
Upvote 0
Thank you! This board has helped me a lot so I plan to stick around and contribute :)

Anyway, I tried my GoTo idea but suffice to say it didn't work.

I also tried putting "modeless" after the .Show method but when running the code, I got an error telling me the variable was undefined... is there anything else I have to do to use modeless? :/

Edit: sorry, took too long and didn't see your latest post. I give things a try.
 
Upvote 0
I would suggest you alter the two forms that get called after the first form so that they return a Cancelled property if applicable. The calling code can then check that and re-show the first form if necessary.
Modeless forms won't work since the first routine will carry on before you have a chance to press a button on the first form.
 
Upvote 0
Did myform ever get to show?

Which variable was undefined? If it was the "Tag" then you should be able to do away with those, now.

I need to go out in a while, but I'll monitor this post for as long as I can.

The modeless method works as advertised, so the problem's probably simple, but will be elsewhere. Without looking at your code it's difficult to troubleshoot.

I just took a new workbook - inserted 3 Userforms.
UF 1 had 2 commandbuttons.
Code:
Userform1.show modeless
displays UF1.
Code:
Private Sub CommandButton1_Click()
UserForm2.Show modeless
End Sub
...displays UF2 - modelessly as well - (so CB2 on UF1 can still be pressed).
Code:
Private Sub CommandButton2_Click()
UserForm3.Show
End Sub
displays UF3 (modelessly - to show example of modeless operation)
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,843
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top