Reset Public Variables 
Author Message
 Reset Public Variables

Has anyone noticed this problem with Access 97?

My Public Variable get cleared when an error occurs!

Try this:

Create a module, put in a Public variable into the declaration like:

Public strDog as String

Now create a test procedure

Sub Test1()
    strDog = "Shepard"
    msgbox strDog
    error 1000
End Sub

Sub Test2()
    msgbox strDog
End Sub

Execute Test1, then Test2...strDog get's reset!!!  What kind of garbage is
this? Argh!!!

Anyone know what's up with this?  How can I trust a global/public variable
if it can be reset by an error? I've trapped the error and get the same
results!!

Help!  Do I need a patch of some sort or am I doing this wrong?

Thanks,

Anthony, MCSE



Mon, 06 Jan 2003 03:00:00 GMT  
 Reset Public Variables


Quote:
>My Public Variable get cleared when an error occurs!
<snip>
>Anyone know what's up with this?  How can I trust a global/public variable
>if it can be reset by an error? I've trapped the error and get the same
>results!!

This is known behavior and by design. It happens on *unhandled* errors.
This is probably the main reason why it's not recommended to use public
variables. One workaround is to use public properties in standard module,
which can be made self-fixing, instead of public vars:

Private dbC As DAO.Database

Public Property Get dbCur() As DAO.Database

If dbC Is Nothing Then
  'something reset the var or it hasn't been set yet
  Set dbC = CurrentDb
End If

Set dbCur = dbC

End Property

Another approach is to use Properties collection of the database to store
globally available values. Those will persist even between sessions.

--
(remove a 9 to reply by email)



Mon, 06 Jan 2003 03:00:00 GMT  
 Reset Public Variables
Anthony,
        I would say that the resetting of the variables after an error is
normal. You need to set up error handling to ensure you don't get thrown
out of the program.
Ian Pratt
Quote:

> Has anyone noticed this problem with Access 97?

> My Public Variable get cleared when an error occurs!

> Try this:

> Create a module, put in a Public variable into the declaration like:

> Public strDog as String

> Now create a test procedure

> Sub Test1()
>     strDog = "Shepard"
>     msgbox strDog
>     error 1000
> End Sub

> Sub Test2()
>     msgbox strDog
> End Sub

> Execute Test1, then Test2...strDog get's reset!!!  What kind of garbage is
> this? Argh!!!

> Anyone know what's up with this?  How can I trust a global/public variable
> if it can be reset by an error? I've trapped the error and get the same
> results!!

> Help!  Do I need a patch of some sort or am I doing this wrong?

> Thanks,

> Anthony, MCSE




Tue, 07 Jan 2003 03:00:00 GMT  
 Reset Public Variables
Dimitri,
        I like your solution, I must give it a try:-)
Ian Pratt
Quote:



> >My Public Variable get cleared when an error occurs!
> <snip>
> >Anyone know what's up with this?  How can I trust a global/public variable
> >if it can be reset by an error? I've trapped the error and get the same
> >results!!

> This is known behavior and by design. It happens on *unhandled* errors.
> This is probably the main reason why it's not recommended to use public
> variables. One workaround is to use public properties in standard module,
> which can be made self-fixing, instead of public vars:

> Private dbC As DAO.Database

> Public Property Get dbCur() As DAO.Database

> If dbC Is Nothing Then
>   'something reset the var or it hasn't been set yet
>   Set dbC = CurrentDb
> End If

> Set dbCur = dbC

> End Property

> Another approach is to use Properties collection of the database to store
> globally available values. Those will persist even between sessions.

> --
> (remove a 9 to reply by email)



Tue, 07 Jan 2003 03:00:00 GMT  
 Reset Public Variables



Quote:
>Dimitri,
>     I like your solution, I must give it a try:-)

Well, it's not mine actually, I lifted it from Michka, but go ahead anyway
<g>

--
(remove a 9 to reply by email)



Tue, 07 Jan 2003 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. public variables not public

2. Public Type vs. Public variable() as Double

3. Public variable not acting public (VB 5.0)

4. Use a public variable or use Private variable with Get and Set

5. Reset value of variables in a Select Case loop

6. Resetting variables

7. Application.Quit statement resets Variables?

8. Access RESETS my global variables

9. Global variable resets in Access 2.0

10. reset variables

11. Resetting variables

12. Is there an easy way to reset variables

 

 
Powered by phpBB® Forum Software