473,583 Members | 4,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strategy for form instantiation: is it a good idea?

Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotma il.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub
Nov 13 '05 #1
19 3597
I have seen few cases where it was useful to instantiate a form from code,
and those only when multiple copies of the same form were to be displayed
concurrently. The simple way is often best... and the simple way is to
design your form and set its properties in design view, then use
DoCmd.OpenForm to open the single copy that is usually all that is needed.
Access is not your typical programming language that is code intensive, and
that is one of its very strong points.

If there's followup to this thread, I hope someone can pick it up, because I
am going to be out of the newsgroups for several days.

Larry Linson
Microsoft Access MVP
"Raposa Velha" <jm******@hotma il.com> wrote in message
news:69******** *************** ***@posting.goo gle.com...
Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotma il.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub

Nov 13 '05 #2
rkc
Raposa Velha wrote:
Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.


Yes.
What's the point?
Nov 13 '05 #3
You have discovered one of the many limitations of the technique of
instantiating forms using the New keyword. In general, you can find another
way to do the same thing, and avoid the problem.

In the rare cases when the techniquer really does add enough value to
compensate for the added complexity and instability (e.g. the form will close
if the code gets reset), at least make sure you write your code in an
event-driven manner so that you don't need your code to halt and wait for the
form to close. Instead, use events on the form to trigger the follow-up
action.

Note that you can bootstrap a form reference by having the form reference
itself, so it doesn't close when the procedure that opens it exits (the
alternative is some sort of messy open instances collection). Just make sure
the form sets its self-reference to Nothing on Form_Close, so all resources
are freed up.

On 28 Dec 2004 10:52:56 -0800, jm******@hotmai l.com (Raposa Velha) wrote:
Hello to all!

Does any of you want to comment the approach I implement for
instantiatin g a form? A description and an example follow.

Cheers,
RV
jmclopesAThotm ail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub


Nov 13 '05 #4
Thanks Larry.
I see your point about keeping it simple. The main idea of this approach is
having a reusable form that would read some global variables and set some
other global variables. This is the alternative I can image for comunication
among different parts of the application with this generic form. This way,
it requires both the form and the rest of the application to be aware of
these global variables. On the other hand, the use of properties would
overcome the use of global variables.

RV

"Larry Linson" <bo*****@localh ost.not> wrote in message
news:xpnAd.1183 6$tG3.85@trnddc 02...
I have seen few cases where it was useful to instantiate a form from code,
and those only when multiple copies of the same form were to be displayed
concurrently. The simple way is often best... and the simple way is to
design your form and set its properties in design view, then use
DoCmd.OpenForm to open the single copy that is usually all that is needed.
Access is not your typical programming language that is code intensive,
and
that is one of its very strong points.

If there's followup to this thread, I hope someone can pick it up, because
I
am going to be out of the newsgroups for several days.

Larry Linson
Microsoft Access MVP
"Raposa Velha" <jm******@hotma il.com> wrote in message
news:69******** *************** ***@posting.goo gle.com...
Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.

Cheers,
RV
jmclopesAThotma il.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub


Nov 13 '05 #5
The point of commenting ofr what to be commented? :-)

"rkc" <rk*@rochester. yabba.dabba.do. rr.bomb> wrote in message
news:3J******** ***********@twi ster.nyroc.rr.c om...
Raposa Velha wrote:
Hello to all!

Does any of you want to comment the approach I implement for
instantiating a form? A description and an example follow.


Yes.
What's the point?

Nov 13 '05 #6
Hi Steve.
Good tips, thanks!
The instability argument it's a strong one, strong enough to make me give up
of this approach. A pity, because this makes the application more similar to
a set of black boxes. Actually this idea come because I wanted to use the
properties capability. The thing is if I use the DoCmd.OpenForm to open the
form, can I still set the custom properties before opening the form? After
the user presses OK (for example), it's no problem reading the custom
properties: it's just a matter of hiding the form instead of closing it.

RV
"Steve Jorgensen" <no****@nospam. nospam> wrote in message
news:km******** *************** *********@4ax.c om...
You have discovered one of the many limitations of the technique of
instantiating forms using the New keyword. In general, you can find
another
way to do the same thing, and avoid the problem.

In the rare cases when the techniquer really does add enough value to
compensate for the added complexity and instability (e.g. the form will
close
if the code gets reset), at least make sure you write your code in an
event-driven manner so that you don't need your code to halt and wait for
the
form to close. Instead, use events on the form to trigger the follow-up
action.

Note that you can bootstrap a form reference by having the form reference
itself, so it doesn't close when the procedure that opens it exits (the
alternative is some sort of messy open instances collection). Just make
sure
the form sets its self-reference to Nothing on Form_Close, so all
resources
are freed up.

On 28 Dec 2004 10:52:56 -0800, jm******@hotmai l.com (Raposa Velha) wrote:
Hello to all!

Does any of you want to comment the approach I implement for
instantiati ng a form? A description and an example follow.

Cheers,
RV
jmclopesAThot mail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub

Nov 13 '05 #7
Bri

Steve,

Can you elaborate on this 'bootstrap' concept? I currently use a
variation of the 'messy open instances collection' (I use a Public array
variable that gets ReDim'd for each new instance) and didn't know there
was an alternative.

Thanks.

--
Bri

Steve Jorgensen wrote:
You have discovered one of the many limitations of the technique of
instantiating forms using the New keyword. In general, you can find another
way to do the same thing, and avoid the problem.

In the rare cases when the techniquer really does add enough value to
compensate for the added complexity and instability (e.g. the form will close
if the code gets reset), at least make sure you write your code in an
event-driven manner so that you don't need your code to halt and wait for the
form to close. Instead, use events on the form to trigger the follow-up
action.

Note that you can bootstrap a form reference by having the form reference
itself, so it doesn't close when the procedure that opens it exits (the
alternative is some sort of messy open instances collection). Just make sure
the form sets its self-reference to Nothing on Form_Close, so all resources
are freed up.

Nov 13 '05 #8
Bri
Raposa,

You can make a Form variable reference the form after you open it:
Air Code:
Dim frm as Form
DoCmd.Open acForm, "MyForm"
Set frm = Forms!MyForm
frm.Message= "Hello form!"
set frm = Nothing

This will not close the form when the frm is set to nothing. It will,
however, open the Form before applying the Property. This happens fast
enough that the user won't see the difference, but if there is code in
your OnOpen or OnLoad Events that depend on the Property, then those
events won't see it as it hasn't been set yet. An option to this is to
have the post Property code run in a Public Sub in the form, then you
could do this:
Air Code:
Dim frm as Form
DoCmd.Open acForm, "MyForm"
Set frm = Forms!MyForm
frm.Message= "Hello form!"
frm.MySub 'This is the Public Sub in the Form
set frm = Nothing

Hope that helps

--
Bri

Raposa Velha wrote:
Hi Steve.
Good tips, thanks!
The instability argument it's a strong one, strong enough to make me give up
of this approach. A pity, because this makes the application more similar to
a set of black boxes. Actually this idea come because I wanted to use the
properties capability. The thing is if I use the DoCmd.OpenForm to open the
form, can I still set the custom properties before opening the form? After
the user presses OK (for example), it's no problem reading the custom
properties: it's just a matter of hiding the form instead of closing it.

RV
"Steve Jorgensen" <no****@nospam. nospam> wrote in message
news:km******** *************** *********@4ax.c om...
You have discovered one of the many limitations of the technique of
instantiati ng forms using the New keyword. In general, you can find
another
way to do the same thing, and avoid the problem.

In the rare cases when the techniquer really does add enough value to
compensate for the added complexity and instability (e.g. the form will
close
if the code gets reset), at least make sure you write your code in an
event-driven manner so that you don't need your code to halt and wait for
the
form to close. Instead, use events on the form to trigger the follow-up
action.

Note that you can bootstrap a form reference by having the form reference
itself, so it doesn't close when the procedure that opens it exits (the
alternative is some sort of messy open instances collection). Just make
sure
the form sets its self-reference to Nothing on Form_Close, so all
resources
are freed up.

On 28 Dec 2004 10:52:56 -0800, jm******@hotmai l.com (Raposa Velha) wrote:

Hello to all!

Does any of you want to comment the approach I implement for
instantiatin g a form? A description and an example follow.

Cheers,
RV
jmclopesATho tmail.com replace the AT with the thing you know ;-)

After discovering that access 2000 support form properties (I'm a
newbie, alright :-) ), I was pleased with the idea of organizing my
code with some object oriented approach. Something like:

- instantiate a form
- set form properties
- display the form
- read form properties
- do whatever required

Then I realized (at least I didn't find how) that I couldn't open the
form in a way that the procedure execution would stop until the form
is closed. With the following code, the form is displayed but the
procedure continues its execution and, when it ends, the form is
destroyed:

Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.visible=true

MsgBox f.Message
Set f = Nothing
End Sub

So I solved this problem as follows. The idea is after the form is
displayed, the procedure loops while the form is visible. The form is
made not visible when the user presses the OK in that form.
'-------- NEW VERSION of my_proc() -----------
Sub my_proc()
Dim f As New Form_my_form
f.Message = "Hello form!" ' Message it's a property I added to
the form

'The user would enter some text in a textbox,
'which the form returns with the property Message
f.Modal = True ' one can set the form to modal to have a popup
behaviour
f.visible=true
While f.Visible
DoEvents
Wend

MsgBox f.Message
Set f = Nothing
End Sub


'-------- MY_FORM PROCEDURES -----------
Private Sub btnOk_Click()
Me.Visible = False
End Sub

Private Sub Form_Unload(Can cel As Integer)
'Don't unload the form, just hide it so the properties
'are still available to the caller
If Me.Visible Then
'only cancel the unload if the form isn't hidden
Me.Visible = False
Cancel = 1
End If
End Sub


Nov 13 '05 #9
Bri <no*@here.com > wrote in news:oxDAd.6023 27$Pl.99862@pd7 tw1no:
Can you elaborate on this 'bootstrap' concept? I currently use a
variation of the 'messy open instances collection' (I use a Public
array variable that gets ReDim'd for each new instance) and didn't
know there was an alternative.


Have you considered a class module, which would automatically
re-initialize the stored values when it is re-instantiated?

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1971
by: Claudio Jolowicz | last post by:
I am trying to find a solution to the following design problem (code at the bottom): We are implementing a trader agent that can trade with other traders on an electronical trading platform. To make the trader more extensible, we have defined a strategy interface and implemented this interface for different trading strategies. The problem...
7
1685
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
4
1655
by: Ney André de Mello Zunino | last post by:
Hello. Given a sorted collection of strings, what would a good (the best?) strategy be to allow fast access to an item, based on a search substring which should match the beginning of the searched item? The actual goal is to implement a functionality similar to that found in help indices, where one can locate an item by gradually typing its...
12
2608
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in an embedded environment with multiple processors. I created a policy for classes, which, I had hoped, would automatically register the class with...
4
3284
by: Koen | last post by:
Hi all, At work I created a database which is really helpful. The database is used by approx 15 users. Everything worked great, until I added some 'scoreboard' forms and reports. I get the "Cannot open any more databases" error. The 'scoreboard' form show a matrix of 6 columns, 7 rows. Each cell is calculated separate by (what I call...
17
8599
by: Barret Bonden | last post by:
As an old programmer just now looking at VB.net I have a question: How does one simply open one form from another ? I don't mean how does one create a new instance of that form , but rather how to refer to THAT form ? And having done this, how does one get data to and from that form ? I have read the MS tutorial on this but it is...
7
1273
by: Marco | last post by:
The subject pretty much says it all. I need to create a form like the msgbox that returns a value, how would I go about doing this?
0
1355
by: ltruett | last post by:
I'm almost done my series of design patterns using PHP 5. Today's pattern is the Strategy Pattern. http://www.fluffycat.com/PHP-Design-Patterns/Strategy/ In the Stratedy Pattern a "family of algorythms" is used interchangably by calling clients. This is the second pattern in as many days as I've used my older work with patterns in...
25
3382
by: marcin.rzeznicki | last post by:
Hello everyone I've got a little problem with choosing the best decoding strategy for some nasty problem. I have to deal with very large files wich contain text encoded with various encodings. Their length makes loading contents of file into memory in single run inappropriate. I solved this problem by implementing memory mapping using...
0
7894
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7825
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8179
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8191
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3816
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2331
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1431
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1155
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.