Form Instance Problem

R

Rob

Hi all,

I am having some trouble opening multiple instances of forms. I use the
following code to open a form. I am using the NewTaskForm (which is taken
directly from the Access Developers Handbook 97), to create new instances
of forms. I use the same form in different ways (add, veiw, edit) and each
view has different properties (although not significant). The problem is
that when I open the second instance, I am presented with the input box
from my Form_Load event. Does this have something do do with the scope of
the iTaskType variable (the iTaskType variable is declared as a Public
variable in frmTasks)?

Here is the code that opens a new form:

If IsOpen("frmTasks") Then
Call NewTaskForm(cNewTask)
Else
DoCmd.OpenForm "frmTasks", acNormal, , , , acWindowNormal, cNewTask
End If

And the NewTaskForm procedure (from the ADH97 book):

Sub NewTaskForm(iFrmType As Integer)

Dim frm As Form
Set frm = New Form_frmTasks

With frm
.iTaskType = iFrmType
End With

mintI = mintI + 1
colForms.Add Item:=frm, KEY:=frm.hwnd & ""
DoCmd.MoveSize (mintI + 1) * 80, (mintI + 1) * 350
frm.Visible = True
End Sub

And then in the Form_Load event of "frmTasks" I have the following code:

' Allow design of the form
If IsNull(Me.OpenArgs) Then
If iTaskType = 0 Then ' Uninitialised
iTaskType = CInt(InputBox("Enter a form type"))
End If
Else
iTaskType = Me.OpenArgs
End If

Select Case iTaskType
Case cNewTask
Case cViewTask
Case cEditTask etc ...

I have also tried using the iTaskType as a global variable and changing it
before each NewTaskForm call, but I still get the same error.

Any help anybody could provide would be greatly appreciated,

Cheers
Rob
 
R

Rob

Hi all,

I am having some trouble opening multiple instances of forms. I use the
following code to open a form. I am using the NewTaskForm (which is taken
directly from the Access Developers Handbook 97), to create new instances
of forms. I use the same form in different ways (add, veiw, edit) and each
view has different properties (although not significant). The problem is
that when I open the second instance, I am presented with the input box
from my Form_Load event. Does this have something do do with the scope of
the iTaskType variable (the iTaskType variable is declared as a Public
variable in frmTasks)?

Here is the code that opens a new form:

If IsOpen("frmTasks") Then
Call NewTaskForm(cNewTask)
Else
DoCmd.OpenForm "frmTasks", acNormal, , , , acWindowNormal, cNewTask
End If

And the NewTaskForm procedure (from the ADH97 book):

Sub NewTaskForm(iFrmType As Integer)

Dim frm As Form
Set frm = New Form_frmTasks

With frm
.iTaskType = iFrmType
End With

mintI = mintI + 1
colForms.Add Item:=frm, KEY:=frm.hwnd & ""
DoCmd.MoveSize (mintI + 1) * 80, (mintI + 1) * 350
frm.Visible = True
End Sub

And then in the Form_Load event of "frmTasks" I have the following code:

' Allow design of the form
If IsNull(Me.OpenArgs) Then
If iTaskType = 0 Then ' Uninitialised
iTaskType = CInt(InputBox("Enter a form type"))
End If
Else
iTaskType = Me.OpenArgs
End If

Select Case iTaskType
Case cNewTask
Case cViewTask
Case cEditTask etc ...

I have also tried using the iTaskType as a global variable and changing it
before each NewTaskForm call, but I still get the same error.

Any help anybody could provide would be greatly appreciated,

Cheers
Rob

Hi all,

I tried using properties as well (Property Let ..) however it appears that
when you create a New instance of the Form, Form_Open and the Form_Load are
called before I could set any variables. I solved my problem by using
global variables (I had a typo in my code when I tried global variables
before). I don't like global variable though (a product of my education!)
and was wondering would a better way of doing this be to wrap it up in a
class module (although that seems to be additional overhead for little
gain).

Cheers
Rob

P.S Uni was some years ago, and haven't really coded much since leaving
 
B

bcap

It isn't because of the scope of your variable, it's because the Open event
runs before you've assigned a value to the variable.

An alternative to global variables is to have a hidden form in your
application which remains open the whole time, and to have text boxes on it
for your shared values. It has the minor advantage that, unlike globals,
the values are not lost every time a run-time error occurs. Frankly,
though, it's barely more elegant than using globals.

There are some other problems to watch out for if you plan on using multiple
form instances as a matter of course. For a start, although when you
instantiate a new instance you will get a shiny new instance of the code
module with the variables uninitiated, you do *not* get a shiny new set of
form properties: what you get is whatever the cuirrent property values are
in the last instance of the form that you previously opened; if, like me,
you are addicted to messing around with a form's behaviour at run-time, this
is a major issue.

I have also had major problems (in Access 2003) when opening a 3rd or
subsequent instance of a form: sometimes code doesn't run, property settings
are ignored, and the instance just sits there, empty and dumb.

Because of these sorts of problems, I no longer use multiple instances.
 
R

Rob

It isn't because of the scope of your variable, it's because the Open event
runs before you've assigned a value to the variable.

An alternative to global variables is to have a hidden form in your
application which remains open the whole time, and to have text boxes on it
for your shared values. It has the minor advantage that, unlike globals,
the values are not lost every time a run-time error occurs. Frankly,
though, it's barely more elegant than using globals.

There are some other problems to watch out for if you plan on using multiple
form instances as a matter of course. For a start, although when you
instantiate a new instance you will get a shiny new instance of the code
module with the variables uninitiated, you do *not* get a shiny new set of
form properties: what you get is whatever the cuirrent property values are
in the last instance of the form that you previously opened; if, like me,
you are addicted to messing around with a form's behaviour at run-time, this
is a major issue.

I have also had major problems (in Access 2003) when opening a 3rd or
subsequent instance of a form: sometimes code doesn't run, property settings
are ignored, and the instance just sits there, empty and dumb.

Because of these sorts of problems, I no longer use multiple instances.

Hi bcap,

Thanks for your response, and your tips re multiple instances. I do set
all properties at run time, so I will keep an eye on problems with the
forms. I was wondering whether a better way of doing global variables
would be to wrap them up in a class module and encapsulate them there,
although I will be implementing the hidden form now as I am getting tired
of reinitialising globals for all my run time errors :) I will let you know
how I get on,

Cheers
Rob
 
F

Florin Voinescu

Hi Rob and bcap,
In Access 2007 you have temporary variables, a collection that is much more stable than module variables (you don't loose them if something crashes) and they are more elegant than a hidden form.
Look into the TempVars collection.
HTH,
Florin
 
B

bcap

Thank you Florin, I do remember reading about that new feature but I'd
forgotten it. I shall give it a try.
 

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