473,405 Members | 2,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

how to declare only once

In my codes i am often referring to one and the same controls.Is it
possible
to declare these controls only once and then insert them in all my
functions and subs?
For example, i am often referring to these controls:
Set main = [Forms]![frmStudents]
Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]

How and where can i declare them, say as constants?
Nov 12 '05 #1
8 1942
On 14 Feb 2004 05:53:07 -0800, Johm wrote:
In my codes i am often referring to one and the same controls.Is it
possible
to declare these controls only once and then insert them in all my
functions and subs?
For example, i am often referring to these controls:
Set main = [Forms]![frmStudents]
Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]

How and where can i declare them, say as constants?


In the declarations section of any public module (at the top before any
code), dim the variables for these objects as public ie.

Public main as Form

Then in a proceedure that happens when the database first starts up, set
them to the objects they define. As long as the object is open at the time
of the Set statement, the variables will be set. Be carefult though, if the
form closes, the variables will no longer be valid as the object no longer
exists in the runtime environment. Also, if for any reason the project is
reset, the variables will be destroyed as well. When assingning objects to
variables, you're better off doing so when you need them as opposed to
trying to keep the scope indefinitely.

--
Mike Storr
veraccess.com
Nov 12 '05 #2
On 14 Feb 2004 05:53:07 -0800, ke**@abv.bg (Johm) wrote:
In my codes i am often referring to one and the same controls.Is it
possible
to declare these controls only once and then insert them in all my
functions and subs?
For example, i am often referring to these controls:
Set main = [Forms]![frmStudents]
Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]

How and where can i declare them, say as constants?


You can, if you want, simply initialize them once, and hold them in public
variables. I find, however, that this makes debugging applications very
difficult, because if you ever reset the code (say, by stopping a procedure in
break mode), then the variables are cleared, and you have to do something to
get them reinitialized.

The better way to handle this kind of thing is with global functions.
Ideally, these functions would also check for specific error codes, and take
specific action if the objects are not open, such as opening them, but I'll
keep is simple for this example.

Public Function StudentsForm() As Form_frmStudents ' Could just say As Form
Set StudentsForm = Forms!frmStudents
End Function

Public Function StudentsFormSub() As Form_frmStudentsSub ' Name of Form object
Set StudentsFormSub= StudentsForm!frmSubform.Form
End Function

Public Function StudentsQuantity() As TextBox
Set StudentsQuantity = StudentsFormSub()!Quantity
End Function
Nov 12 '05 #3
CDB
Following some tips from MichKa, and using a class, I use this technique for
the few globals that I carry:

Public Property Get logfile() As String
If (IniLogFile = vbNullString) Then
IniLogFile = IniVal.DataPath & cbProject & "log.txt"
End If
logfile = IniLogFile
End Property

This checks that there is a value for the desired property of the class, and
fills it if not, then returns the value to the call. (X = IniVal.logfile.)
After a debug incident, any call for an IniVal property value will cause the
entire set to be re-established.

Clive
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:vj********************************@4ax.com...
On 14 Feb 2004 05:53:07 -0800, ke**@abv.bg (Johm) wrote:
In my codes i am often referring to one and the same controls.Is it
possible
to declare these controls only once and then insert them in all my
functions and subs?
For example, i am often referring to these controls:
Set main = [Forms]![frmStudents]
Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]

How and where can i declare them, say as constants?
You can, if you want, simply initialize them once, and hold them in public
variables. I find, however, that this makes debugging applications very
difficult, because if you ever reset the code (say, by stopping a

procedure in break mode), then the variables are cleared, and you have to do something to get them reinitialized.

The better way to handle this kind of thing is with global functions.
Ideally, these functions would also check for specific error codes, and take specific action if the objects are not open, such as opening them, but I'll keep is simple for this example.

Public Function StudentsForm() As Form_frmStudents ' Could just say As Form Set StudentsForm = Forms!frmStudents
End Function

Public Function StudentsFormSub() As Form_frmStudentsSub ' Name of Form object Set StudentsFormSub= StudentsForm!frmSubform.Form
End Function

Public Function StudentsQuantity() As TextBox
Set StudentsQuantity = StudentsFormSub()!Quantity
End Function

Nov 12 '05 #4
Sure, but in this case, the form could also be closed, and the variable still
not be Nothing, just invalid. So, more complex code would be required to
safely cache the reference.

On Sun, 15 Feb 2004 12:02:52 +1300, "CDB" <al***@delete.wave.co.nz> wrote:
Following some tips from MichKa, and using a class, I use this technique for
the few globals that I carry:

Public Property Get logfile() As String
If (IniLogFile = vbNullString) Then
IniLogFile = IniVal.DataPath & cbProject & "log.txt"
End If
logfile = IniLogFile
End Property

This checks that there is a value for the desired property of the class, and
fills it if not, then returns the value to the call. (X = IniVal.logfile.)
After a debug incident, any call for an IniVal property value will cause the
entire set to be re-established.

Clive
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:vj********************************@4ax.com.. .
On 14 Feb 2004 05:53:07 -0800, ke**@abv.bg (Johm) wrote:
>In my codes i am often referring to one and the same controls.Is it
>possible
>to declare these controls only once and then insert them in all my
>functions and subs?
>For example, i am often referring to these controls:
>
>
>Set main = [Forms]![frmStudents]
>Set MySubform = [Forms]![frmStudents]![frmSubform].[Form]
>Set Quantity = [Forms]![frmStudents]![frmSubform].[Form].[Quantity]
>
>How and where can i declare them, say as constants?


You can, if you want, simply initialize them once, and hold them in public
variables. I find, however, that this makes debugging applications very
difficult, because if you ever reset the code (say, by stopping a

procedure in
break mode), then the variables are cleared, and you have to do something

to
get them reinitialized.

The better way to handle this kind of thing is with global functions.
Ideally, these functions would also check for specific error codes, and

take
specific action if the objects are not open, such as opening them, but

I'll
keep is simple for this example.

Public Function StudentsForm() As Form_frmStudents ' Could just say As

Form
Set StudentsForm = Forms!frmStudents
End Function

Public Function StudentsFormSub() As Form_frmStudentsSub ' Name of Form

object
Set StudentsFormSub= StudentsForm!frmSubform.Form
End Function

Public Function StudentsQuantity() As TextBox
Set StudentsQuantity = StudentsFormSub()!Quantity
End Function


Nov 12 '05 #5
Steve Jorgensen <no****@nospam.nospam> wrote in
news:t2********************************@4ax.com:
Sure, but in this case, the form could also be closed, and the
variable still not be Nothing, just invalid. So, more complex
code would be required to safely cache the reference.


I think I'd use a class module for this.

Whether or not it would be generic or not, I don't know.

I'm not sure I see the need for a global pointing to an open form.

I can see it within code that repeatedly refers to a form, but that
can often be managed with a WITH structure (the code in the WITH
structure is nonsensical, of course):

With Forms!myForm
.Recordsource = [SQL]
If .Dirty Then .Dirty = False
[etc.]
End With

When needing to work with subforms repeatedly, I often declare a
form variable within a subroutine, or use a WITH structure.

But I can't quite think of a context in which code would truly
benefit from a globally available reference to a form, unless you've
already wrapped such a form in a class.

--
David W. Fenton http://www.bway.net/~dfenton
dfenton at bway dot net http://www.bway.net/~dfassoc
Nov 12 '05 #6
I thought I had a case where I had done that, but I now realize that it's a
slightly different case. In my case, I wanted to have a global function to
determine onformation about the active company, and the active company is
determined by the current row in a form. This is a different case, though,
because the calling code doesn't know or care how the active company is
determined, and the function returns a company info object, not the form
reference.

So - I'm thinking you're right. If there's a need to make anything global, it
should also be abstract, like a company id or a company info class, and not
specific, GUI object reference.

On Sat, 14 Feb 2004 23:38:34 GMT, "David W. Fenton"
<dX********@bway.net.invalid> wrote:
Steve Jorgensen <no****@nospam.nospam> wrote in
news:t2********************************@4ax.com :
Sure, but in this case, the form could also be closed, and the
variable still not be Nothing, just invalid. So, more complex
code would be required to safely cache the reference.


I think I'd use a class module for this.

Whether or not it would be generic or not, I don't know.

I'm not sure I see the need for a global pointing to an open form.

I can see it within code that repeatedly refers to a form, but that
can often be managed with a WITH structure (the code in the WITH
structure is nonsensical, of course):

With Forms!myForm
.Recordsource = [SQL]
If .Dirty Then .Dirty = False
[etc.]
End With

When needing to work with subforms repeatedly, I often declare a
form variable within a subroutine, or use a WITH structure.

But I can't quite think of a context in which code would truly
benefit from a globally available reference to a form, unless you've
already wrapped such a form in a class.


Nov 12 '05 #7
CDB
Agreed. I was talking of the principle of validating a variable before using
its contents.

Clive
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:c0********************************@4ax.com...

So - I'm thinking you're right. If there's a need to make anything global, it should also be abstract, like a company id or a company info class, and not specific, GUI object reference.


Nov 12 '05 #8
On Sun, 15 Feb 2004 16:25:39 +1300, "CDB" <al***@delete.wave.co.nz> wrote:
Agreed. I was talking of the principle of validating a variable before using
its contents.

Clive
"Steve Jorgensen" <no****@nospam.nospam> wrote in message
news:c0********************************@4ax.com.. .

So - I'm thinking you're right. If there's a need to make anything

global, it
should also be abstract, like a company id or a company info class, and

not
specific, GUI object reference.



I don't think any of us thought you were either agreeing or disagreeing. It
was clear you were just adding a point about caching global data and checking
it for validity.
Nov 12 '05 #9

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

Similar topics

1
by: Bernie Yaeger | last post by:
I have a simple mailsys solution that works splendidly. I decided to add a listview with file association icons to replace a listbox that displays the full path only of an attachment. But when I...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
10
by: Bob Hollness | last post by:
Hi all. I have a Sub that calls another sub. Both subs use a common object, so I used Public to declare it at the top of the module, as below. Public Writer As StreamWriter =...
3
by: Just Me | last post by:
I declare the following and use it if the msg returns values. I also declare an SendMessage using ByVal for other calls. Isn't there a way I can used the SendMessage and in the call tell it to...
5
by: peppi911 | last post by:
Hi, is it possible to create a cursor from a dynamic string? Like: DECLARE @cursor nvarchar(1000) SET @cursor = N'SELECT product.product_id FROM product WHERE fund_amt > 0' ...
6
by: CJ | last post by:
Functions can accept "argv like" variable definitions, i.e. foo(int argc, char *argv), which was defined/initialized in the C start up stubs, but we can't declare one for our own use. Example: ...
27
by: arkmancn | last post by:
Any comments? thanks. Jim
0
by: FLANDERS | last post by:
Hi all, Is it possible to declare a SQL type of result set or similar? I want to do use the IN predicate like you can in a non-procedural SQL like this: UPDATE TABLE1 SET COL1 = 123 WHERE COL2 IN...
2
by: apank | last post by:
I am trying to declare a maximum # of characters that can be inputted into multiple Memo fields. I have a len counter displaying the # of characters. When the count gets to 10 lets say I want a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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...
0
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.