Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Close Form in Load Event 1

Status
Not open for further replies.

Durkin

Programmer
Nov 6, 2000
304
GB
I've tried using Me.Close() in the load event of a form to shut it down but it throws an exception. Is there any other way?

Durkin
 
add Me.Show() before the Me.Close()

Somtimes, the easy answer is the hardest to find. :)
 
That doesn't seem to work. Also, I'm using Form.Show() from another form which is firing up the Load event so that's pretty much the same thing. Anyway, I'm still getting the same error.

Durkin
 
what is the error it is thorwing?


Somtimes, the easy answer is the hardest to find. :)
 
System.InvalidOperationException:
Cannot call Close() while doing CreateHandle()

Durkin
 
I am led to wonder why you want to open and close a form right away? and have you tried Me.Hide()

Somtimes, the easy answer is the hardest to find. :)
 
A number of reasons. Firstly, if a person has not got permissions to use the form. Also, in the case of a data maintenance form, if the user asks for a non-existent record. There are a few other reasons but you see what I mean.

Durkin
 
well, it sounds like you need some kind of delay so the form can load all the way first. or you might try...

e.cancel=True
me.close

that code works good for a lot of things i do that are similar.

Becca

Somtimes, the easy answer is the hardest to find. :)
 
It sounds like I'm going to have to find some kind of workaround. Not to worry. Thanks for your help.

Durkin
 
Why not test the permissions before you load the form?

--------------------------------------
"We are star-stuff. We are the universe made manifest trying to figure itself out."
-- Delenn in Babylon 5 - "A Distant Star"
 
That's something I may have to look at.

Durkin
 
Durkin,

We also had the same kind of problem and the simple answer is, No we can not use Me.Close() at the load event and it is not a permission issue, so what we did was we created two functions on the base class, one is preLoad and the other is postLoad and we moved all the logic from the Load event to the preload and if everything is succesfull then we set a flag, something like isErrorOnPreLoad depending on the way how preLoad went. And in postLoad we check to see if the flag is TRUE, if so don't close the window, else close the window in postLoad function. So the code would be like this,

Dim oFrm as New frmMain

With ofrm

.preLoad()
.Show()
.postLoad()
End With

And if there was an error in preLoad, then we hide the window and postLoad will then close the window later. It depends on how you want to implement this, because the flag which I was talking about that can be an attribute and can be done this way as well.

Dim oFrm as New frmMain

With ofrm

.preLoad()
IF .isErrorOnPreLoad then Return
.Show()
End With

This way we eleminated postLoad and will work for any type of forms like sheets, response etc. I think this might help you little bit.

-Kris
 
Kris11, thanks you. It's almost exactly what I was looking for. What I have done is put a routine in the base class similar to your 'preload' which will do a bunch of checks and then call the show method if everything is okay. This way saves me showing the form at all if the user has no permissions for it.
Thanks again for your help.

Durkin
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top