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

how to call a sub from another sub

Status
Not open for further replies.

sm2605

Programmer
Nov 19, 2003
44
0
0
US
Hi, i have a sub form_load() and a sub process_click() but i dont know how to connect them. meaning the variables i used in form_load() i want to be able to use in process_click(). both are public subs and here is the code from both: your help is greatly appreciated...thanks

Public Sub Form_Load()
flag = dataDirectory & "\temp.txt"
Ix2 = 4 'Initial index
KSlash2 = InStr(1, flag, "\", 1) 'Search For first "\"
For Cntflag = 1 To Len(flag) 'Run until discover
'other directories
KSlash2 = InStr((KSlash2 + 1), flag, "\", 1)
If KSlash2 = 0 Then Exit For 'Last slash
dir12 = Left(flag, (KSlash2 - 1))
cdir12 = Mid(dir12, Ix)
Ix2 = Ix2 + Len(cdir12) + 1
hh2 = Dir(dir12)
If Dir(flag, vbDirectory) <> &quot;&quot; Then
'MkDir (dir12 & &quot;\temp&quot;)
MsgBox (&quot;flag exists&quot;)
'backup files from Ptemp to dataDirectory
CopyFile Pfolder, dataDirectory, p
MsgBox &quot;Number of files copied = &quot; & Str$(p)
Else
'flag doesnt exist, copy data directory to Ptemp
MsgBox (&quot;flag doesn't exists&quot;)

CopyFile dataDirectory, Pfolder, p
MsgBox &quot;Number of files copied = &quot; & Str$(p)
End If
Next Cntflag
end sub

Public Sub Process_Click()
MsgBox (&quot;You chose &quot; & TimePeriods.List(TimePeriods.ListIndex))

Open flag For Output As #1
Close #1

End Sub


Ok, the open flag for output does not work because it seems that the sub doesnt know what flag is.

thank you again

 
sm2605,

First of all, make sure the line

Option Explicit

is the first line in every module, form, and class.

Then, when you declare all your variables, you might want to declare &quot;flag&quot; variable as private in the module you use it ( outside of any sub). For example:

Option Explicit
Private flag As String
....

There are other ways to do this in VB.

Vladk




 
sm2056 ..
You need to set up the Process_Click event to accept input parameters. Something like this ...

I am not sure of the type that needs to be declared for a sesquential file.

Process_Click(flag as String)
Do what you need here
....
....
...
End Sub
And from the calling procedure, it would be
Process_Click flag

If all you want to do is use the same variable in more than one Sub or Function, the easiest way to dothat is to just declare it in the General Declarations section of the form.
Also, using Form_Load to do all you are, is really not the proper place to do it. Better to break that routine into a few different logical operations, then call them from form_load.
HTH
Michael
 
thanks so much, really appreciate it.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top