View Full Version : Call a Sub inside a function
11-26-2003, 12:27 AM
Is it possible to call a sub inside a function?
11-26-2003, 12:51 AM
Have you tried it? :) If you're just calling it it works, the only limitation is you can't contain one inside the other...
P.S. I tested it just as easily as you posted:
11-26-2003, 12:55 AM
Yeah but it didnt work! I tried below!
Function GenerateGUID(ByRef Connection, ByVal TableName, ByVal GUIDFieldName)
Dim GUID, IsOk, SQL, RS
IsOk = False
Counter = 0
'loop round generating new GUIDs until we've found one that hasn't been used yet!
While Not IsOk
Counter = Counter + 1
GUID = MakeGUID()
SQL = "SELECT " & GUIDFieldName & " FROM " & TableName & " WHERE " & GUIDFieldName & " = '" & GUID & "'"
'Response.Write SQL & "<BR />"
Set RS = ExecuteQuery(Connection, SQL, False, True)
If RS.EOF Then
'No matching GUID found, so it really *is* unique
IsOk = True
sqla = "insert into CouponCode (StartDate, ExpireDate, ToField, Referal, FromField, Catagory, CCode, CCDValue, Used, Ip) values ('" & date & "', '" & ((date)+365) & "', '" & ToField & "', '" & Referal & "', '" & FromField & "', 'All', '" & GUID & "', '" & CCDValue & "', '-1', '" & Ip & "');"
'Response.Write "GenerateGUID took " & Counter & " attempts to generate a new GUID.<BR>"
GenerateGUID = GUID
<%Sub Email() '''''''''''''''''''''''''''''''''''''''%>
<%End Sub '''''''''''''''''''''''''''''''''''''''''''%>
but it doesnt work?
11-26-2003, 01:02 AM
That's because you dimension the variable inside of the function, limiting its scope to "local", i.e. within the function. Once you exit the function (even with a call to a sub), the variant is destroyed.
If you want the variable to be global you need to dimension it outside of the function, or pass the variable to said sub. Does that make sense? :)
11-26-2003, 01:04 AM
Not really? I do realize that the sub does work I added a Response.Write "Blah"
and that came up! So its the GUID that is not coming up really odd cause if I put the
Response.Write GUID is where I put the Call Email() it works?
11-26-2003, 01:05 AM
It's a basic programming tenet; if you declare a variable within a function (or subroutine or class), then it only exists as long as that function is around. Once you go outside of the function, that variable (or variant, in this case) no longer exists!
So in order to fix the problem you're having, the easiest solution is to remove the Dim statement from within your function and put it outside. :)
11-26-2003, 01:07 AM
Ok now that I understand!
and it works great thankyou :)
11-26-2003, 01:44 AM
It might benefit you to do a google search on "variable scope" or somesuch; it applies to all programming languages as far as I know. I'm glad it's working now. :)
11-27-2003, 10:25 PM
Crash1hd, when helping you over MSN the other night you said that you had read through the "VBScript User Guide" in the Windows Scripting help file that I posted the link to the other day... it seems not... see the attached screenshot of the fourth topic of the first section...
11-28-2003, 12:54 AM
You know I read that too! When whammy had mentioned it I realized how dumb my question was lol, I guess I always look for the hard way when its right under my face!
11-28-2003, 05:03 AM
This is a pretty important concept when it comes to programming. Like I said, this applies to every language I know of (which isn't many, lol)... but programming is pretty much the same except for syntax from language to language, barring features unique to the langauge and syntax. :)
Powered by vBulletin® Version 4.2.2 Copyright © 2017 vBulletin Solutions, Inc. All rights reserved.