View Single Post
  #6   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default SetFocus to first textbox on userform upon Userform1.Show

Paul,

As I think you suspected, it is unloaded at this point.

Good news though, there is a QueryClose event that is invoked whenever the
form is unloaded. There are 4 close circumstances, so you can trap them and
react as you see fit. Here is some code

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
Select Case CloseMode
Case vbFormControlMenu:
Cancel = True 'The user has chosen the Close command from the
Control menu on the UserForm.
Case vbFormCode:
'The Unload statement is invoked from code.
Case vbAppWindows:
'The current Windows operating environment
session is ending.
Case vbAppTaskManager:
'The Windows Task Manager is closing the
application
End Select

End Sub

In this I have shown all 4 instabces, but the only one that is trated is the
Control Menu close, the X that is, where I cancel the close. So it has no
effect to click the X.

You could action some of the others, but be careful, you might never be able
to close the dang thing.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Paul Simon" wrote in message
...
Hi Bob,

I have a follow-up question. First of all, your suggestion of my putting

my Userform1.Textbox1.SetFocus line in the Userform_Activate event and
changing the code in my Exit button back to Userform1.Hide (instead of
Unload Me) all works perfectly.

What I'm wondering is this: If the user clicks the Close button (the

built-in "x" button at the top right corner of the Userform) instead of my
Exit button, is the Userform being hidden or unloaded? If unloaded, is
there a way to remove the "x" Close button from the Userform?

Many thanks,
Paul

----- Paul Simon wrote: -----

Hi Bob,

You are absolutely right! If I load and unload the form several

times, I eventually run out of memory. In fact, I even reached a point
where Excel told me it could not save the file. (And exiting Excel does not
regain the memory. You actually have to reboot.)

Thanks very much for great advice, Bob.

Paul




----- Bob Phillips wrote: -----

Paul,

Could I advise you to re-consider. Sometimes, often, it is

better to hide a
sheet rather than unload it, thereby removing that load overhead

if you
re-load in the same session. You can still achieve your

objective using the
Activate event.

Not saying you should, just make sure you are happy which is the

best way
for you.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

"Paul Simon" wrote in

message
...
Thanks very much Bob. Your first paragraph was the clue to my

problem. I
changed from Hide to Unload Me, and now it works perfectly.

Thanks again,
Bob - I appreciate it.
Paul