View Full Version : Sql query via a form button

12-30-2005, 05:04 PM

I have an area of web page that I do a query to display some
information. It is basically a Select statement to display everything in the
table, which loops around till everything is displayed. That works fine and
I want to create three buttons to display information for "Past 24 Hours",
"Past 72 Hours", and "All Entry's"

I am not sure the best way to do this, any ideas?

OH, the select statement for 24 hours, 72, or all entries I have but the
part I am not sure is how to put the code into the buttons.

thanks in advance,

12-31-2005, 02:47 PM
You can use If/Else or Select Case to select the values

If Len(Request.Form) > 0 Then

iChoice = Request.Form("Submit")

sql = "SELECT * FROM table"
r_start_date = FormatDateTime(Now,2)

Select Case iChoice
Case "24"
r_end_date = FormatDateTime(Now-1,2)
sql = sql & " WHERE creation_time BETWEEN #" & r_start_date & "# AND #" & r_end_date & "#"
Case "72"
r_end_date = FormatDateTime(Now-3,2)
sql = sql & " WHERE creation_time BETWEEN #" & r_start_date & "# AND #" & r_end_date & "#"
Case Else
sql = sql
End Select
End If
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<form name="form1" id="form1" method="post" action="">
<input type="submit" name="Submit" value="24" />
<input type="submit" name="Submit" value="72" />
<input type="submit" name="Submit" value="All" />
If sql <> "" Then
Response.Write sql
End If

12-31-2005, 03:09 PM
if you use a submit button (input type="submit") it passes a value, so all you need to do is request.form the name of the button and grab that value.


<INPUT type="submit" value="Past 24 Hours" name="when">
<INPUT type="submit" value="Past 72 Hours" name="when">
<INPUT type="submit" value="All Entrys" name="when">

dim someVar, whereClause
someVar = Request.Form("when")
If InStr(1,someVar,"24") Then
whereClause = " your 24 hour WHERE statement is defined here"
ElseIf InStr(1,someVar,"72") Then
whereClause = " your 72 hour WHERE statement is defined here"
whereClause = " your all WHERE statement is defined here"
End If

01-04-2006, 09:27 PM
Hi Degsy & Miranda,

I got it to work using the link the Degsy provded. :)

Thanks again,

Below is snippet of what I did. I am working on a intranet so I am retyping what I did.


A HREF="default.asp?choice=1">Past 24 Hours</A>&nbsp;
<A HREF="default.asp?choice=2">Past 72 Hours</A>&nbsp;
<A HREF="default.asp?choice=3">All items</A>
' Declare our variable
Dim iChoice

' Read in the choice the user clicked on.
' This will equal "" (an empty string) if no choice is there
iChoice = Request.QueryString("choice")

' Execute the appropriate branch based upon the value of the variable we just read in.
Select Case iChoice
Case "1"
Select * From Table Where ([Date] BETWEEN DateAdd('h',-24,Now()) AND Now())"
Case "2"
Select * From Table Where ([Date] BETWEEN DateAdd('h',-72,Now()) AND Now())" <%
Case "3"
"Select * From Table "
Case Else
' This executes if no of the other conditions are met!
' This actually runs the first time through when the user
' arrives at this page because the choice is blank.

"Select * From Table "

End Select

01-06-2006, 09:38 PM
And for a related link for if you want everything to run off of one page:

Classic ASP Design Tips - Post Back Page

01-07-2006, 04:13 AM
Hi Bullschmidt,

Actually the code that I ended up using that Degsy provided did run on one page.

The link to your site also provided some nice suggestions. :)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum