By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,163 Members | 934 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,163 IT Pros & Developers. It's quick & easy.

how to declare only once

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.