Print Page | Close Window

function inside a function.

Printed From: Web Wiz Forums
Category: General Discussion
Forum Name: Classic ASP Discussion
Forum Description: Discussion on Active Server Pages (Classic ASP).
URL: https://forums.webwiz.net/forum_posts.asp?TID=11264
Printed Date: 31 March 2026 at 3:07pm
Software Version: Web Wiz Forums 12.08 - https://www.webwizforums.com


Topic: function inside a function.
Posted By: Badaboem
Subject: function inside a function.
Date Posted: 22 July 2004 at 6:27pm
Is it impossible to have a function inside a function?

I'm working on a page that uses querystrings to call upon certain bits of code.

for example:

Case "Search"
Download_SCSearch()


calls forth everything between

Function Download_SCSearch()


and

end function
.

The issue is that the search fields i'm using between function ''scsearch" requires another function to build SQL query's for the options ''search all'' or ''any words''.

When I put this function inside the one that is called upon by the querystring i receive an error:

Microsoft VBScript compilation error '800a03ea'

Syntax error

Is there a workaround for this? I could create a separate search page, but would rather have it all located in one asp page.



Replies:
Posted By: dpyers
Date Posted: 22 July 2004 at 8:44pm

You can call a function from within another function - an example would be calling IsDate from within a function.

Where you can run into trouble is if the nested function calls the parent function -e.g.

Function1()
   ~Some Code~
   Function2()
        ~some Code~
        Function1()
   End Function
End Function
It can be done, but you have to be very careful with your code.

More than likely, it is a syntax problem with the function parameters - e.g. ByVal vs ByRef, or how they're dimmed. My rule of thumb is that if it returns True/False/A Specific Value. it's a function, else it's a sub routine.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Gullanian
Date Posted: 23 July 2004 at 4:11am
A function calling itself is called a recursive function also.


Posted By: Badaboem
Date Posted: 23 July 2004 at 5:41am
thanks for your help so far. I've been searching aspin for recursive functions and nested/sub functions but have not found an example of how to arrange functions in a proper manner. I guess I'm not sure what exactlty to look for.

I've simplified the structure that is currently in use and brought it down to the basics in order to see if something else than the function was causing the issue. This is not the case.

the structure is


Function Download_SCSearch()
'Function to build SQL query's for seach all or any words
search code goes here..

Function BuildSQL (strTable, sarySearchWord)

    'Initilaise variables
    Dim intSQLLoopCounter


    'Initilaise variables
    intSQLLoopCounter = 0

    'Search for the first search word
    BuildSQL = BuildSQL & "(" & strTable & " LIKE '%" & sarySearchWord(0) & "%'"

    'Loop to search for each search word entered by the user
    For intSQLLoopCounter = 1 To UBound(sarySearchWord)

        'If the search is for all words then place AND between the words to be serached
        If strSearchMode = "1" Then
            BuildSQL = BuildSQL & " AND "

        'Else the user has choosen to search for any words so place OR between the words to be serached
        Else
            BuildSQL = BuildSQL & " OR "
        End If

        'Place the search word in the query
        BuildSQL = BuildSQL & strTable & " LIKE '%" & sarySearchWord(intSQLLoopCounter) & "%'"
    Next

    'Close the end of the search words in the SQL query by closing the bracket
    BuildSQL = BuildSQL & ")"
End Function

End Function


very similar to what you described dpyers, so I guess that's the issue right there . But how can this structure be altered , but the BuildSQL still be nested within Function Download_SCSearch?

Sorry for being the noob I am. This is the first search i'm building along with functions .


Posted By: dpyers
Date Posted: 23 July 2004 at 5:44am
Another thing I thought of is either a mispelled/invalid object, property, or method, or you used a keyword for an id.

-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Badaboem
Date Posted: 23 July 2004 at 5:52am
both functions work when they are not inside of eachother, so it has to be the structure i guess. 


Posted By: dpyers
Date Posted: 23 July 2004 at 6:08am

Sorry, I thought you were calling the second function from within the first - not attempting to run it inline. Try this

Function BuildSQL (strTable, sarySearchWord)

    'Initilaise variables
    Dim intSQLLoopCounter


    'Initilaise variables
    intSQLLoopCounter = 0

    'Search for the first search word
    BuildSQL = BuildSQL & "(" & strTable & " LIKE '%" & sarySearchWord(0) & "%'"

    'Loop to search for each search word entered by the user
    For intSQLLoopCounter = 1 To UBound(sarySearchWord)

        'If the search is for all words then place AND between the words to be serached
        If strSearchMode = "1" Then
             BuildSQL = BuildSQL & " AND "

        'Else the user has choosen to search for any words so place OR between the words to be serached
        Else
             BuildSQL = BuildSQL & " OR "
        End If

        'Place the search word in the query
        BuildSQL = BuildSQL & strTable & " LIKE '%" & sarySearchWord(intSQLLoopCounter) & "%'"
    Next

    'Close the end of the search words in the SQL query by closing the bracket
    BuildSQL = BuildSQL & ")"
End Function

 


Function Download_SCSearch()
'Function to build SQL query's for seach all or any words
search code goes here..

BuildSQL (strTable, sarySearchWord)

End Function

In your mailline code, you'd just have the statemet

Download_SCSearch

EDIT: Appears that I led you astray in my initial post. - My aplogies.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Badaboem
Date Posted: 23 July 2004 at 6:18am
So the code below will alow the function BuildSQL to work within function Download_scsearch? (just so i understand correctly). In that case it doesn't matter where the actual functions are located i guess, even if it's outside the initial function that's being called upon ?

BuildSQL (strTable, sarySearchWord)





Posted By: Badaboem
Date Posted: 23 July 2004 at 6:45am
never mind..
It works as i suspected. Thanks for helping out dpyers.

The only thing i had to add was call in front of BuildSQL (strTable, sarySearchWord).


Posted By: dpyers
Date Posted: 23 July 2004 at 10:16am

The only difference between a function and a subroutine is that a function returns a value and a subroutine doesn't have to.

You really shouldn't have to invoke either with a call in asp 3.0. A function should return abe able to be referenced just like any asp command that returns a value.

As an example, I commonly use a function to validate email addresses that returns true/false. -
Function IsEmail(ByRef asString)

I invoke it in mainline code as... 
If IsEmail(strEmailAddress) Then ...

I have a subroutine that pulls html from a remote page into a string
Sub LoadThePage(strPageText, strInputURL)

I invoke it in code by...
LoadThePage strPageText, strInputURL


Neither the function nor the sub require the call statement.



-------------

Lead me not into temptation... I know the short cut, follow me.


Posted By: Badaboem
Date Posted: 23 July 2004 at 10:21am
Thanks for taking the time to explain.

Not having call in front of the sub ''function'' produced an error. Google lead me to the microsoft help pages explaining call had to be used in order to use ()

That's why I used it, but I will have a look at your explanations.


Posted By: dpyers
Date Posted: 23 July 2004 at 10:50am
The call is an older asp version hangover... but hey... if it ain't broke... don't fix it

-------------

Lead me not into temptation... I know the short cut, follow me.



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.08 - https://www.webwizforums.com
Copyright ©2001-2026 Web Wiz Ltd. - https://www.webwiz.net