As Kevin mentioned in a previous introduction to ASP, you don't
have to use VBScript with ASP pages. You can also use JScript, which is
pretty much
identical to JavaScript - it's Microsoft's version -
and ActiveState offers
Perl scripting, which is a nice option for you Perl coders.
But I've decided to use VBScript with ASP for two reasons. First,
it is the language most commonly used with ASP, and second, it is quite
possibly the easiest programming language ever created. If you're
dubious, I think I can bring you around. You already know
what this code does.
<% do while not cars.eof
Response.Write(cars(1))
cars.movenext
loop%>
Now try - think really hard - and figure out the meaning of this statement.
<%
If cars(0) = 'Jaguar' then
Response.write "Jags rule!"
Elseif cars(0) = 'Mercedes' then
Response.write "I love my poshmobile!"
Else
Response.write "My car ain't worth nuthin'!"
End if
%>
This is just a basic if-else statement. In other
words, if the car
name is Jaguar, the appropriate pro-Jag text will print, but if the car is
a Mercedes, it will print a different message, and in all other circumstances,
a third message will print.
Let's get totally fancy and put these two pieces together.
<% do while not cars.eof
If cars(0) = 'Jaguar' then
Response.write "Jags rule!"
Elseif cars(0) = 'Mercedes' then
Response.write "I love my poshmobile!"
Else
Response.write "My car ain't worth nuthin'!"
End if
cars.movenext
loop%>
We're still checking for Jaguars and Mercedes (so we can print out
the appropriate message), but now we're checking each and every row in the
ADO recordset.
Believe it or not, that, along with some knowledge of
VBScript operators, combined with what you already know about ADO and two-dimensional
arrays, is
enough to get you through quite a lot of coding for your database. In fact,
we'll be building the actual pages of our application using these very
constructs in the next
lesson.
But before we do that, let's practice our VBScript a bit, shall we?
One thing you need to know about VBScript is that it deals with variable
types strangely. There is really only one kind of
variable in VBScript, and it's called a variant. This does not
mean, however, that all variants are the same. There is a numeric variant,
a date
variant, a text-string variant, as well as some others. The theory with
variant is that VBScript is smart enough to figure out
what kind of variable you want and will assign it automatically. And this can
cause problems. It's
something you'll want to be aware of down the road.
Also, it's not necessary to declare variables in VBScript. Of course,
good coders
always declare their variables, and so should you (using the Dim statement).
But the following commands would execute without a problem even if there
were no preceding Dim statements.
<%
foo=6
bar=8
foobar=foo + bar
Response.Write(foobar)
%>
If you want to learn a bunch more about VBScript, you can start by
taking the Microsoft tutorial, or
browse through the language
reference info.
Obviously, the onus is on you to go out and learn more
VBScript. But I think you're up to it. Once you're comfy with
VBScript, you're ready to add forms to the mix.
next page»