Search Search

#1 worldwide
FREE Coding Lessons

since 1996
   THE BEST WAY to learn ASP & Asp.net!
Advertise Here!
click for details
Credits Host:
DiscountASP.net
Server Admin:
The "Team"
Contact Info.
Charles M. Carroll

my Blog
[prev. Lesson]  Subroutine: Query2List
     [next Lesson]  Subroutines w/Dictionary Objects

Subroutines - A "Flexible Approach" by Charles Carroll

Subroutines can be very cleverly constructed so that you can use the same routine to power several different tasks instead of copying code as the following examples illustrates!

   filename=/learn/test/subreusabledataform.asp

<Test Script Below>


<HEAD><TITLE>subreusabledataform.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
call query2entryform("select * from publishers where pubid=16")
%>
</BODY></HTML>

   filename=/learn/test/subreusabledatalist.asp

<Test Script Below>


<HEAD><TITLE>subreusabledatalist.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/lib_subflexible.asp"-->
<%
call query2list("select distinct state from publishers","thestate")
call query2list("select distinct city from publishers","thecity")
call query2list("select distinct zip from publishers","thezip")
%>
</BODY></HTML>

   filename=/learn/test/subreusabledatatable.asp

<Test Script Below>


<HEAD><TITLE>subreusabledatatable.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
call query2table("select * from publishers")
%>
</BODY></HTML>

   filename=/learn/test/subreusableform.asp

<Test Script Below>


<HEAD><TITLE>subreusableform.asp</TITLE></HEAD>
<HTML><body bgcolor="#FFFFFF">
<!--#include virtual="/learn/test/subflexible.asp"-->
<%
call query2form("select * from publishers")
%>
</BODY></HTML>

The library file lib_subflexible.asp looks like:

   filename=/learn/test/lib_subflexible.asp

<Test Script Below>


<% 
dim htmlstart, htmlend
dim rowstart, rowend
dim fieldstart, fieldend
dim namestart, nameend
dim fieldnames
fieldnames=false

sub query2list(myquery,listname)
    htmlstart="<select name='" & listname & "'>"
    htmlend="</select>"
    rowstart="<option>"
    rowend="</option>"
    fieldstart=""
    fieldend=""
    call query2html(myquery)
end sub

sub query2table(myquery)
    htmlstart="<table border=1>"
    htmlend="</table>"
    rowstart="<tr>"
    rowend="</tr>"
    fieldstart="<td valign=top>"
    fieldend="</td>"
    call query2html(myquery)
end sub

sub query2form(myquery)
    htmlstart=""
    htmlend=""
    rowstart=""
    rowend="<hr>"
    fieldstart=""
    fieldend="<br>"
    fieldnames=true
    namestart=""
    nameend="&nbsp;=&nbsp;"
    call query2html(myquery)
end sub

sub query2entryform(myquery)
    htmlstart=""
    htmlend=""
    rowstart=""
    rowend=""
    fieldstart="%name%&nbsp;=&nbsp;<input type='text name='%name%' value='"
    fieldend="' size='%size%'><br>"
    fieldnames=false
    namestart=""
    nameend="&nbsp;&nbsp;="
    call query2html(myquery)
end sub

sub query2html(inputquery)
    set conntemp=server.createobject("adodb.connection")
    conntemp.open "DSN=Student;uid=student;pwd=magic"
    set rstemp=conntemp.execute(inputquery)
    howmanyfields=rstemp.fields.count -1
    redim fsa(howmanyfields)
    redim fea(howmanyfields)
    for i = 0 to howmanyfields
        tempstart=replace(fieldstart,"%name%",rstemp(i).name)
        tempend=replace(fieldend,"%name%",rstemp(i).name)
        tempstart=replace(tempstart,"%size%",rstemp(i).actualsize)
        tempend=replace(tempend,"%size%",rstemp(i).actualsize)
        fsa(i)=tempstart
        fea(i)=tempend
    next 
    response.write htmlstart & vbcrlf
    counter=0
    do until rstemp.eof
        response.write rowstart & vbcrlf
        for i = 0 to howmanyfields
            if fieldnames=true then
                response.write namestart & rstemp(i).name & nameend
            end if
            response.write fsa(i) & rstemp(i) & fea(i) & vbcrlf
        next 
        response.write rowend & vbcrlf
        counter=counter+1
        rstemp.movenext 
        if response.isclientconnected=false then
            exit do
        end if
    loop
rstemp.close
set rstemp=nothing
conntemp.close
set conntemp=nothing
response.write htmlend
end sub
%>

Chaz Wish List
Tall Tip $5
Grande Tip $20
Venti Tip $39
Tip Jar Thanks
2004 Thanks
2005 Thanks
HUGE Tip -love site