Miscellaneous code collected from elsewhere that I want to keep handy


Delete that works to delete multiple files

if request.form("df")="yes" then
if NOT request.form("dfile") ="" then
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(Server.MapPath("../../../../_private/files/" & "thumbs/" & request.form("dTHfile")))


Set objFSO2 = Server.CreateObject("Scripting.FileSystemObject")
objFSO2.DeleteFile(Server.MapPath("../../../../_private/files/" & request.form("dfile")))
end if
end if


Table Names in Select Box to enter into a form

In this example, we have a form for uploading a new icon image with the image file to go into a folder and the image filename to go into the Table "ICONS".  We want to designate a specific table for each Icon to be associated with. This is how to display and select from the table names.

<%
'here is where you set the DSN for the application
Session("DSN") = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Request.ServerVariables("APPL_PHYSICAL_PATH") & "..\database\yourdatabase.mdb" & ";"
Set DBConn = Server.CreateObject("ADODB.Connection")
DBConn.Open Session("DSN")
Set rstSchema = DBConn.OpenSchema(20)
%>
<SELECT name="x_ICONTABLE" id="x_ICONTABLE" size=2 style= "HEIGHT: 100px; WIDTH:200px">
<%
Do Until rstSchema.EOF
'here you specify a table, should be a query this way they can defined by us first and presented to the user.
If rstSchema("TABLE_TYPE") = "TABLE" Or rstSchema("TABLE_TYPE") = "VIEW" Then
'TABLE for database tables and VIEWS for queries or views
response.write "<OPTION VALUE= """ & rstSchema("TABLE_NAME") &""">" & _
rstSchema("TABLE_TYPE") & " : " & rstSchema("TABLE_NAME") &"</option>" & vbCRLF
'nest query in here to get the fields.
End If
rstSchema.MoveNext
Loop
Set rstSchema = Nothing
DBConn.Close
Set DBConn = Nothing
%>
</SELECT>


The following is nice for when you want to use a single asp file for giving different queries. In other words, making a single page file behave like four or more separate pages!
In the example below, the first strsql differentiates according to whether the page request is for showing broodmares or for showing stallions.

The second strsql statement filters for the horses that are for sale.

The third one returns a list of all the horses regardless of gender or sale status.

Conditional SQL Statements:

if NOT request.querystring("gen")="" then
strsql = "select * from [horsedata] WHERE ACCTID ="& strAcct &" AND horsedata.gender ="""& request.querystring("gen")&""""
end if
if NOT request.querystring("sale")="" then
strsql = "select * from [horsedata] WHERE ACCTID ="& strAcct &" AND [horsedata.for-sale]="&"""Yes"""
end if
if request.querystring("sale")="" then
if request.querystring("gen")="" then
strsql = "select * from [horsedata] WHERE ACCTID ="& strAcct &""
end if
end if


Get Primary Key Autonumbered Identity of Newly Inserted Record, Access Database

This isn't perfect, because all it actually does is return the most recently created autonumber. You can't be guaranteed that this is actually the record you want. However, odds are extremely exceptionally excellent that it is!


<%

xDb_Conn_Str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("../../database/yourdatabase.mdb") & ";"
' Open Connection to the database
Set conn = Server.CreateObject("ADODB.Connection")
conn.Open xDb_Conn_Str

Set oRs = conn.Execute("SELECT MAX(ACCOUNTID) FROM ACCOUNTS", , adCmdText) ' ACCOUNTID relates to your primary key field. Use your primary key field name, whether it is ID or MYID or YOURPRIMARYKEY, whatever.

Your_ID = oRs(0).Value

response.write Your_ID

oRs.Close
Set oRs = Nothing
conn.Close
Set conn = Nothing
%>


VERY simple database display from this code: http://www.planet-source-code.com/vb/scripts/ShowCode.asp?lngWId=4&txtCodeId=7088

<%

set Conn=Server.CreateObject("ADODB.Connection") 'create Server Database Connection Object
set RS = server.CreateObject("ADODB.Recordset") 'create RecordSet Conn Obj
Conn.open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & server.mappath("../../../database/yourdb.mdb") & ";"


sql= ("SELECT [USERID], [USEREMAIL] FROM USERS WHERE [USERID]='"& Session("USER_status_UserID")&"'")


RS.Open SQL, Conn, 3, 3



if RS.EOF then 'traps for IF DB empty THEN:
Response.Write "there is no such data"
Response.End
end if


Response.Write RS("USEREMAIL")
%>
 


Filter records by Year of Date Column

If you have a date column, it may have the month, day, time, year. Perhaps you want to filter the records by the YEAR of the date only.

strsql = "SELECT DISTINCT ([HORSES].[HORSEBREED]), (SELECT COUNT (*) FROM HORSES WHERE HORSEGENDER='Mare') AS TOTAL FROM HORSES" WHERE HORSEGENDER='"& strSex &"'" & " AND (year([HORSEBDAY]))="& request.querystring("yr") &""


Show Records Only by Date Expired

"SELECT [USERS].[USERID], [USERS].[USEREMAIL], [PAYMENTS].[PAYMTID], [PAYMENTS].[PAYMENT_DATE],[PAYMENTS].[EXPIREWEB], [PAYMENTS].[ITEM_NAME], [PAYMENTS].[ITEM_NUMBER], [PAYMENTS].[QUANTITY], [PAYMENTS].[PAYER_EMAIL], [PAYMENTS].[CREDIT] FROM USERS INNER JOIN PAYMENTS ON [USERS].[USEREMAIL] =[PAYMENTS].[PAYER_EMAIL] AND ((DateDiff('d', [EXPIREWEB], Date()))<1)"


SQL Statements that I worked very hard to write

It nests a select count query inside another query. This is what the results look like:

CLASS TEAM_ID HOW_MANY FIRST_NAME LAST_NAME
2. Adult Showmanship at Halter -18 & Over 783 2 Jane Doe
3. Open Showmanship at Halter 783 4 Jane Doe
9. Adult Western Horsemanship -18 & Over 783 6 Jane Doe
17. Adult Western Pleasure -18 & Over 783 5 Jane Doe
18. Jr. Horse Western Pleasure (Horses 5 and under) 783 6 Jane Doe
20. Open Western Pleasure 783 6 Jane Doe
23. Open Trail Class 783 6 Jane Doe

This is how you can get Access to display how many exhibitors were in the same class as a particular exhibitor. The idea is, I've referred to four tables here: EVENTS, PLACEMENTS, EXHIBITORS and RIDER_HORSE_TEAMS. The EVENTS table is the bookmark, you might say, to which the number-in-a-class are counted.

SELECT EVENTS.CLASS, PLACEMENTS.TEAM_ID, (SELECT Count([PLACEMENTS].[TEAM_ID]) FROM PLACEMENTS WHERE [EVENTS].[EVENT_ID]= [PLACEMENTS].[EVENT_ID]) AS HOW_MANY, EXHIBITORS.FIRST_NAME, EXHIBITORS.LAST_NAME
FROM EXHIBITORS INNER JOIN (RIDER_HORSE_TEAMS INNER JOIN (EVENTS INNER JOIN PLACEMENTS ON EVENTS.EVENT_ID = PLACEMENTS.EVENT_ID) ON RIDER_HORSE_TEAMS.TEAM_ID = PLACEMENTS.TEAM_ID) ON EXHIBITORS.EXHIBITOR_ID = RIDER_HORSE_TEAMS.EXHIBITOR_ID
WHERE (((PLACEMENTS.TEAM_ID)=707) OR (PLACEMENTS.TEAM_ID)=708);

And another...

As developed in ACCESS:

SELECT DISTINCT TOP 6 SHOWPLACEMENTS04.HORSEENTRYNUMBER, SHOWPLACEMENTS04.CLASSIDNUMBER, (Avg((SHOWPLACEMENTS04.JUDGEPLACE))) AS OVERALL_PLACEMENT
FROM SHOWPLACEMENTS04
WHERE (((SHOWPLACEMENTS04.CLASSIDNUMBER)=14) AND ((SHOWPLACEMENTS04.JUDGEPLACE)<>0))
GROUP BY SHOWPLACEMENTS04.HORSEENTRYNUMBER, SHOWPLACEMENTS04.CLASSIDNUMBER
HAVING (((SHOWPLACEMENTS04.HORSEENTRYNUMBER)=[HORSEENTRYNUMBER]))
ORDER BY (Avg((SHOWPLACEMENTS04.JUDGEPLACE)));

and it went into my script like this:

sSql="SELECT DISTINCT TOP 6 SHOWPLACEMENTS.HORSEENTRYNUMBER, SHOWPLACEMENTS.CLASSIDNUMBER, AVG(SHOWPLACEMENTS.JUDGEPLACE) AS OVERALL_PLACEMENT FROM SHOWPLACEMENTS"


' Load Default Filter
sDefaultFilter = "[CLASSIDNUMBER]=" & request.querystring("classid")&" AND JUDGEPLACE<>0"
sGroupBy = "SHOWPLACEMENTS.HORSEENTRYNUMBER,SHOWPLACEMENTS.CLASSIDNUMBER"
sHaving = "(((SHOWPLACEMENTS.HORSEENTRYNUMBER)=[HORSEENTRYNUMBER]))"

' Load Default Order
sDefaultOrderBy = "AVG(SHOWPLACEMENTS.JUDGEPLACE) ASC"

 

ASP Examples Menu of Active Server Pages scripts code Classic ASP

Classic ASP Active Server Pages Examples, code, tutorials, scriptsASP Examples Menu (below*)



Active Server Pages | ASP | Scripts | Tutorials | Code | Web Programming | Examples
Active Server Pages ASP code examples, tutorials, and snippets for use in Programming interactive websites.

NOTE: Recently changed hosting and url so some demos aren't working now because I haven't updated pathing; if you find omissions or errors let me know.
 

*

Multiple records per row with paging

Display multiple records per row

Get ID of newly inserted record, Classic ASP, Access Database

Javascript timer with ASP to keep track of time expired until login expires / timeout session is up

Multiple Javascript event functions triggered from single form onsubmit

Get Querystring key names along with querystring variables

ASP Youtube Downloader  Version FOUR
All ASP code. Allows user client side downloads of Youtube videos. You have the option to allow files to be saved directly to your site also.

ASP/PHP video downloader (Works better than ASP for large files)

Use ASP to dynamically parse a Youtube XML playlist to render to html.

Zip up an archive file of your site for backup if you need to restore website with ASP and free Zip Component.

Zip all files in a folder into an archive or backup file dynamically by specific file extension.

Sanitize or clean price cost field for database insertion or display.

Javascript image dimensions and image file size preview.

Put recordset into array, assign keys to the records then use record associated with specific array key as a variable elsewhere.

Access Database Interface Generator Wizard to read your table and field names and to create a table display.

Combining dropdown selection box menu with dynamic ASP database page.

How to redirect user to original page they tried to view before logging, after they are logged in.

Previous/Current/Next Records, or Buddy Ring Script

Custom ASP 404 page not found error script sends visitors to virtual folders

ASP Example: Combine ASP with Javascript for popup windows
dynamically sized to image dimensions.

ASP Example: How to use ASPJPEG to create thumbnails
Click for free Highly recommended thumbnailer for which you don't need Server access

ASPjpeg: How to re-scale aspect ratio of thumbnails by height instead of width.

ASP Example: What if your host has an older version of ASPJPEG that doesn't support gifs?

Using inline frames as "dynamic includes" (offline for updating)

Select Count Distinct Records for Access Database

Upload Image to folder and insert filename into database

Get date in such a way that it can be made part of a file name

Isolate a file name away from its path or extension or strip a path from a file name or strip characters from database field

User-customized on-the-fly stylesheet css files

Enhanced Page Hits Counter (also counts downloads and menu link hits)

Loop through array and compare to string with select case

Aphabetical Paging for your ASP scripts

Web Wiz Guide's Site Searcher Script modified as a file content indexer

Let Users Select How Many Records to View

Access Database for your PayPal IPN scripts

Miscellaneous collected code

You don't have to be a genius, just persistent, to write original Active Server Pages scripts

Software Recommendation: bare_bones_no_bells_and_whistles_asp_code_generator_database_interface_maker

--Lil at gmail.com