Instantiate Forms

T

Terry

This might seem like an obvious question, but I am approaching Access forms
from the view of a VB programmer.
So let me explain the issue I have, here goes.

Option Compare Database
Option Explicit
Private m_frmAny as Forms_frmMyForm
Private Sub cmdOpenForm()
Set m_frmAny = new Forms_frmMyForm
m_frmAny.Init
'Now how do I display this instance?
End Sub

Right I instantiate a new instance of the form called Forms_frmMyForm and
then calll
an Initialise routine that does some setting up. So how would I display this
instance?
The only way I have been able to display a form is DoCmd.OpenForm which
opens the form
first!

Any help or ideas?
Many thanks
Terry
 
R

Rick Brandt

Terry said:
This might seem like an obvious question, but I am approaching Access
forms from the view of a VB programmer.
So let me explain the issue I have, here goes.

Option Compare Database
Option Explicit
Private m_frmAny as Forms_frmMyForm
Private Sub cmdOpenForm()
Set m_frmAny = new Forms_frmMyForm
m_frmAny.Init
'Now how do I display this instance?
End Sub

Right I instantiate a new instance of the form called Forms_frmMyForm
and then calll
an Initialise routine that does some setting up. So how would I
display this instance?
The only way I have been able to display a form is DoCmd.OpenForm
which opens the form
first!

Any help or ideas?
Many thanks
Terry

The way to open a form in Access is DoCmd.OpenForm. How were you expecting to
display a form without opening it? Do you mean you want to open it hidden or in
design view? There are arguments for OpenForm that do both of those.

You should be able to put all of your initializing code in either the Open event
or the Load event of the form.
 
T

Terry

Hi Rick,

Well this isssue is that I would like to pass some values to the form before
it displays.
So in VB it would be something like this:
Option Compare Database
Option Explicit
Private m_frmAny as Forms_frmMyForm
Private Sub cmdOpenForm()
Set m_frmAny = new Forms_frmMyForm
m_frmAny.Init "InitValue"
'Now how do I display this instance?
m_frmAny.Show vbModal
End Sub

As I was saying the DoCmd.OpenForm seems to only be able to pass filter
criteria and nothing else.
I think that it will also instantiate another form.
 
N

Nikos Yannacopoulos

Terry,

PMFJI, I've spent a little time playing with VB6.0 and I think I can
uderstand where you're coming from. Well, it's different in Access, you
don't load, manipulate then show a form, you just open it right away and
do whatever is required; it all happens so fast the user doesn't realize.

Regards,
Nikos
 
R

Rick Brandt

Terry said:
Hi Rick,

Well this isssue is that I would like to pass some values to the form
before it displays.
So in VB it would be something like this:
Option Compare Database
Option Explicit
Private m_frmAny as Forms_frmMyForm
Private Sub cmdOpenForm()
Set m_frmAny = new Forms_frmMyForm
m_frmAny.Init "InitValue"
'Now how do I display this instance?
m_frmAny.Show vbModal
End Sub

As I was saying the DoCmd.OpenForm seems to only be able to pass
filter criteria and nothing else.
I think that it will also instantiate another form.

You can use OpenArgs to pass a delimited list of parameters and then use a
parsing routine in the Open event or you can use public variables (or a
single public array variable).

You will be better off assuming by default that the way you did things in VB
will seldom be the way you do them in Access (certainly as far as
forms/controls is concerned).
 
K

Ken Ismert

'Now how do I display this instance?
That works for UserForms and VB forms, but for Access forms, use:

m_frmAny.Visible = True

Modal is a property of the form, and can be changed before display.

OpenArgs is a parameter passing option, but it is inconvenient for
multiple parameters, and doesn't work with the Set New form instance
creation method you are using.

It is perfectly acceptable for you to define an Initialize method on
your child form, and call it with your parameter list before making it
visible.

-Ken
 
A

Albert D.Kallal

Actually, you just need to set the forms visible property to true, and it
will display.
m_frmAny.Init
'Now how do I display this instance?

m_frmAny.Visiable = True

Do note that when you assign the var as:

Set m_frmAny = new Forms_frmMyForm

then the forms on-open, and on-load event will fire. The on-load event is
typically where you Init code goes.

And, since you are from VB land, I don't have to mention that when that var
goes out of scope...the form will close...

Also, do note we have two events for form loads, and with VB you got only
one:

We have got:
on-open
and
on-load

Your init code typically goes in the on-load (you can't set controls etc
that are bound...as the on-open event is too early. however, code can
EXAMINE values in controls). Also, note that the on-open event has a CANCEL
option, and thus you can in fact had code in that event to PREVENT the form
from loading. (this is brilliant design, and something we miss dearly in VB,
as canceling a VB form load is painful - hence, we got two events in place
of the VB load event).

And, if you do cancel a form load, you have to trap the error in the calling
code (code that opens/displays the form)
 

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