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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get a variable that is inside a function...

Status
Not open for further replies.

Gti

Programmer
Jul 23, 2001
99
PT
I need to get a variable that is inside Module and inside a function.
(eg:

Public Sub ScanFonteDir(Origdir, Destdir)
scandir = Dir(Origdir & "\*.cls")
Do While scandir <> &quot;&quot;
MsgBox scandir
scandir = Dir
Loop
Exit Sub
End Sub
)

i need to get that scandir to the main program instead of using a msgbox...
Any help?
tkx ;-)
 
Change your Sub into a function and return the variable . . . for example . . .

Code:
Public Function ScanFonteDir(Origdir, Destdir) As String
    scandir = Dir(Origdir & &quot;\*.cls&quot;)
    Do While scandir <> &quot;&quot;
      MsgBox scandir
      scandir = Dir
    Loop

    ScanFonteDir = scandir
End Sub


IF you want to return multiple strings, then you may want to consider passing them in a collection or array.
- Jeff Marler B-)
 
Define ScanDir in the module level declarations and use a function to return it as needed. You can't get a variable that is DIMed inside a function since it disappears when the function is exited.
 
Change the sub in the module to a function:
The only problem is that you are looping through the dir, so you need to return all of files found

Public Function ScanFonteDir(Origdir, Destdir) as String
dim sDirecotry as string
scandir = Dir(Origdir & &quot;\*.cls&quot;)
Do While scandir <> &quot;&quot;
scandir = Dir
sDirectory = sDirectory & scandir & &quot;,&quot;
Loop
ScanFonteDir = sdirectory
End Sub
This will return all paths found and separate them with a &quot;,&quot;
 
Try making the Sub Routine a Function and return the scandir as a string...something like the following:

Public Function ScanFonteDir(Origdir, Destdir)As String
scandir = Dir(Origdir & &quot;\*.cls&quot;)
Do While scandir <> &quot;&quot;
'MsgBox scandir
scandir = Dir
ScanFonteDir=scandir
Loop
Exit Sub
End Sub

And when you call the Function from your Main program, set it equal to a variable:
Dim strDirectory As String
strDirectory=ScanFonteDir(OrigDir,DestDir)

Hope this helps....

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top