<HTML>
<HEAD>
</HEAD>
<BODY>
<SCRIPT LANGUAGE="VBScript">
<!--
VBScript code goes here
-->
</SCRIPT>
</BODY>
</HTML>
Dim Name
Name = "Tim Koets"
If IsNumeric(Age) = False Then
MsgBox "The size must be a number. Please enter it again."
End If
<HTML>
<TITLE>Chapter 5 Quiz #2</TITLE>
<H1><A HREF="http://w3.softlookup.com"><IMG ALIGN=BOTTOM SRC="../shared/jpg/_
samsnet.jpg" BORDER=2></A>
The Ticket Stand</H1>
<BODY>
<HR>
<PRE>Name <INPUT NAME="txtName"></PRE>
<PRE>Number of Tickets <INPUT NAME="txtTickets"></PRE>
<PRE>Row Number (1 for front row) <INPUT NAME="txtRow"></PRE>
<CENTER><INPUT TYPE=BUTTON VALUE=" Get Cost " NAME="cmdCost"></CENTER>
<HR>
<center>
from <em>Teach Yourself Visual Basic Script in 21 Chapters</em> by
<A HREF="../shared/keith.asp">Keith Brophy</A> and
<A HREF="../shared/tim.asp">Tim Koets</A><br>
Return to <a href="..\default.asp">Content Overview</A><br>
1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdCost_OnClick()
Dim Cost
Dim Ticket_Count
Ticket_Count = txtTickets.Value
Cost = 20.00 ' Base price
If txtRow.Value = 1 Then
Cost = Cost + 4.00 ' Add $4 for front row
End If
Cost = Cost * Ticket_Count ' Get cost for all tickets
Cost = Cost + Cost * 0.03 ' Add 3% commission
Cost = Cost + Cost * 0.08 ' Add 8% sales tax
MsgBox "Your total cost is " & Cost
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
If Result = True Then
MsgBox "The result is true."
End If
For Next-This control structure is used to repeat a block of code nestled between the For and Next statements based on some condition.
A simple example of its use is
For x = 1 to 100
age(x) = 0
Next
Do Loop While-This control structure is used to repeat a block of code while the condition is true.
An example of its use is
Do
age(x) = 0
x = x + 1
Loop While x < 100
YearBorn = InputBox("What year were you born in? ")
If YearBorn < 1890 Or YearBorn > 1990 Then
MsgBox "The year you have entered is invalid."
End If
Proceed = False
Do
YearBorn = InputBox("What year were you born in? ")
If YearBorn < 1890 Or YearBorn > 1990 Then
MsgBox "The year you have entered is invalid."
Else
Proceed = True
End If
Loop Until Proceed = True
Sub cmdTest_OnClick()
Dim Base_Cost
Dim Total_Cost
Dim Tax
Tax = 5 ' Michigan sales tax (5%)
Total_Cost = CalculateCost(Base_Cost, Tax)
txtResult.Value = "The total cost is $" & Total_Cost
End Sub
Function CalculateCost(ByVal Cost, ByVal Tax)
Tax = Tax / 100
Cost = Cost + Tax * Cost
CalculateCost = Cost
End Function
The solution to the problem is to pass the two parameters in by value so that the function has its own copy of the variables. That way, it can modify the variables as it needs to. The new code listing shows the necessary changes.
<HTML>
<HEAD>
<TITLE>Chapter 8 Quiz #1</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://w3.softlookup.com"><IMG ALIGN=MIDDLE
SRC="..
/shared/jpg/samsnet.jpg" BORDER=2
HSPACE=20></A>
<EM>Chapter 8 Quiz Answer</EM></h1>
<HR>
<H2>Product Information</H2>
<FORM NAME="MyForm">
<P>Please enter all of the products you would like information about
in the space below. Then, click on the button and you will be
presented with a series of Web pages detailing your selections.
<HR>
<PRE><TEXTAREA NAME="txaProducts" COLS="60" ROWS="10"></TEXTAREA></PRE>
<P><INPUT TYPE="BUTTON" NAME="cmdGetInfo"
VALUE=
"Get Product Information">
</FORM>
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Chapters</em><br>
by <A HREF="../shared/info/keith.asp">Keith Brophy</A> and
<A HREF="../shared/info/tim.asp">Tim Koets</A><br>
<br>
Return to <A href=Back08.asp>content overview</A><br>
1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!-- Option Explicit
Sub cmdGetInfo_OnClick()
MsgBox "The products you're requesting information onÂ
will be sent to you momentarily."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
All you need to do next is pass the products to the server and get the information back to the user. You'll learn more about how to do that on Chapter 19.
Figure C.1 : A Web page that converts feet, inches, or yards to meters.
The following segment shows the source code for the Web page:
<HTML>
<HEAD>
<TITLE>Chapter 9 Quiz</TITLE>
</HEAD>
<BODY>
<H1>
<A HREF="http://w3.softlookup.com"><IMG ALIGN=MIDDLE
SRC="../shared/jpg/samsnet.jpg" BORDER=2 HSPACE=20></A>
<EM>Converting Feet, Inches or Yards to Meters</EM></H1>
<HR>
<INPUT TYPE="TEXT" NAME="txtNumber" SIZE="10">
<INPUT TYPE="RADIO" NAME="optUnits" chECKED
OnClick="SetUnits('Feet')"> Feet
<INPUT TYPE="RADIO" NAME="optUnits"
OnClick="SetUnits('Inches')"> Inches
<INPUT TYPE="RADIO" NAME="optUnits"
OnClick="SetUnits('Yards')"> Yards
<P>
<INPUT TYPE="BUTTON" NAME="cmdResults" VALUE="Calculate">
<INPUT NAME="txtResult" SIZE="50">
<HR>
<center>
From <em>Teach Yourself VBScript in 21 Chapters</em><br>
by <A HREF="../shared/info/keith.asp">Keith Brophy</A> and
<A HREF="../shared/info/tim.asp">Tim Koets</A><br>
<br>
Return to <A href=Back09.asp>content overview</A><br>
1996 by SamsNet<br>
</center>
<SCRIPT LANGUAGE="VBScript">
<!--
Dim Units
Units = "Feet"
Sub SetUnits(NewUnits)
Units = NewUnits
End Sub
Sub cmdResults_OnClick()
Dim Number
Dim Result
Number = txtNumber.Value
If Units = "Feet" Then
Result = Number * 0.3048
ElseIf Units = "Inches" Then
Result = Number * 0.00254
ElseIf Units = "Yards" Then
Result = Number * 0.9144
End If
txtResult.Value = "There are " & Result & " meters in " &_
Number & " " & Units & "."
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
The solution uses a script-level variable called Units that tracks the user-selected units. Whenever the user clicks a radio button, the subroutine SetUnits proceeds to change the script-level Units variable. Then, when the user clicks Convert, the script uses the appropriate multiplier in a conditional expression to obtain the result. This example shows how to effectively use intrinsic HTML controls, procedures, conditional structures, and script-level variables to accomplish the task.
<SCRIPT LANGUAGE="VBSCRIPT">
<!--
sub lblTester_Click
lblTester.Caption = "Kenny Jasper"
end sub
-->
</SCRIPT>
If (lblTester.Angle >= 20)and (lblTester.Angle <= 40) then
lblTester.Caption = "Brooke"
else
lblTester.Angle = lblTester.Angle + 10
end if
Sub MyTimer_Time
' Check to see what the current interval is
if MyTimer.Interval = 2000 then
MyTimer.Interval = 1000
elseif MyTimer.Interval = 1000 then
MyTimer.Interval = 500
else
MyTimer.Enabled = 0
end if
end sub
MyItem.date = "7/6/62"
By setting the suppression date back in time, you ensure that the code will consider that the suppression date has already been reached, and the graphic will not display.
<OBJECT
id="jvaGifts"
CLASSid="java:BirthChapter.Logs"
CODETYPE="application/java-vm"
CODEBASE="http://w3.softlookup.com/javastuff/"
HEIGHT=100
WIDTH=100
>
<PARAM NAME="MaxSpend" VALUE="200">
A java applet is used on this page but your browser does not support that.
</OBJECT>
vAge=txtage.value
If IsNumeric(vAge) then
if vAge < intGENERATION_X_AGE Then
msgbox "Dude, prepare to surf!"
Else
msgbox "Welcome - Prepare to enter our Web page"
End If
Elseif vAge = "unknown" Then
msgbox "you must supply an age to proceed"
Else
msgbox "please supply a valid age"
End If
<script Language="VBScript">
sub lblFeedback_click
msgbox "Feedback label has been clicked"
end sub
Sub cmdCalculate_OnClick
msgbox "Button has been clicked"
End sub
</script>
<HTML>
<HEAD>
<TITLE>Brophy & Koets' Teach Yourself VBScript - Quiz 1 Solution</TITLE>
</HEAD>
<BODY>
<SCRIPT LANGUAGE=VBS>
<!-
dim rc ' Stores return code value
rc = msgbox("Do you want extra hints to be displayed on this page?", _
vbYesNo + vbQuestion,"Setting up the page")
if rc = vbYes then
' Show Big Spender Page
Spender = vbTrue
document.write "<H2>Extra Hints</H2>"
' ... more document.write statements to display hints could go here
end if
-->
</SCRIPT>
<! Regular HTML statements that are always carried out could follow... -->
<H3>Welcome to the Question Page</H3>
<p>Q1. What eats lots of oranges and has 80,000 legs?
<p>Q2. Why is the grass green?
<BR>
<BR>
<p>A1. The field in the Boston Marathon
<p>A2. If it was white you couldn't tell if a polar bear was in your lawn!
</BODY>
</HTML>
<HTML>
<HEAD>
<TITLE>Brophy & Koets' Teach Yourself VBScript - Quiz 2 Solution</TITLE>
</HEAD>
<BODY>
<! Regular HTML statements that are always carried out could follow... -->
<H3>Welcome to the Question Page</H3>
<p>Q1. What eats lots of oranges and has 80,000 legs?
<p>Q2. Why is the grass green?
<BR>
<SCRIPT LANGUAGE=VBS>
<!-
dim rc ' Stores return code value
rc = msgbox("Do you want extra hints to be displayed on this page?", _
vbYesNo + vbQuestion,"Setting up the page")
if rc = vbYes then
' Show Big Spender Page
Spender = vbTrue
document.write "<H2>Extra Hints</H2>"
document.write "<p>Q1. Think about 26.2 miles"
document.write "<p>Q1. It involves an animal"
' ... more document.write statements to display hints could go here
end if
-->
</SCRIPT>
<BR>
<p>A1. The field in the Boston Marathon
<p>A2. If it was white you couldn't tell if a polar bear was in your lawn!
</BODY>
</HTML>
rc = MsgBox ("Do you want poor Fred to bike 68 miles by himself?", _
vbYesNo + vbDefaultButton2, "Biking Question")
if StrComp(txtDepartment.Value, "Accounting", 1) = 0 then
' Accounting department has been entered
else
' Accounting department was not entered
end if
Note that the non-0 third parameter to StrComp instructs the function to carry out a match that is not case sensitive.
dim blnDone ' indicates when the string count is complete
dim intCount ' counter of how many times Brown is detected in string
dim intFoundPosition ' the position where Brown is found in string
dim intCurrentPosition ' the next position to search for Brown in string
' Initialize variables to start looking at string
blnDone = False
intCurrentPosition = 1
' Continue to advance through string until done
' condition no more Brown's are found
do while not blnDone
' Find where the next Brown is
intFoundPosition = InStr(intCurrentPosition, _
s_strBorrower, "Brown")
' See if Brown was found
if intFoundPosition = 0 then
' No more Brown's are in string so the search is done
blnDone = vbTrue
else
' Brown was found, increment count and prepare
' to search again after this one
intCount = intCount + 1
intCurrentPosition = intFoundPosition + len("Brown")
end if
loop
msgbox "Brown borrowed the guide " & intCount & " times. "
Dim dtmNow
' Get current time
dtmNow = Now()
if s_dtmEvent < dtmNow then
msgbox "Registration deadline is passed."
End if
dim intCounter, intLeftOverPlayers
for intCounter = 1 to 1000
intLeftOverPlayers = _TotalPlayers Mod 9
next
msgbox intLeftOverPlayers
If a > 3 then
b = c + 1
MsgBox "After b= c + 1", vbOKOnly , "Debug-Time Trace"
else
if a = 1 then
b = c + 4
MsgBox "After b= c + 4", vbOKOnly , "Debug-Time Trace"
else
if < -2 then
b = c + 7
MsgBox "After b= c + 7" vbOKOnly , "Debug-Time Trace"
end if
end if
end if
dim strVarP, strVarQ, strVarR
' Retrieve the subtype and value of each of the variables and then display_ it.
' vbCrLf is used to separate lines in the output message.
strVarP = "(P) Subtype = " & VarType(P) & " Value = " & P
strVarQ = "(Q) Subtype = " & VarType(Q) & " Value = " & Q
strVarR = "(R) Subtype = " & VarType(R ) & " Value = " & R
MsgBox strVarP & vbCrLf & strVarQ & vbCrLf & strVarR, vbOKOnly, "Debug Variables"
r = q / p
This code will print the value and subtype of each variable. This trace code must be inserted before the problem statement. If you inserted it after the problem statement, the runtime error would occur first and your trace statement would never be reached (unless you used On Error Resume Next).
The statements will print the variable's current value as well as the subtype, but nothing will be displayed when printing the value of an empty or a null variable. In some cases, the subtype of the variable may be empty or null. If so, the value returned by the VarType function will provide you with this information. (See Chapter 4 for a full discussion of VarType capabilities.) For other subtypes, the standard subtype and value of the variable are displayed.
With this information on each variable involved in the equation, you should be able to pinpoint the cause of any type of error that occurs. Inspection of this information would show that one of the variables being used in the division has an underlying subtype of double (VarType = 5) and the other has an underlying subtype of string data (VarType = 8). The remaining variable has a subtype of empty (VarType = 0), but that could be correct since that is the result variable and perhaps no values have yet been assigned to it. The fact that a string is being used in the division, however, should be enough to tell you there is an errant code assignment somewhere, and lead you to other areas of the code to find the culprit. One potential cause of such a problem could be code like the following:
dim p, q, r
p = 1.3
q = "a"
r = q / p
This type of error is obvious if the statements are clustered together, but if the assignments are not in the vicinity of the division statement, then analyzing variables is usually a necessary first step to knowing where to look for the cause of the problem.
Another way to carry out the same type of analysis would be to use the VarAnalyze procedure. This procedure provides the same capabilities as the code snippet above, but the procedure itself generates information each time it is called. The procedure also provides a verbal description of the VarType (such as empty) rather than just the numeric value. If the VarAnalyze procedure were used, the trace would appear like this:
' Retrieve the subtype and value of each of the variables and then display_ it.
' vbCrLf is used to separate lines in the output message.
VarAnalyze ("Contents of p prior to the division: ", p)
VarAnalyze ("Contents of q prior to the division: ", q)
VarAnalyze ("Contents of r prior to the division: ", r)
r = q / p
sub lnkguides_OnClick
. . .
end sub
sub lnkguides_MouseMove (s, b, x, y)
. . .
end sub
sub lnkguides_Click
location.href = "http://www.microsoft.com/vbscript"
end sub
' Make sure the user age is 18 or over
if document.frmApply.txtAge.value < 18 then
MsgBox "Sorry youngster, you're not old enough to apply!", _
vbOKOnly,"Can't take app"
frmApply_OnSubmit=False
else
document.frmApply.Submit
MsgBox "Application processed", vbOKOnly, "Confirmation"
frmApply_OnSubmit=True
end if
end sub
-->
<!--
Function frmApply_OnSubmit
' Process the application by submitting to server if data is valid
' Make sure the user age is 18 or over
if document.frmApply.txtAge.value < 18 then
MsgBox "Sorry youngster, you're not old enough to apply!", _
vbOKOnly,"Can't take the application"
elseif len(document.frmName) = 0 then
frmApply_OnSubmit=False
MsgBox "Sorry, we can't take an application from just nobody!",
vbOKOnly,"Can't take app"
frmApply_ONSubmit=false
else
document.frmApply.Submit
MsgBox "Application processed", vbOKOnly, "Confirmation"
frmApply_OnSubmit=True
end if
end function
-->
</script>
dim TAX_RATE
dim ITEM_PRICE
Dim intUnits as Integer
Dim intCost as Integer
' Set up constant values
TAX_RATE = 0.04
ITEM_PRICE = 25
' Calculate the total cost
intUnits = InputBox("How many units do you wish to purchase?")
intCost = (ITEM_PRICE * intUnits) * TAX_RATE
' Store cost in its integer representation
intCost = cInt(intCost)