Web Wiz - Green Windows Web Hosting

  New Posts New Posts RSS Feed - function inside a function.
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

function inside a function.

 Post Reply Post Reply Page  12>
Author
Badaboem View Drop Down
Senior Member
Senior Member


Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
Post Options Post Options   Thanks (0) Thanks(0)   Quote Badaboem Quote  Post ReplyReply Direct Link To This Post Topic: function inside a function.
    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.
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Gullanian View Drop Down
Senior Member
Senior Member
Avatar

Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
Post Options Post Options   Thanks (0) Thanks(0)   Quote Gullanian Quote  Post ReplyReply Direct Link To This Post Posted: 23 July 2004 at 4:11am
A function calling itself is called a recursive function also.
Back to Top
Badaboem View Drop Down
Senior Member
Senior Member


Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
Post Options Post Options   Thanks (0) Thanks(0)   Quote Badaboem Quote  Post ReplyReply Direct Link To This Post 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 .
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
Badaboem View Drop Down
Senior Member
Senior Member


Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
Post Options Post Options   Thanks (0) Thanks(0)   Quote Badaboem Quote  Post ReplyReply Direct Link To This Post 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. 
Back to Top
dpyers View Drop Down
Senior Member
Senior Member


Joined: 12 May 2003
Status: Offline
Points: 3937
Post Options Post Options   Thanks (0) Thanks(0)   Quote dpyers Quote  Post ReplyReply Direct Link To This Post 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.



Edited by dpyers

Lead me not into temptation... I know the short cut, follow me.
Back to Top
Badaboem View Drop Down
Senior Member
Senior Member


Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
Post Options Post Options   Thanks (0) Thanks(0)   Quote Badaboem Quote  Post ReplyReply Direct Link To This Post 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)



Back to Top
 Post Reply Post Reply Page  12>

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.08
Copyright ©2001-2026 Web Wiz Ltd.


Become a Fan on Facebook Follow us on X Connect with us on LinkedIn Web Wiz Blogs
About Web Wiz | Contact Web Wiz | Terms & Conditions | Cookies | Privacy Notice

Web Wiz is the trading name of Web Wiz Ltd. Company registration No. 05977755. Registered in England and Wales.
Registered office: Web Wiz Ltd, Unit 18, The Glenmore Centre, Fancy Road, Poole, Dorset, BH12 4FB, UK.

Prices exclude VAT at 20% unless otherwise stated. VAT No. GB988999105 - $, € prices shown as a guideline only.

Copyright ©2001-2026 Web Wiz Ltd. All rights reserved.