When writing ASP scripts that deal with databases, you often
aren't fortunate enough to have a database whose naming
conventions make sense given the context of your script.
While it might be obvious that something named first comes
before something named last, do the hypothetical fields
store someone's first and last name or the date
of their first and last transaction?
There's no need to carry this ambiguity through into the
recordsets you use in your ASP code. You
can easily reference the fields by more accurate names to make
your code more readable. The secret is simply in the SQL command
you use.
SELECT first, last, date FROM tblEmployee
might get the job done, but
SELECT first As FirstName, last As LastName, date As DateOfBirth FROM tblEmployee
leaves no question what those fields actually contain and will make
the rest of your code that deals with them that much
easier to understand. Especially since you can use the new
aliases when requesting the fields from your recordset.
Another time when this technique can be extremely valuable is
when you don't know what a field would otherwise be called.
This is often the case when you're doing a calculation of some sort.
For example...
SELECT Count(*) FROM tblEmployee
might return a count of all the employees, but you'll be forced to
reference it like this:
rstEmployee.Fields(0).Value
If you instead use...
SELECT Count(*) As EmployeeCount FROM tblEmployee
you can more acurately indicate what you're
trying to retreive from the recordset:
rstEmployee.Fields("EmployeeCount").Value
If you have a tip you would like to submit, please send it to:
webmaster@asp101.com.