Link to home
Start Free TrialLog in
Avatar of Sandra Smith
Sandra SmithFlag for United States of America

asked on

Modul variable not recognized other procedure ACCESS 2010

I have a module level variable declaredas DIM strPath as String at the top of the module.  However, when I run the process to assign I a value, the other procedures do not pick it up, they return null for the strPath.  I tried using Private, but that did not work either.  What am I missing?

Option Compare Database
Option Explicit
Private strPath As String
Private Sub cmdImportNewProjects_Click()

If fntCheckFileList Then Exit Sub
Debug.Print "FILE: " & strPath
Call ImportNewProjects(strPath)
   
End Sub

Private Function fntCheckFileList() As Boolean
Dim strPath As String

If IsNull(Me.FileList) Then
    fntCheckFileList = True
    Exit Function
Else
    strPath = Me.FileList  'This is the full path and file name to be uploaded
    Debug.Print strPath
    fntCheckFileList = False
End If

End Function
Avatar of Norie
Norie

Where is the module?

Does it belong to a form?

Try inserting a standard module (Insert>Module) and putting the declaration there.
ASKER CERTIFIED SOLUTION
Avatar of mvidas
mvidas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can't use Private, of course. You'd need to change it to:

Dim strPath As String

or

Public strPath As String

What type of Module are you using? If this is a Standard Module, then that's all you'd need to do.

If it's a Form Module, then you cannot refer to it directly. Form Modules are Class Modules, and as such are not good choices to hold global variables. The best way to "expose" a value from a Form Module is to either (a) add a hidden textbox on the form, and refer to that directly or (b) add a Property to the class, and provide access to the variable in that manner (note this also works for standard Class modules as well).

(a) Just add a hidden textbox named something like "txtPath", and then refer to it as needed like this:

Forms("MyForm").txtPath

(b) Build a Property statement to give access to the value:

Property Let Path() As String
  Path = strPath
End Property

Now "call" that property from elsewehere in the program:

Dim sPath As String
sPath = Forms("MyForm").Path

Or from a Class module:

Dim cMyClass As cYourClass
cMyClass = New cYourClass

spath = cMyClass.Path
In addition to the above comments, it looks like you have strPath declared twice.

If you want to use it as a module level variable (visible to all procedures in a given module), declare it ONCE -- right under option explicit.

As LSM said, don't use the Private

And also, don't Dim it in the individual subs/functions in your module.

Just ONCE:

Dim strPath As String

Open in new window


at the very top of the module.
the new collection available in A2007 and newer versions "TempVars Collection" is a lot better to use than Public variables..

see this link

http://blogs.office.com/b/microsoft-access/archive/2010/09/27/power-tip-maximize-the-user-of-tempvars-in-access-2007-and-2010.aspx
I missed mvidas's comment at http:#a39732534 earlier.  My own comment duplicates his post.

(Sorry about that...)
Avatar of Sandra Smith

ASKER

That was it.