469,283 Members | 2,349 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,283 developers. It's quick & easy.

Accessing the Created Instance of a form in my modules

Hello,

I need to create a single (new) instance of a FORM and have that instance
available to be called from my modules and some other forms. This is how I
launch it, from <FormAnyform>

Option Explicit
Dim WithEvents frmNew as myExistingForm
---------------------------

Private Sub InitializeNewStuff()
Set frmNew = New frmSomething
frmNew.Hide
End Function

I can't use frmSomething.Show, as I normally do and need to
create an instance (to work with a UserControl and Events)
but now I cannot access frmNew form my modules...

For example frmNew.Caption="Hello" cannot happen as
frmNew is not "public".

If I try frmSomething.Caption="Hello" then I launch a NEW, second
instance of frmSomething.... this is bad.

How can I set frmNew to a Global so I can just use frmNew after it
is created throughout my program?

Thanks
-stone

because of a UserControl and requirement of RaiseEvents)


Jul 17 '05 #1
7 2786

"Stone" <x@y.com> wrote in message
news:t4*****************@news4.srv.hcvlny.cv.net.. .
Hello,

I need to create a single (new) instance of a FORM and have that instance available to be called from my modules and some other forms. This is how I launch it, from <FormAnyform>

Option Explicit
Dim WithEvents frmNew as myExistingForm
---------------------------

Private Sub InitializeNewStuff()
Set frmNew = New frmSomething
frmNew.Hide
End Function

I can't use frmSomething.Show, as I normally do and need to
create an instance (to work with a UserControl and Events)
but now I cannot access frmNew form my modules...

For example frmNew.Caption="Hello" cannot happen as
frmNew is not "public".

If I try frmSomething.Caption="Hello" then I launch a NEW, second instance of frmSomething.... this is bad.

How can I set frmNew to a Global so I can just use frmNew after it is created throughout my program?


I suppose you can also set a reference to a global variable in
that module. Be sure to clear it, though, as it could prevent
the form from being unloaded.
Jul 17 '05 #2
Sorry... I'm kind stupid..

I tried setting in a module

Public frmNew as frmSomething

in a module but it never gets referenced to the instance I made.
How do I set the tie this Public to the new instance?

Thanks-
-stone

Grinder wrote in message ...


I suppose you can also set a reference to a global variable in
that module. Be sure to clear it, though, as it could prevent
the form from being unloaded.

Jul 17 '05 #3
Here's one way....
'=================Form1 Code
Option Explicit

Private WithEvents frmNew As myExistingForm

Private Sub Command1_Click()
Set frmNew = NewForm
frmNew.Show
End Sub

Private Sub frmNew_Test()
MsgBox "Fired Event!!!"
End Sub

Private Sub frmNew_UnloadFired()
Set NewForm = Nothing 'clear the refs
Set frmNew = Nothing
End Sub
'=================myExistingForm Code
Option Explicit

Public Event Test()
Public Event UnloadFired()

Private Sub Command1_Click()
RaiseEvent Test
End Sub

Private Sub Form_Unload(Cancel As Integer)
RaiseEvent UnloadFired
End Sub
'=================Module1 Code
Option Explicit

Private mobjNewForm As myExistingForm

Public Property Get NewForm() As myExistingForm
If mobjNewForm Is Nothing Then
Set mobjNewForm = New myExistingForm
End If
Set NewForm = mobjNewForm
End Property

Public Property Set NewForm(ByRef NewSetting As myExistingForm)
'This is basically to clear the object variable but
'will also allow you to do other stuff with it if you want.
Set mobjNewForm = NewSetting
End Property
'=================
--
Ken Halter - MS-MVP-VB - http://www.vbsight.com
Please keep it in the groups..
"Stone" <x@y.com> wrote in message news:t4*****************@news4.srv.hcvlny.cv.net.. .
Hello,

I need to create a single (new) instance of a FORM and have that instance
available to be called from my modules and some other forms. This is how I
launch it, from <FormAnyform>

Option Explicit
Dim WithEvents frmNew as myExistingForm
---------------------------

Private Sub InitializeNewStuff()
Set frmNew = New frmSomething
frmNew.Hide
End Function

I can't use frmSomething.Show, as I normally do and need to
create an instance (to work with a UserControl and Events)
but now I cannot access frmNew form my modules...

For example frmNew.Caption="Hello" cannot happen as
frmNew is not "public".

If I try frmSomething.Caption="Hello" then I launch a NEW, second
instance of frmSomething.... this is bad.

How can I set frmNew to a Global so I can just use frmNew after it
is created throughout my program?

Thanks
-stone

because of a UserControl and requirement of RaiseEvents)


Jul 17 '05 #4

"Stone" <x@y.com> wrote in message
news:gi******************@news4.srv.hcvlny.cv.net. ..
Sorry... I'm kind stupid..

I tried setting in a module

Public frmNew as frmSomething

in a module but it never gets referenced to the instance I made. How do I set the tie this Public to the new instance?


I wouldn't name it frmNew, if that's also the name of a form in
your project. Be that as it may:

Set frmNew = MyNewInstance

Jul 17 '05 #5
Thanks..

When I set this I can access the form Methods via myNewInstance
(good!) however at runtime I get

Object Variable or With Variable not set (ERR91)

in forms and modules within the program when the code it executed.

myNewInstance.Caption="Hello"

Is an example:

Thanks for the info, I must have missed something in your answer....
b.t.w. .. I impleted the code from Ken Halter in this post and it worked
perfectly..

Grinder wrote in message <##**************@TK2MSFTNGP09.phx.gbl>...
How do I set the tie this Public to the new instance?


I wouldn't name it frmNew, if that's also the name of a form in
your project. Be that as it may:

Set frmNew = MyNewInstance

Jul 17 '05 #6
Ken... Thanks, worked like a charm.
To unload without crashing I needed to change the exit strategy a bit.

I the sequence to get rid of the program I used...

Set NewForm = Nothing
Unload frmNew

if I used

Set NewForm = Nothing
Set frmNew=Nothing

then VB crashed on exist.
Other than that EVERY is perfecty and my User Control looks good now!

Ken Halter wrote in message ...
Here's one way....
'=================Form1 Code
Option Explicit

Private WithEvents frmNew As myExistingForm

.......
Jul 17 '05 #7

"Stone" <x@y.com> wrote in message
news:ut********************@news4.srv.hcvlny.cv.ne t...
Thanks..

When I set this I can access the form Methods via myNewInstance (good!) however at runtime I get

Object Variable or With Variable not set (ERR91)

in forms and modules within the program when the code it executed.
myNewInstance.Caption="Hello"


That means that myNewInstance has not be set. Surprisingly,
the error message is accurate *and* explicit. If that is your
global variable, then you'll have to set it after you
instantiate the form.

In your example code, you were using a local variable to
instantiate your form. I suggested that you set it to a global
variable, as per your requirements, so that it would fit in
with what you were already doing. You can simply instantiate
the form by using the global variable though:

Set myNewInstance = New WhateverYourFormTypeIs

Jul 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Vivek Sharma | last post: by
3 posts views Thread by =?Utf-8?B?dmJ0cnlpbmc=?= | last post: by
8 posts views Thread by Eric B. | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.