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!

can you call sub within a sub? 1

Status
Not open for further replies.

thompom

Technical User
Dec 4, 2006
395
GB
hi,

am using subs a lot to help reuse my code,
problem is i now want to run a sub within a page that itself is a sub

is this possible - have done searches but cant find the answer

im getting syntax errors on the first line of the second sub so i suppose this answers my question, but maybe there is a way round it

thanks MG
 
Yes.

Try something like: [tt]
Dim MyValue

MyValue = 1
AddNum 10
Response.Write MyValue

Sub AddNum(num)
Dim i
If num < 1 then
exit sub
end if

For i = 1 to num
AddOne
Next
End Sub

Sub AddOne()
MyValue = MyValue + 1
End Sub
[/tt]
 
You can do that, but don't go too deep within subs without exiting, otherwise you will hit a stack error.

[monkey][snake] <.
 
thanks for the definitive answer scheco - heres a *

my problem was the include page was within the first function
i got an error at the top of the second funtion
so i moved the include to the main page that calls the first function and now it works - makes sense, probably not!

 
Yes this makes sense because although you CAN call one sub or function from inside another sub or function, you CANNOT declare one inside another.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top