Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Public variable not keeping value - why?

Status
Not open for further replies.

MichaelRed

Programmer
Dec 22, 1999
8,410
US
I have read many of the threads reefering to this. I undersdand that they generally refer to the scope of the variable. Brief statement of this situation:

1.[tab]I have searched the APP for type declarations there is only ONE. It is in a code (not a form) module.

2.[tab]I have traced it through the code where it is set (this is a form module) - however the form is (and remains) open throughout the APP, and I can see the value and it is the expected value at the call to the next procedure.

3.[tab]At the FIRST executable statement of the called procedure in a code module, the value has been destroyed (was 3, now 0).

4.[tab]Although several of the threads I read suggested alternatives to the use of the Public type statement, both the literature and many of the same threads noted that the process, as described above shoud work.

5.[tab]Somewhat related to this, I noted that an additional property of typed variables "Shared" was useful / required. I attempted to include this, however MS Access (ver 2003) deems it 'bothersome' and removes it IMMEDIATLY after moving focus to ANY where else (another module / procedure / line / form ...).


So does anyone have any suggestions?

MichaelRed


 


hi,

You have not stated exactly WHERE the variable is declared and HOW it is declared, except to say that is it in a module.

You state that "At the FIRST executable statement of the called procedure in a code module, the value has been destroyed (was 3, now 0)." If your variable was 3, then how did it get to be 3?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Declared PUBLIC in a code module.

Value is set in a the Form's module in a sub procedure.

Thanks for your interest.



MichaelRed


 
Without seeing this hard to tell. Can you show the lines that

1) Dimension the variable
such as: Public SomeVariable as Integer
I assume that this is at the top of a standard module
2) Where the variable is used
someVariable = 3

verify that you have "Option Explicit" on all modules


Here is my first guess. You declare a public variable as well as a local variable with the same name. So you are setting a local variable to 3 but thinking it is the public variable.

public myVariable as integer

public subOne
'set public variable
myVariable = 3
end sub

public subTwo
dim myVariable as integer
'returns 0
msgbox myVariable
myVariable = 4
end sub

Public subThre
'Returns 3
msgbox myVariable
end sub


if you executed subone,subtwo,subthre
you would set the public variable to 3
sub two would return 0 the value of the local variable with the same name
subThree would return 3 the value of the public variable.

BTW share is only a VB.net construct AFAIK
 
Option Compare Database
Option Explicit

Public dbs As DAO.Database

Public rstSolHdr As DAO.Recordset
Public rstSolCoeffs As DAO.Recordset
Public rstSolReCalc As DAO.Recordset
Public rstSolPoints As DAO.Recordset
Public rstVarFit As DAO.Recordset

Public qdfDelProbSol As DAO.QueryDef

Public qdfParams As DAO.QueryDef
Public qdfDataIn As DAO.QueryDef
Public qdfNpointss As DAO.QueryDef
Public rstProb As DAO.Recordset

Public NDeg As Integer
Public Npoint As Integer <------- Var of interest


Public Function basGetProbStmt()

Set qdfParams = dbs.QueryDefs("qryLinearParams")
qdfParams.Parameters("intTitleId") = frm.txtProbId
Set rstProb = qdfParams.OpenRecordset

Dim Idx As Integer

'Params Use TitleId to get the records for the problem
With rstProb
intI = !TitleId
NDeg = !NDeg
Cy = !Cy
Cx = !Cx
Kw = !Kw
KStop = !KStop
Np = !Npoint 'do away w/ this. just use: NProc_(xxx)
Npoint = !Npoint <---- VAr Set to Value


I have done "find: on the variable with the scope set to the app. The only TYPE declaration is shown above.

ALL modules have option explicit set

I am / have been attempting variations on the theme, and have moved ALL code out of the form moduule, except to call the same functionallity in a code only module. This DOES seem to fix / avoid the issue - but seems to defy the literature provided (by MS and others).

Again, thanks for your attention.



MichaelRed


 



Out of curiosity, what happens to the value in NDeg?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Sorry, I didn't get to check. "NPoint" is creitcal to the operation (used as an throughout the app), so nothing else happens when it fails.


MichaelRed


 
i have tried to duplicate this with no success. Setting the value in a forms module, declaring it in a code module even after setting the value and closing the form it still held its value as expected. Are you sure its not set somewhere else ... can we see the forms code?

HTH << MaZeWorX >> "I have not failed I have only found ten thousand ways that don't work
 
Thanks for looking into it. I have posted all of the relevant(?) parts of the code above. As noted, in continuing to proceede with this app, I have continued and resolved (really avoided) the issue by simply moving the form procedures to a general / code module and calling one of the new functions from the form module (leaving it essientially 'empty'. I doing so, I have (natureally?) lost / destroyed the actual code.

As noted above, I did do a complete search (e.f. Find) for the variable. There was only one type declaratation (as Public) and only one assignment of a vlue to the var. All MODULES included option explicit.

Since this is a conversion for another party, I m not at liberty to post the entire listing of the code.

Again, thanks for your interest.



MichaelRed


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top