referencing the same class objects in two forms

R

rocco

Hello,
I have a button in Form A that will open another form (Form B).
In Form B I have declared a class object:
Public WithEvents Calendario As Calendar
(it is calendar I have created with few feature needed by the application).

I have declared it as public hoping this will allow me to set two of its
properties through code in Form A by coding in Form A module:
DoCmd.OpenForm FormBname, acNormal 'and its module will create the
public Calendario object
Calendario.WhichControl = "contactData"
Calendario.WhichForm = Me.Name

But I get the error that the object doesn't exist.

Then I tried to solve the issue by creating a public calendar object in Form
A and setting its two properties in Form A module:
Public CalendarioST as Calendar

Private Sub Form_Load()
Dim i As Integer
Set CalendarioST = New Calendar
CalendarioST.WhichControl = "contactData"
CalendarioST.WhichForm = Me.Name
'then I open Form B
DoCmd.OpenForm FormBname, acNormal
Set CalendarioST=nothing
End sub

in Form B I tried this:
Private Calendario as Calendar

Private Sub Form_Load()
Dim i As Integer
Set Calendario = New Calendar
Calendario.WhichControl = CalendarioST.WhichControl
Calendario.WhichForm = CalendarioST.WhichForm
End sub

But still I get error! I need to create the object in Form B, but I have to
set two of its properties in Form A. I know I can go through public module
variables, but I would really know why my solutions shouldn't work! They both
seem reasonable to me. Obviously they are not and I would love to learn why.

Thanks!
Rocco
 
D

Dirk Goldgar

rocco said:
Hello,
I have a button in Form A that will open another form (Form B).
In Form B I have declared a class object:
Public WithEvents Calendario As Calendar
(it is calendar I have created with few feature needed by the
application).

I have declared it as public hoping this will allow me to set two of its
properties through code in Form A by coding in Form A module:
DoCmd.OpenForm FormBname, acNormal 'and its module will create the
public Calendario object
Calendario.WhichControl = "contactData"
Calendario.WhichForm = Me.Name

But I get the error that the object doesn't exist.

Then I tried to solve the issue by creating a public calendar object in
Form
A and setting its two properties in Form A module:
Public CalendarioST as Calendar

Private Sub Form_Load()
Dim i As Integer
Set CalendarioST = New Calendar
CalendarioST.WhichControl = "contactData"
CalendarioST.WhichForm = Me.Name
'then I open Form B
DoCmd.OpenForm FormBname, acNormal
Set CalendarioST=nothing
End sub

in Form B I tried this:
Private Calendario as Calendar

Private Sub Form_Load()
Dim i As Integer
Set Calendario = New Calendar
Calendario.WhichControl = CalendarioST.WhichControl
Calendario.WhichForm = CalendarioST.WhichForm
End sub

But still I get error! I need to create the object in Form B, but I have
to
set two of its properties in Form A. I know I can go through public module
variables, but I would really know why my solutions shouldn't work! They
both
seem reasonable to me. Obviously they are not and I would love to learn
why.


Since your Calendar object is declared in form B, any reference to it must
be qualified with a reference to form B. So you should do this:

DoCmd.OpenForm FormBname, acNormal
' assuming that Form B's Open event instantiates the
' calendar object.
With Forms!FormBName.Calendario
.WhichControl = "contactData"
.WhichForm = Me.Name
End With

Note that the code above assumes that the calendar object is instantiated in
the Open event of Form B, not its Open event; e.g.,

' in form B
Private Sub Form_Open()

Set Calendario = New Calendar

End sub
 
D

Dirk Goldgar

Dirk Goldgar said:
Note that the code above assumes that the calendar object is instantiated
in the Open event of Form B, not its Open event

Er, that should have been, "in the Open event of Form B, not its Load
event".
 
T

tom_willpa

high quality Soccer jerseys NBA Jersey tracksuit and jackets, GHD
hairstraightener supplier from www.willpa.com

Are you a Retail businessman who bother by the purchase price? China
Cheapest TOP wholesale website can help you

we are specialize in replica sport goods manufacturing in china, we can
offer you all kinds of soccer jersey, NBA jersey,shoes and so on. they are
the best brand replica goods whih are look the same as the original goods.
excellent quality and steady supply for them. we have been marketed in Europe
and American for 3 year. all the goods we offer are AAA quality. our soccer
jersey are Thailand style. If any goods you buy from my company have problem,
we will refund or resend them again. Most of ourProducts have no minimum
order requirements,soyou can shop retail goods at wholesale prices. if you
can buy more than 300usd. We offer free shipping. The more you buy the more
discount for you.

National soccer jerseys: http://www.willpa.com
Club soccer jerseys: http://www.willpa.com
NBA Jerseys: http://www.willpa.com
T-shirt and shirt: http://www.willpa.com
Tracksuit: http://www.willpa.com
Hoody & Jackets: http://www.willpa.com
UGG boots: http://www.willpa.com
Hair style: http://www.willpa.com
shopping Index: http://www.willpa.com

EMS shipping. 7days arrive, paypal accept

want more information pls contact us or check our website: www.willpa.com
 

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