473,549 Members | 2,734 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1950
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_frmStudent s ' Could just say As Form
Set StudentsForm = Forms!frmStuden ts
End Function

Public Function StudentsFormSub () As Form_frmStudent sSub ' Name of Form object
Set StudentsFormSub = StudentsForm!fr mSubform.Form
End Function

Public Function StudentsQuantit y() As TextBox
Set StudentsQuantit y = 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.c om...
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_frmStudent s ' Could just say As Form Set StudentsForm = Forms!frmStuden ts
End Function

Public Function StudentsFormSub () As Form_frmStudent sSub ' Name of Form object Set StudentsFormSub = StudentsForm!fr mSubform.Form
End Function

Public Function StudentsQuantit y() As TextBox
Set StudentsQuantit y = 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.w ave.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_frmStudent s ' Could just say As

Form
Set StudentsForm = Forms!frmStuden ts
End Function

Public Function StudentsFormSub () As Form_frmStudent sSub ' Name of Form

object
Set StudentsFormSub = StudentsForm!fr mSubform.Form
End Function

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


Nov 12 '05 #5
Steve Jorgensen <no****@nospam. nospam> wrote in
news:t2******** *************** *********@4ax.c om:
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********@bwa y.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.c om...

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.w ave.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
1263
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 add this (which I need to get the file association icons): Private Declare Ansi Function SHGetFileInfo Lib "shell32.dll" (ByVal pszPath As String,...
15
5310
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 private string myArray;
10
2142
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 = File.CreateText(Application.StartupPath & "\Update.txt") The first time my Sub runs it works fine. But the second time it fails telling me "Cannot write to a...
3
1695
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 use ByRef? Public Declare Auto Function SendMessageRef Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByRef...
5
13833
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' DECLARE ic_uv_cursor CURSOR FOR @cursor
6
3324
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: foo(char *args) { /* implementation excluded }; // ok int main(int argc,char *argv) {
27
5476
by: arkmancn | last post by:
Any comments? thanks. Jim
0
2789
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 (SELECT COL3 FROM TABLE2) I want to do this in procedural SQL, but I only want to use one cursor. So to do this I have declared a cursor for...
2
1248
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 msgbox to pop up. This is working. Instead of saying: if lencounter >10 then Msgbox "You have reached the maxium # of characters" I would...
0
7524
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
7451
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
7720
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. ...
1
7475
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5372
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3483
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1944
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
1061
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
766
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.