3

I have created a classic asp page with a function like the following:

<%@ Language=VBScript %>
<% Option Explicit %>

<%
      <!--#INCLUDE file='ADOVBS.inc'-->
       Response.Buffer = True

%>
<%
      Function Lettergrade(lg)

             Dim lgs
             if lg>50 then
            set    lgs="P"
                else
            set    lgs="F"
             end if
           set Lettergrade= lgs
       End Function
  %>
<%
set strlettergrade=Lettergade(strgrade)
... other code goes here
%>

I'm trying to call the function in an expression but when it gives me the following error:

Microsoft VBScript runtime error '800a01f4'

Variable is undefined: 'Lettergade'

Does anyone have an idea?

Community
  • 1
  • 1
Elmer Tillett
  • 51
  • 1
  • 1
  • 4

5 Answers5

5
  1. Option Explicit means you must DIM variables before they can be used.
  2. <!--#INCLUDE file='ADOVBS.inc'--> is not vbscript so do not include them between the <% %> script delimiter tags.
  3. You need to DIM strgrade
  4. you need to DIM strlettergrade before you use it
  5. Fix the typo of Lettergade(strgrade)

<%@ Language=VBScript %> 
<% Option Explicit %>  
<% Response.Buffer = True %>

<!--#INCLUDE file='ADOVBS.inc'-->

<%
Dim strlettergrade
Dim strgrade

%>

<%
Function Lettergrade(lg)

if lg > 50 then
  Lettergrade ="P" 
else 
  Lettergrade ="F" 

End Function
%>

<%
strgrade = 75
strlettergrade=Lettergrade(strgrade)
... other code goes here 
%> 
2

Try getting rid of the Set inside the function call and just assign Lettergrade = lgs.

Guido Gautier
  • 1,237
  • 9
  • 13
Ryan Fiorini
  • 800
  • 5
  • 9
1

Variable is undefined: 'Lettergade'

Function appears to be called Lettergrade not Lettergade - might just be a typo issue

Try changing:

strlettergrade=Lettergade(strgrade)

to

strlettergrade=Lettergrade(strgrade)

Chris Young
  • 1,749
  • 12
  • 17
1

There are several problems with this script, some already pointed out by Chris and Ryan.

  1. You only need Set when you assign an object to a variable. Strings are not objects in VBScript. Get rid of all the Set's in your script.

  2. The obvious typo that Chris mentioned. Change Lettergade to Lettergrade.

  3. You don't seem to declare the strlettergrade or strGrade variable. With Option Explicit turned on (which is a good practice), it is mandatory to declare all variables. Use Dim for this (like the one inside your function).

  4. You seem to use a string as a parameter for your Lettergrade function. Since the variable name is strgrade. Within the function you are comparing with an integer. This could cause problems. Convert your string to an integer first using the cInt function.

  5. Includes need to go outside the <% %> tags. Move <!--#INCLUDE file='ADOVBS.inc'--> to the line below Option Explicit

Follow these steps, or copy paste the script from @Cape Cod Gunny. After this your script should work as expected ;). Except for point 4, you still need to correct that.

Guido Gautier
  • 1,237
  • 9
  • 13
0

You misspelled Lettergrade when calling the function. Variable is undefined: 'Lettergade' Also I would take SLaks advice as well. Set should not be used on strings.

deusxmach1na
  • 368
  • 6
  • 17