Schongar P.VBScript unleashed.1997
.pdfListing 3.4. Day.htm.
<HTML>
<HEAD>
<TITLE>Day of the week</TITLE>
</HEAD>
<BODY>
<H1>What day did it happen?</H1>
<HR COLOR="BLUE">
Enter any valid date and click the button to find out what day it was!<BR>
<INPUT TYPE="TEXT" NAME="TxtDate"><BR>
<INPUT TYPE="SUBMIT" NAME="Btn1" VALUE="Tell me the day of the week">
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Btn1_OnClick()
Dim DayVal, Message, MyDate
MyDate = DateValue(TxtDate.Value)
DayVal = Weekday(MyDate)
If DayVal = 1 then Message = "Sunday"
If DayVal = 2 then Message = "Monday"
If DayVal = 3 then Message = "Tuesday"
If DayVal = 4 then Message = "Wednesday"
If DayVal = 5 then Message = "Thursday"
If DayVal = 6 then Message = "Friday"
If DayVal = 7 then Message = "Saturday"
Message = "It happened on a " + Message + "."
MsgBox Message, 64,"When did it happen?"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
Minute
The Minute function retrieves the minute value of a time value.
Month
The Month function returns a numeric value for the month from a valid date.
Now
The Now function returns the current date and time from the client machine. This function takes no arguments.
Second
The Second function returns the seconds value from a time value.
Time
The Time function returns the current system time as a date subtype variant.
TimeSerial
The TimeSerial function takes hours, minutes, and seconds as arguments and returns a variant of subtype date. The hour argument is an integer between 0 and 23.
TimeValue
The TimeValue function takes a string containing a valid time and returns a variant of subtype date containing the time.
You can use this function to get input from the user and convert it to a date format. Valid values for the time argument include times from 12and 24-hour clocks.
Boolean Functions
Boolean functions always return a value of true or false. Each of the functions listed in Table 3.9 tests for the truth of a condition.
Table 3.9. Boolean functions.
Function |
Tests |
IsArray |
Is variable an array? |
IsDate |
Is expression a date? |
IsEmpty |
Has the variable been initialized? |
IsError |
Is this an error value? |
IsNull |
Is this a null value? |
IsNumeric |
Is this a numeric value? |
IsObject |
Is this variable an object? |
These Boolean functions are important because VBScript has little built-in error checking and no debugger other than Internet Explorer. You can use the Boolean functions to test data before trying to feed the data into functions where it might cause an error.
Listing 3.4 had no built-in error checking. Listing 3.5 shows the same program with a feature to check whether the data from the text input box is a valid date.
Listing 3.5. DayChk.htm.
<HTML>
<HEAD>
<TITLE>Day of the week</TITLE>
</HEAD>
<BODY>
<H1>What day did it happen?</H1>
<HR COLOR="BLUE">
Enter any valid date and click the button to find out what day it was!<BR>
<INPUT TYPE="TEXT" NAME="TxtDate"><BR>
<INPUT TYPE="SUBMIT" NAME="Btn1" VALUE="Tell me the day of the week">
<SCRIPT LANGUAGE="VBS">
<!--
Sub Btn1_OnClick()
Dim DayVal, Message, MyDate, blnCheck
blnCheck = IsDate(TxtDate.Value)
If blnCheck = True then
MyDate |
= DateValue(TxtDate.Value) |
|||
DayVal |
= Weekday(MyDate) |
|||
If |
DayVal |
= |
1 |
then Message = "Sunday" |
If |
DayVal |
= |
2 |
then Message = "Monday" |
If |
DayVal |
= |
3 |
then Message = "Tuesday" |
If |
DayVal |
= |
4 |
then Message = "Wednesday" |
If |
DayVal |
= |
5 |
then Message = "Thursday" |
If |
DayVal |
= |
6 |
then Message = "Friday" |
If |
DayVal |
= |
7 |
then Message = "Saturday" |
Message = "It |
happened on a " + Message + "." |
|||
MsgBox |
Message, |
64,"When did it happen?" |
||
Else
Message = "You must enter a valid date."
MsgBox Message, 48,"Error"
End If
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
If you run DayChk.htm and enter an invalid date, instead of having the script crash with a runtime error, you'll see a message box telling you that the date you entered was not valid. You'll learn more about techniques that you can use to deliver error-free code in Chapter 11, "Optimizing Code."
Review
In this chapter, you learned about procedures in VBScript. You read about functions intrinsic to VBScript and looked at some examples that illustrate the usefulness of functions.
In the next chapter, you'll learn about VBScript's built-in controls and how these basic controls are used to create
powerful HTML documents.
Chapter 4
Intrinsic Controls
by Brian Johnson
CONTENTS
●Introduction
●Events in VBScript
●Messages in a GUI Environment
●Placing Controls in HTML
●Intrinsic Controls
Button
●Checkbox
Hidden
Text
Textarea
●Select
●Using Controls in Your Documents
●Review
Introduction
VBScript's intrinsic controls are the same ones that you are using if you're working with forms in HTML documents. I'll talk about each of these controls and how they can be used to create interactive Web pages using VBScript.
This chapter covers
●Controls and messages in VBScript
●Using intrinsic controls with VBScript
●Each of the intrinsic controls in detail
●Client-side validation
Events in VBScript
So far, you've created active pages with code that runs when the user clicks a button. Clicking a button generates an event. Events in Windows generate messages. Messages in Windows tell the applications and the operating system what to do and what's going on.
Most of the objects that you place into your HTML documents will have events to which you can attach script code. The Button control you've been using so far has an onClick event. When this event occurs, code in the onClick
procedure runs.
Visual Basic is an inherently graphical programming language. The way most information is retrieved and processed from the user is through graphical user interface (GUI) objects that you create for your user. Once the GUI is in place, the user causes the functions and procedures within your program to be initiated through keystrokes or mouse clicks. Table 4.1 defines these terms that I've just used.
|
Table 4.1. Interaction terms. |
Term |
Definition |
Data |
Information that is retrieved, manipulated, and returned to the user |
GUI |
Graphical User Interface-the buttons, boxes, labels, and other |
|
elements of your program that you use to interact with your user |
Event |
An action by the user, such as a mouse click or a keystroke |
Messages |
Sent back and forth from the operating system to the program in |
|
response to events |
Messages in a GUI Environment
Events generate messages in a GUI environment, but not all events are initiated by the actions of the user. There are a number of other ways that events are triggered.
Timers generate messages by default at the end of their timing cycles. These cycles are set by the programmer at design time but can be manipulated by the user at runtime.
Hardware can also generate messages. For example, a message is sent to the operating system when a CD is inserted in the CD-ROM. If the CD is built up for Windows 95, a program on the disk runs automatically. Likewise, if your machine is set up to receive faxes, a message is sent when your phone rings.
Multimedia generates its own types of messages. The multimedia system in the Windows operating system includes highresolution timers that keep count of frames and time while a sound or video file is playing. During the start and completion of multimedia files, messages are also sent.
Depending on the types of control that you're using in your Web pages, you can write code that reacts to these types of messages. When dealing with the intrinsic controls though, the messages that you will deal with most often include mouse clicks, mouse movements, and keystrokes.
Placing Controls in HTML
You can put these intrinsic controls on your page just as you would regular forms controls. The usual syntax is to place the <INPUT> tag into your HTML and use the appropriate TYPE= attribute for the control that you're creating. Placement of the controls on your page is entirely dependent on the HTML code. Listing 4.1 is an HTML page that contains a number of Internet Explorer's intrinsic controls. You can see the result in Figure 4.1.
Figure 4.1 : VBScript intrinsic controls in HTML.
Listing 4.1. HTML document with controls.
<HTML>
<HEAD>
<TITLE>Tester Page</TITLE>
</HEAD>
<BODY>
<H1>Intrinsic Controls in VB Script</H1>
<HR COLOR="BLUE">
<CENTER>
<TABLE>
<TR>
<TD><FONT SIZE=5 FACE=ARIAL COLOR=BLUE>Controls...</FONT></TD>
<TD><INPUT TYPE="SUBMIT" NAME="Btn1" VALUE="This is button #1"></
TD>
<TD><INPUT TYPE="TEXT" NAME="Txt1" VALUE="TEXT" ></TD>
</TR>
<TR>
<TD></TD>
<TD><INPUT TYPE="RADIO" NAME="Rad1">Radio Button</TD>
<TD><INPUT TYPE="TEXTAREA" NAME="TextArea1"
ROWS=50 COLS=25 VALUE="TEXTAREA"></TD>
</TR>
<TR>
<TD><INPUT TYPE="PASSWORD" NAME="Pass1" VALUE="PASS"></TD>
<TD><INPUT TYPE="RESET" NAME="Reset1" VALUE="Reset #1">
<TD><INPUT TYPE="CHECKBOX" NAME="Radio1"
VALUE="Checkbox">Checkbox</TD>
</TR>
</TABLE>
</CENTER>
<SCRIPT LANGUAGE="VBS">
<!--
Sub Btn1_OnClick()
Dim Message
Message="Hello World!"
MsgBox Message, 0,"Tester Result"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
In the preceding example, controls are displayed in an HTML table. Tables offer fairly good control over where your controls are placed on a form, but it takes a little work to get the controls placed where you want them. Microsoft FrontPage allows you to create WYSIWYG (what you see is what you get) tables in Web pages. It's much easier to use a program like FrontPage to lay out a table visually, as shown in Figure 4.2.
Figure 4.2 : Laying out an HTML document with controls in Microsoft FrontPage.
Intrinsic Controls
Button
I've used the button input type, Submit, to test most of the code we've played with so far. It's an easy control to use because users know what to do with it automatically: see a button and click it. What could be easier? Table 4.2 shows the properties for a Button control.
|
Table 4.2. Button properties. |
Property |
Description |
enabled |
Control is enabled (1) or disabled (0) |
form |
Name of the form to which the control belongs |
name |
Name used to identify the button in code |
value |
Caption of the button |
Most of the intrinsic controls are created as a parameter of the <INPUT> tag. To create an instance of a Button control in HTML, insert an <INPUT> tag with the type set to BUTTON or SUBMIT:
<INPUT TYPE=SUBMIT NAME=Button1 VALUE="Click Me">
The SUBMIT type can be used interchangeably with BUTTON. The RESET type works the same way but is used to clear all the values in a current page.
Properties for the Button control can be set at load time and changed at runtime. Take a look at how properties are set for an intrinsic control at load time in Listing 4.2. Clicking the button causes the runtime event to occur, changing the value of the button.
Listing 4.2. Setting properties for the Submit control.
<HTML>
<HEAD>
<TITLE>Tester Page</TITLE>
</HEAD>
<BODY>
<H1>Tester Page for VBScript</H1>
<HR COLOR="BLUE">
<INPUT TYPE="SUBMIT" NAME="Btn1" VALUE="Click to test the code">
<SCRIPT LANGUAGE="VBScript">
<!--
Sub Btn1_OnClick
Btn1.Value = "Value is changed!"
End Sub
-->
</SCRIPT>
</BODY>
</HTML>
We've done this before. Here, we set the type, name, and value of the <INPUT> tag when a document is loaded into the browser. Notice how we can change the value property of Btn1 in the Btn1_onClick event. This change takes
