FUNCTION Question

1. Senior Member
Join Date
Dec 1969
Posts
195

## FUNCTION Question

I would like to create a function where I can either load rs("h"), rs("w"), or rs("p") into it.<BR><BR>Something like:<BR><BR>&#060;%<BR>FUNCTION level()<BR> IF rs("h") = "1" THEN<BR> response.Write "Low"<BR> ELSEIF rs("h") = "2" THEN<BR> response.Write "Medium"<BR> ELSE <BR> response.Write "High"<BR> END IF<BR>END FUNCTION<BR>%&#062;<BR><BR>I know it probably has something to do with the empty "()", but I don&#039;t know how to go about using it.<BR><BR>Any help will be appreciated. Thanks!

2. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## One simple thing

what is the SCOPE of rs??<BR><BR><BR>If it is as the page level then it will work. But if rs is declared in another function or sub, it will not work.<BR><BR>If that is the case, pass th recordset to this function<BR><BR>FUNCTION level(rs)<BR><BR><BR>and you call it like Call level(YourRecordset)<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
195

## RE: FUNCTION Question

The example below is measuring Activity Levels at "home", "work", and "play".<BR><BR>There are 3 levels: "low"; "medium"; and "high".<BR><BR>rs("h") = Home Level, rs("w") = Work Level, and rs("p") = Play Level.<BR><BR>&#060;% <BR>FUNCTION level() <BR> IF rs("h") = "1" THEN <BR> response.Write "Low" <BR> ELSEIF rs("h") = "2" THEN <BR> response.Write "Medium" <BR> ELSE <BR> response.Write "High" <BR> END IF <BR>END FUNCTION <BR>%&#062; <BR><BR>---<BR><BR>&#060;HTML&#062;<BR>&#060;% Level() %&#062;<BR>&#060;/HTML&#062;<BR><BR>???

4. Senior Member
Join Date
Dec 1969
Posts
195

## So is it something like this... ???

&#060;%<BR>FUNCTION AL(rs)<BR><BR>IF rs_MyP("" & rs & "") = "1" THEN <BR>response.Write "Low"<BR>ELSEIF rs_MyP("" & rs & "") = "2" THEN<BR>response.Write "Medium"<BR>ELSE<BR>response.Write "High"<BR>END IF<BR><BR>END FUNCTION<BR>%&#062;<BR><BR>---<BR><BR>&#060;HTML&#062;...<BR><BR>&#060;% CALL AL(h) %&#062;<BR><BR>...&#060;/HTML&#062;<BR>

5. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## Nope

you stick with the same code but IF the scope of your recordset it not for the entire page, pass the ENTIRE recordset to the function<BR><BR>OR<BR><BR>if the scope extends to the function also, then you do not need to pass anything to the function<BR><BR><BR>your old code should work<BR><BR>

6. Senior Member
Join Date
Dec 1969
Location
Los Angeles, CA
Posts
21,192

## What you MAY have to show us

is where this recordset object is created<BR><BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## Akhilesh is confused...

...because he didn&#039;t understand how simple this problem really is. He misunderstood your dilemma, I think.<BR><BR>You&#039;ve got two choices on writing this: <BR><BR>(1) Pass in the *name* of the field that you want to use, and then get the value for that named field from the recordset inside the subroutine.<BR>[ Incidentally, since you aren&#039;t returning any value from the subroutine, it should be a SUB, not a function. ]<BR><BR> &#060;%<BR> SUB Level( fieldName )<BR> Dim value<BR> &#039; get value from fieldname, here:<BR> value = RS(fieldname)<BR> IF value = "1" THEN <BR> response.Write "Low" <BR> ELSEIF value) = "2" THEN <BR> response.Write "Medium" <BR> ELSE <BR> response.Write "High" <BR> END IF <BR> END Sub<BR> %&#062;<BR><BR>You&#039;d then call the SUB using something like<BR> &#060;% Level "h" %&#062;<BR>or<BR> &#060;% Level "w" %&#062;<BR><BR>*********************** <BR><BR>(2) Pass in the *value* of the field you want to use.<BR><BR> &#060;%<BR> SUB Level( value )<BR> IF value = "1" THEN <BR> response.Write "Low" <BR> ELSEIF value) = "2" THEN <BR> response.Write "Medium" <BR> ELSE <BR> response.Write "High" <BR> END IF <BR> END Sub<BR> %&#062;<BR><BR>You&#039;d then call the SUB using something like<BR> &#060;% Level RS("h") %&#062;<BR>or<BR> &#060;% Level RS("w") %&#062;<BR><BR>*********************************** ******<BR><BR>Of the two choices, I prefer the second, because (among other things) it prevents any problems with variable scoping (the stuff Akhilesh was worrying about but which probably doesn&#039;t apply if you don&#039;t know what he was talking about...but just in case...).<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•