
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | RE: Make instant of a Class persistent for whole session. Hi Simon,
Why can't you just declare the variables that you wish to persist as
globals, and set their value in the function that you run from your autoexec
macro. I really don't see the need to instantiate a class to accomplish this
goal. Something like this for determining the users NTUserID, and storing it
in the global variable named gstrUserName. Then just call the fOSUserName
function from your Autoexec macro.
Module name: basUtilities
Within this module:
' Get Login name
' http://www.mvps.org/access/api/api0008.htm
Option Compare Database
Option Explicit
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Public gstrUserName As String
Function fOSUserName() As String
On Error GoTo ProcError
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If (lngX > 0) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
gstrUserName = fOSUserName
ExitProc:
Exit Function
ProcError:
MsgBox "Error " & Err.Number & ": " & Err.Description, _
vbCritical, "Error in procedure fOSUserName..."
gstrUserName = "Unknown"
Resume ExitProc
End Function
Tom Wickerath
Microsoft Access MVP http://www.access.qbuilt.com/html/ex...tributors.html http://www.access.qbuilt.com/html/search.htmlReference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
__________________________________________
"sime" wrote:
> Hello
>
> When my database opens, I use Autoexec to run a function. In that
> function I want to instantiate a class and have that instance remain
> accessible for the whole session.
>
> Specifically I am using the class to make variables efficiently
> available to any other code.
>
> I can do this with a form that hides itself - is that the only way to
> do this? It seems a bit silly, but hey if that's the way I should do it Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
> I won't complain. 
>
> Thanks
> Simon
>
> | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. Yes, that does the trick. And it works with Objects.
I had tried "Global" inside the class module, and I had tried your Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
suggestion with with things like "Static" and "Global Static". So Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
close!
Thanks heaps
Simon
Tom Wickerath wrote:
> Hi Simon,
>
> Why can't you just declare the variables that you wish to persist as
> globals, and set their value in the function that you run from your autoexec
> macro. | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. Hi Simon,
You're welcome.
The only other thing you might do, just to help ensure that your global Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
variable will always have a value, is to use the Len function to ensure this
just prior to an attempt to reference it. Later versions of Access have been
much better than earlier versions about not losing global variables, however,
it is possible to lose the value if you are working in the Visual Basic
Environment, or it may not have been set in the first place if you opened
your database with the Shift key depressed in order to bypass the Autoexec
file. Here is an example:
If Len(gstrUserName) = 0 Then Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
fOSUserName ' Reinitialize value
End If
Select Case gstrUserName
Case blahblahblah
Do this
Case Else
Do that
End Select
This way, if the test for the length of your global variable is zero, a call
is made to re-initialize it just prior to attempting to do something with it,
such as Select Case.
Tom Wickerath
Microsoft Access MVP http://www.access.qbuilt.com/html/ex...tributors.html http://www.access.qbuilt.com/html/search.html
__________________________________________
"sime" wrote:
> Yes, that does the trick. And it works with Objects.
>
> I had tried "Global" inside the class module, and I had tried your
> suggestion with with things like "Static" and "Global Static". So
> close!
>
> Thanks heaps
> Simon | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. Hehe, I came across this issue only an hour later. I was wondering if
there were any circumstances where the Global would lose it's value and
a bit of searching revealed that it can happen quite easily unless you
are careful with error handling (which I'm not) or creating an MDE
(which I'm not).
I have decided to take the "easy" option and have a hidden form. And
while I'm there I've turned it into a variable viewer and made it
visible during development.
Tom Wickerath wrote:
> Hi Simon,
>
> You're welcome. Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
>
> The only other thing you might do, just to help ensure that your global
> variable will always have a value, is to use the Len function to ensure this
> just prior to an attempt to reference it. Later versions of Access have been Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
> much better than earlier versions about not losing global variables, however,
> it is possible to lose the value if you are working in the Visual Basic
> Environment, or it may not have been set in the first place if you opened
> your database with the Shift key depressed in order to bypass the Autoexec
> file. Here is an example:
> | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. sime wrote:
> Hello
>
> When my database opens, I use Autoexec to run a function. In that
> function I want to instantiate a class and have that instance remain
> accessible for the whole session.
>
> Specifically I am using the class to make variables efficiently
> available to any other code.
>
> I can do this with a form that hides itself - is that the only way to
> do this? It seems a bit silly, but hey if that's the way I should do it
> I won't complain. 
>
> Thanks
> Simon
>
I might be missing something, but what about instantiating the class at
the top of the module in which the function is called? This assumes the
function is in a standard module.
Module MyMod
============
Option Compare Database Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
Option Explicit
Set TheClass = New MyClass
------------------------------ Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
Function AutoexecFunction()
blah blah blah
End Function
Don't forget to Set TheClass = Nothing before you close everything up.
--
Smartin | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. "Smartin" wrote in message
news:K7KdnarfULdc7FjZnZ2dnUVZ_o2dnZ2d@giganews.com Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
>
> I might be missing something, but what about instantiating the class Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
> at the top of the module in which the function is called? This
> assumes the function is in a standard module.
>
> Module MyMod
> ============
> Option Compare Database
> Option Explicit
>
> Set TheClass = New MyClass
> ------------------------------
> Function AutoexecFunction()
> blah blah blah
> End Function
>
> Don't forget to Set TheClass = Nothing before you close everything up.
Won't work. Executable statements must be enclosed in procedures; they
can't just sit at the top of a module. And if they could, what event
would cause them to be executed?
--
Dirk Goldgar, MS Access MVP www.datagnostics.com
(please reply to the newsgroup) | 
28-Jul-2006, 08:40 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. Dirk Goldgar wrote:
> "Smartin" wrote in message
> news:K7KdnarfULdc7FjZnZ2dnUVZ_o2dnZ2d@giganews.com
>> I might be missing something, but what about instantiating the class
>> at the top of the module in which the function is called? This
>> assumes the function is in a standard module.
>>
>> Module MyMod
>> ============
>> Option Compare Database
>> Option Explicit
>>
>> Set TheClass = New MyClass
>> ------------------------------
>> Function AutoexecFunction()
>> blah blah blah
>> End Function
>>
>> Don't forget to Set TheClass = Nothing before you close everything up.
>
> Won't work. Executable statements must be enclosed in procedures; they Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
> can't just sit at the top of a module. And if they could, what event
> would cause them to be executed?
>
I thought I was doing something similar at work, but I will check the
code tomorrow to be sure and clarify or withdraw my proposition. Thanks
for the heads-up.
--
Smartin | 
28-Jul-2006, 08:41 AM
|  | Guest | | | | | | | | | | Re: Make instant of a Class persistent for whole session. Smartin wrote:
> Dirk Goldgar wrote:
>> "Smartin" wrote in message
>> news:K7KdnarfULdc7FjZnZ2dnUVZ_o2dnZ2d@giganews.com
>>> I might be missing something, but what about instantiating the class
>>> at the top of the module in which the function is called? This
>>> assumes the function is in a standard module.
>>>
>>> Module MyMod
>>> ============
>>> Option Compare Database
>>> Option Explicit
>>>
>>> Set TheClass = New MyClass
>>> ------------------------------
>>> Function AutoexecFunction() Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
>>> blah blah blah
>>> End Function
>>>
>>> Don't forget to Set TheClass = Nothing before you close everything up.
>>
>> Won't work. Executable statements must be enclosed in procedures; they
>> can't just sit at the top of a module. And if they could, what event
>> would cause them to be executed?
>>
>
> I thought I was doing something similar at work, but I will check the
> code tomorrow to be sure and clarify or withdraw my proposition. Thanks
> for the heads-up.
>Reference:: Sikh Philosophy Network http://www.sikhphilosophy.net/showthread.php?t=13794
My error, the declaration I am using is actually
Option Compare Database
Option Explicit
Public TheClass As New MyClass
--------------------------------------
Works great. The class is accessible to other modules and forms.
--
Smartin | 
Support Us! Become a Promoter! | | Gurfateh ji, you can become a SPN Promoter by Donating as little as $10 each month. With limited resources & high operational costs, your donations make it possible for us to deliver a quality website and spread the teachings of the Sri Guru Granth Sahib Ji, to serve & uplift humanity. Every contribution counts. Donate Generously. Gurfateh! | (View-All)
Members who have read this thread : 0
| | There are no names to display. | |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | | | | Tools | Search | | | | | Display Modes | Linear Mode |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is On | | | | » Gurbani Jukebox | Listen to Gurbani while surfing SPN! | » Active Discussions | | | | | | | ਨਾਮਾ Today 06:37 AM 2 Replies, 53 Views | | | | | | | | | | | | | | | | | | | | | | | » Books You Should Read... | | | |