Cascading problems with hide and unload from command buttons

J

John Shanley

I hope that this phenom had been dealt with before. I did search and
couldn't find anything.

I've got a multi-userform app used as a front end for a Word document.
The first form is presented for the user to add content. There are at
least 10 screens (the user has the choice to "add" content screens at
the 10th screen).

Each form has an Accept, Cancel, Previous and Forward button. The
Prev. and Forward butons allow the user to browse the existing screen
content. The Accept button writes the screen's content to the Word doc
and the Cancel button offers to save the Doc and drops out of the
userform interface.

I have been reading in existing doc content and populating the screens
in each Initialize event. Then use the click event of each button to
move from screen to screen. I was using the load and unload commands
but that got wacky. I assumed because the init event gets called each
time. So I changed the app to load all forms in the Doc load event and
then use Hide and show to move around.

like this:

Private Sub butPrevious_Click()
Me.Hide
If frmWelcome.Visible = False Then
frmWelcome.Show
End If
End Sub

Private Sub butForward_Click()
Me.Hide
If frmLearningObj.Visible = False Then
frmLearningObj.Show
End If
End Sub

The problem is that when I unexpectedly dropped out of the interface
(continually pressing the Previous button back to the first screen)
and then use f8 to step thru the code line by line, I'm seeing the
code from previous forms firing. It just keeps going back and back.
Eventually getting back to the first screen's code and somehow
unloading itself.

Any clues on why this happening? More importantly, how can I get the
screen's "disconnected"?

Thanks,
JohnS
 
J

John Shanley

Well, being as no one has replied and I did figure out a solution to
my problem, I figured I share what I came up.

What I eventually did, was put debug statements in all the events I
could find that had anything to do with passing control to and hiding
forms. I did find that the Activate event gets called in tandem with
the Initialize when a form is loaded. But Activate is also called when
a hidden form is "shown". So, I shifted the "repopulating" of the
form's controls of a hidden form to the Activate event.

As far as the hide/show cascading problem. After watching the code
sequences run, it now makes some sense that the code from a sub
continue to execute and follow it's "trail" back, even if the form
that initiated the code is NOT visible anymore. Not "common sense",
but VB sense.<G> When I realized that this is the way VB worked, I saw
that the oft recommended way to hide a form of "me.hide" was the
problem. Actually, me.anything could be a problem, because ME ain't
the ME you expect it to be when you are coding and thinking the that
form lives in it's own little space and time; it's part of a continum
of code that stretches backward (unless you are VERY careful),
possibly to the very first form's controls.

So, all instances of ME. had to be hard coded to the actual form name.
This is the sequence I ended up using:

If frmCurrent.Visible = True Then
frmCurrent.Hide
End If

If frmNext.Visible = False Then
frmNext.Show
End If

Here's the weird thing: If you put ANYMORE code in this subroutine, it
will get executed BUT NOT until the code in frmNext's Initialize,
Activate, ... CloseQuery (probably a few other events in frmNext) is
run.

So, I found that managing multiple userforms was trickier than I
expected. I'm surprised that I could find no advice or practical
guidance on the proper sequence to do it right. I'm sure it's out
there, somewhere, I just have never stumbled across it.

I hope this helps.

JohnS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top