Variables?

Sprocket

Registered User.
Local time
Today, 03:59
Joined
Mar 15, 2002
Messages
70
How do I make a variable both public and static?

At least I think that is what I want to know - let me explain.

I have a form that I get to from a button on another form. The following code is on the button click event and works fine,

Private Sub Special_Needs_CmdBtn_Click()
On Error GoTo Err_Special_Needs_CmdBtn_Click

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "Special_Needs"
If Me.ARCHIVE = True Or Me.ARCHIVE2 = True Or Me.ARCHIVE3 = True Then
MsgBox "This student is archived"
Exit Sub
Else
stLinkCriteria = "[STUDENT_NUMBER]=" & "'" & _
Me![STUDENT_NUMBER] & "'"

DoCmd.Close

DoCmd.OpenForm stDocName, , , stLinkCriteria
End If

Exit_Special_Needs_CmdBtn_Click:
Exit Sub

Err_Special_Needs_CmdBtn_Click:
MsgBox Err.Description
Resume Exit_Special_Needs_CmdBtn_Click

End Sub

The second form closes on a timed event to stop people leaving it open when they move away from their PC. This bit also works fine.

The problem I'm having is setting up a quick (one click) way of getting back to the same record on form 2 without reopening it form one.

What I am trying to do is grab the value of the student number off form 2 as it closes - hold this value in a static state and then pass it to a button - using the same code as above but substituting the value of the variable for the second instance STUDENT_NUMBER - ie. Me![STUDENT_NUMBER] in the code sample above.

I have delared a public variable called KEEPHOLD which grabs the value of the student number as the form closes. I tested this by putting in a message box that used the code:

Msgbox " you are passing number" & KEEPHOLD

This popped up a message with the correct number so going out is OK but comming back in the value of KEEPHOLD is empty.


HOW - WHERE do I keep hold of the value to use again.


Many thanks...... Sprocket
 
Where did you declare the KEEPHOLD variable?

Class Module behind the Form?

Or in a Module in the Modules Tab?

If you put it in a Class Module behind the Form this variable will be lost once the form closes.

Put the variable in a New Module in the Modules tab. This will make it public for entire MDB. One note, if an error occurs in your code you will lose Public variables.
 
Thanks for the quick response Travis, but.......

I think that I must be doing something else wrong as I have put the declaration in a utility module under the modules tab.

Could you enlighten me on the static bit. Can a public variable also be static and if so how do you declare it as both.

My problem seems to be hanging on to the value and passing it back.
 
Sprocket said:
Could you enlighten me on the static bit. Can a public variable also be static and if so how do you declare it as both.

You don't need the Static declaration in a module as the public variable retains its value throughout the runtime of the application.*

The Static declaration keyword can only be used with a function or subroutine.

* If an error occurs that hasn't been sufficiently trapped then the value will be lost.
 
Whoops! Hadn't seen Travis's info above. :rolleyes:
 
It WORKS!!!

It was my first time using a global variable and thus I thought it must be the variable bit that I was getting wrong.

Once you guys convinced me that it was not placing the variable that was wrong it became much simpler to find that I had cut and pasted a line of code into the wrong place.

Many thanks all.
 

Users who are viewing this thread

Back
Top Bottom