Rick Curtis - Tutorial 9
Using Access or SQL Server to Write your ASP Code
Sometimes I'm just plain lazy. Other times I don't want to have to spend hours debugging ASP code because of typos so I've found a way to help short cut some repetitive ASP coding tasks. The idea is an old one that I used back in Access 2.0 days to write Web pages in Access. The idea is to mix strings of text with database field values to create the lines of code you need. So here we go.
I was working on a site that contains resumes. The headache for the database design for this is that there are about 70 different skills to track for people. Since the resumes aren't stored permanently but removed after 6 months creating a multi-table relational database to store this would have been a nightmare. So I went with one basic table storing the person's resume in one place. This means a lot of fields in the database.
I also needed a Lookup table to store all the Skill values to be able to use for dynamically created list boxes in UltraDev. So I first created the Lookup table called Skills.
Here's some sample data.
There are 4 other Categories within the skills table for a total of 70 different skills. I created a total of 5 recordsets each one showing only the records from that Category like the Medical Category above. I used these 5 recordsets in an Insert page to drive dynamic check boxes so that people could check yes if they have that skill.
Now let's look at the Resume Table. It obviously has name, address, and all the skill fields. As you can see I gave the field names for the skills the same value as the Primary Key Name in the Skills table so that I could easily make the connections. [It also means that if I need to add more skills I add them to the Skills table with the descriptive name and can easily add field M9 to the Resume table making it much easier to read.]
On the Insert form I give each Checkbox a name (M1, M2, M3...) so that when I apply the UD Insert Server Behavior it maps the the correct Checkbox to the proper field in the Resume table. Here's a more concise way to say it.
Creating a View/Query/Stored Procedure
You can accomplish this using either Access 97/2000 or SQL Server. In my case I am using SQL Server so the syntax for things like string concatenation are a little different (SQL uses the '+' sign while Access uses the '&' sign). Here is a View I created in SQL 7 using the query grid that is similar to Access. Press the SQL button on the toolbar to see the SQL code generated. The code in blue is the basic SQL code which would map out to SELECT SkillID, Lookup FROM Skills. Everything in red is simply a string set off in single quotes. You will see that the single quotes are in blue because they are part of the SQL syntax, not the string. In the places where I need actual field values I have to close the string with a single quote then use the + sign to concatenate the string to the field value then another + and a new single quote to connect to the next string. You need to pay special attention to where you need spaces and single or double quotation marks as you concatenate the entire string. It takes little trial and error, but once it is set up it works like a charm.
What I am going to do is run a query off the Skills table which will bring up two field values, the Skill ID and the Lookup description. To that query I and going to add text strings that contain the ASP code I need. Once you start adding strings, the grid is no longer showing just a single field so it adds the AS Expr1 clause.
IF (rsSearch.Fields.Item("' + SkillID
+ '").Value) = "True" THEN
Response.Write "<li> ' + Lookup
+ ' </li>" END IF %>'
When you run the query or view here is what you see in the results grid. Voila, perfect ASP code almost ready to cut and paste into your page. I say almost ready because what you see in the query grid may not be what you get when you actually copy it (SQL Server only). In Access what you see is what you get. In SQL Server copying the query rows adds extra double quotes so what you get when you copy it is " at the ends and "" inside as shown below. The extraneous double quotes are shown in red.
"<% IF (rsSearch.Fields.Item(""M1"").Value) = ""True"" THEN Response.Write ""<li> CPR </li>"" END IF %>"
A quick search and replace will eliminate the doubles leaving you with perfect code to paste into your page.
<% IF (rsSearch.Fields.Item("M1").Value) = "True"
THEN Response.Write "<li> CPR </li>" END IF %>
Let me explain what is happening with this code. Since there is no actual relationship between the two tables don't have a way of connecting the Skills Table and the Resumes table directly. The rsSearch recordset is the UD recordset that calls up the Resume table based on user search criteria. By having the SkillID Primary Key value be M1 and that is the same name for the corresponding field in the Resume Table I can essentially create the ASP lookup statement just from the Skills query in red and blue above.
The actual ASP code is simply evaluating if the Resume table field M1 value is True, if so it writes out the Description in an unordered list. I took me a little time to work this out, but thinking about typing/pasting & editing all 70 ASP statements and then figuring out where I typed it incorrectly, it was worth it. Also, if I add new skills to the Skills table and corresponding fields to the Resume table all I have to do is run the query again and cut and paste in the new values. You obviously won't write an whole page this way, but anytime you have a lot of repetitive code that would be interspersed with database values, this technique can save a lot of time.
Another quick example uses the following stored procedure. This is another lookup but in this case I wanted to have the database generate the ASP code in the correctly sorted order. Well SQL Views don't support the ORDER BY clause (unless you go through a bunch of extra steps that I won't go into). So I wrote the following stored procedure.
Alter Procedure spASPPopulation
You can use this same approach anywhere you need to create repetitive ASP code that interfaces with your database. For example, imagine that you needed to define 70 variables on a page. Just set up a string that merges your Dim statement with your database field names/values and just cut and paste. Hope you find this tip useful.
|Copyright © 2000 All rights reserved Rick Curtis, Princeton,
Macromedia and UltraDev are trademarks of the Macromedia Corporation.