Really DUMB question about variables (1 Viewer)

buratti

Registered User.
Local time
Today, 12:31
Joined
Jul 8, 2009
Messages
234
Ok guys I'm sorry and really embarrassed for asking this, but its so simple and I'm surprised I never ran into this before.

When using variables in a forms vba module (not sure the actual name of that, but NOT a separate module, but the one attached to a form), If I declare a variable in the forms onOpen event and set a value to it, would that variable and value be accessible in lets say that same forms onClick event of a button?

I obviously tried it before posting and apparently I cant, or am possibly just declaring it wrong:

Code:
Private Sub Form_Open(Cancel As Integer)
 
Dim myVariable As string
myVariable = "Step 1"
 
End Sub
 
Private Sub cmdnext_Click()
 
Select Case myVariable
Case "Step 1"
        Me.lblStep1.Visible = False
        Me.lblStep2.Visible = True
        myVariable = "step 2"
Case "step 2"
        Dim strSearchPath As String
        Dim strFileName As String
        ...

How would I go about doing this?
 

mrrcomp

Registered User.
Local time
Today, 19:31
Joined
May 3, 2010
Messages
16
Hi
The scope of the variable is only for the sub that its in. You need to declare it outside the scope of any of the code behind the form subs.

Hope that helps
 

buratti

Registered User.
Local time
Today, 12:31
Joined
Jul 8, 2009
Messages
234
So basically in other words, what you are saying is that I have to declare the variable OUTSIDE of any Private sub...End Sub procedures???

And by doing so that variable will now be availabe INSIDE any Private sub...End Sub procedures?

Am I correct on understand your response?
 

boblarson

Smeghead
Local time
Today, 09:31
Joined
Jan 12, 2001
Messages
32,059
Variables have SCOPE - where they are accessible depending on where, and how, you declare them.

In a Form Module:

If declared as Private in the form's GENERAL DECLARATIONS SECTION (the part at the top just after the

Option Compare Database
Option Explicit


Then the scope is that it can be referenced in any procedure within that same module.

If declared as Public, then it can be referenced outside of that module but only with the identifying form information too.

If you declare in a STANDARD module (not form or report) then you can declare as PRIVATE which would mean it is only available to any procedure within that module, or PUBLIC which then is available to anything, anywhere.

Hope that helps.
 

buratti

Registered User.
Local time
Today, 12:31
Joined
Jul 8, 2009
Messages
234
Thanks that cleared up everything. I cant believe I was having trouble with this. I am no where near the level of knowledge that most of you here have, but that is something I should of known. LOL
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 11:31
Joined
Feb 28, 2001
Messages
27,261
When declaring in a class module, you cannot declare something PUBLIC. When declaring in a general module, you can do so.

Another difference: If the form closes and opens, private variables are destroyed and recreated by that action. Therefore, values in a class module (i.e. attached to a report or form) do not survive closing the form/report or taking an unhandled exception in the module such that the overall Access trap handler has to get involved. When Access takes the trap, variables in class modules go crazy.

But then again, if something happens such that your general module closes, you are again going to lose context. It is very tricky to navigate this particular mine field.
 

ChrisO

Registered User.
Local time
Tomorrow, 02:31
Joined
Apr 30, 2003
Messages
3,202
Bob is correct in post #4.
 

Users who are viewing this thread

Top Bottom