[AccessD] Public Variables - Scope and Lifetime

Hale, Jim Jim.Hale at FleetPride.com
Mon Nov 28 08:49:08 CST 2005


In this context what does division by zero do? (ie on error resume next;
x=x/0; ) Does it have the same effect as an End? As I recall (but this was
from a discussion looong ago) all globals are cleared with this error.
Jim Hale

-----Original Message-----
From: DWUTKA at marlow.com [mailto:DWUTKA at marlow.com]
Sent: Saturday, November 26, 2005 11:32 PM
To: accessd at databaseadvisors.com
Subject: Re: [AccessD] Public Variables - Scope and Lifetime


Change the stop to an End.  Stop only 'pauses' the code, it doesn't stop
it's execution.  End clears the value, just as it would with a global
variable.

Drew

	-----Original Message-----
	From:	Arthur Fuller [SMTP:artful at rogers.com]
	Sent:	Saturday, November 26, 2005 3:46 PM
	To:	'Access Developers discussion and problem solving'
	Subject:	Re: [AccessD] Public Variables - Scope and Lifetime

	Your reply made me decide to test static functions against a STOP,
which I
	have never done before.
	Turns out that static values are preserved despite a STOP. Code
follows. 
	Place this in a new module:
	<code>
	Static Function CurrentValue(Optional lngNew As Long) As Long

	    Dim lngCurrent As Long
	    On Error GoTo CurrentValue_Error

	    If lngNew <> 0 Then lngCurrent = lngNew
	    CurrentValue = lngCurrent
	    #If conDebug = 1 Then
	        Debug.Print "Current Value: ", CurrentValue
	    #End If

	    On Error GoTo 0
	    Exit Function

	CurrentValue_Error:
	    MsgBox "Error " & Err.Number & " (" & Err.Description & ")" &
vbCrLf & _
	    "in procedure CurrentValue of Module CurrentValues"

	End Function</code>
	Place this in a new module:
	<code>
	Option Compare Database
	Option Explicit

	Sub TestPersistence()
	    CurrentValue (123)
	    MsgBox "Current value is: " & CurrentValue(), vbInformation +
vbOKOnly,
	"Test Persistence of Static Values"
	    Stop
	    MsgBox "Current value is: " & CurrentValue()
	End Sub
	</code>
	Compile, then run the sub above. At the STOP, hit F5 to continue.
The moral
	of the story is that if you want values to persist beyond a STOP,
use static
	functions.
	Arthur
	-----Original Message-----
	From: accessd-bounces at databaseadvisors.com
	[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of
DWUTKA at marlow.com
	Sent: November 26, 2005 1:42 PM
	To: accessd at databaseadvisors.com
	Subject: Re: [AccessD] Public Variables - Scope and Lifetime

	Ugh....not this again!  LOL.  Marty, that is not what happens.  If
you STOP
	your code, then everything gets reset.  That does include your
global
	variables.  It's not a maybe, and they don't 'lose' their values,
the code
	is stopped.  It is just like stopping an .exe, all values in memory
are
	cleared.  The difference is, with Access, that the Access Shell
didn't stop,
	and as soon as you do something requiring code, Access starts it all
up
	again, and so your Globals will be reset.
	It's not a wishy washy thing, it is just how the code works.
	Drew

	-- 
	AccessD mailing list
	AccessD at databaseadvisors.com
	http://databaseadvisors.com/mailman/listinfo/accessd
	Website: http://www.databaseadvisors.com
-- 
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

***********************************************************************
The information transmitted is intended solely for the individual or
entity to which it is addressed and may contain confidential and/or
privileged material. Any review, retransmission, dissemination or
other use of or taking action in reliance upon this information by
persons or entities other than the intended recipient is prohibited.
If you have received this email in error please contact the sender and
delete the material from any computer. As a recipient of this email,
you are responsible for screening its contents and the contents of any
attachments for the presence of viruses. No liability is accepted for
any damages caused by any virus transmitted by this email.


More information about the AccessD mailing list