| Author |
Topic Search Topic Options
|
Badaboem
Senior Member
Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
|
Post Options
Thanks(0)
Quote Reply
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
.
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.
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
Gullanian
Senior Member
Joined: 04 January 2002
Location: England
Status: Offline
Points: 4373
|
Post Options
Thanks(0)
Quote Reply
Posted: 23 July 2004 at 4:11am |
|
A function calling itself is called a recursive function also.
|
 |
Badaboem
Senior Member
Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
|
Post Options
Thanks(0)
Quote Reply
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  .
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
Badaboem
Senior Member
Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
|
Post Options
Thanks(0)
Quote Reply
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.
|
 |
dpyers
Senior Member
Joined: 12 May 2003
Status: Offline
Points: 3937
|
Post Options
Thanks(0)
Quote Reply
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
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.
|
 |
Badaboem
Senior Member
Joined: 12 April 2002
Location: Netherlands
Status: Offline
Points: 600
|
Post Options
Thanks(0)
Quote Reply
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) |
|
 |