Visual Basic Scripting: Michael Hahsler
Visual Basic Scripting: Michael Hahsler
Michael Hahsler
29.2.2002
1
Contents
• Scripting
• ASP files
• Features of VBScript
– Dictionary
– File
– Formating, Strings
– Loops
– Functions
2
Scripting
• Client side: JavaScript
• Server side: VBScript for MS-servers; most
other servers support Perl, C, Java, PHP,...
3
Processing of Active Server
Pages
User 1. Request
2.
(Web Browser) Web Server Web Page (.asp)
6. Web Page Script Processor
(Visual Basic
Script)
3. Loads ADO Objects
5. Results
ActiveX Data
Objects (ADO)
4
A simple ASP file
<html><head></head><body>
<%
Response.Write "<p>Hello World!</p>" &_
vbCrLf
%>
</body></html>
5
Other ways to include Code
Short for Response.Write:
<% = "<p>It's " & Now()& "</p>" %>
6
Features of VBScript
• Implicit variable definitions (exception
arrays Dim a_array(100))
• Weak typing of variables
• No Modules
• Objects specific to VBScript
– Dictionary (associative array)
– File (access to files)
7
Dictionary
Set person =
Server.CreateObject("Scription.Dictiona
ry")
person("fname") = "Jim"
person("sname") = "Buyens"
person("state") = "Arizona"
8
Dictionary: Example
Set holdRec =
Server.CreateObject("Scription.Dictiona
ry")
For Each fld In rs.Fields
holdRec(fld.Name) = fld.Value
Next
For Each ky In holdRec
Response.Write "<p>" & ky & "=" &_
holdRec(ky) & "</p>" & vbCrLF
Next
9
File and Function
<%
Set fs = Server.CreateObject("Scripting.FileSystemObject")
Function GetFileDate(argURL)
Set fileObj =
fs.GetFile(Server.MapPath(argURL))
GetFileDate = file.Obj.DateLastModified
Set fileObj = Nothing
End Function
%>
10
Formatting Functions
amt = 12.25
Response.Write FormatCurrency(amt)
Response.Write FormatPercent(amt)
Response.Write FormatDateTime(Date())
11
String Functions
status = Split("test,21,guest",",")
statusJoined = Join(status,";")
result = Filter(status,"es")
pos = InStr("--xy-----xy--","xy")
Replace("bicycle","bi","motor")
12
Loops
Do [{While | Until} condition]
[Exit Do]
...
Loop
While expression
...
Wend
13
Logic Control
if expression Then
...
Else
...
End If
14
Functions and Subroutines
Function name [(argument,...)]
...
name=expression
End Function
15