Groups | Blog | Home
all groups > inetserver asp db > february 2005 >

inetserver asp db : Generate random query with all fields from an entry


andrewdreib
2/23/2005 9:43:02 AM
I'm trying to create an ASP page that will
randomly query the database and show entire entires. I found some code
online that prints on entry from the table, but only one piece of information
from it, and I'm hoping someone here know what I need to change to make it
print the entire entry.

Here's what I have:

=============================================================

<%
Option Explicit
Response.Buffer = True
%>
<html>
<head>
<style>
p { font-family:verdana; font-size:11px; }
</style>
</head>
<body>
<br><p align="center">
<%
' ADO Constant. Dont change this
Const adCmdText = &H0001

' Connection string and SQL statement
Dim query, connStr
query = "select ID from numbers"
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Inetpub\wwwroot\numbers.mdb"

' Opening database
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open query, connStr, 3, , adCmdText

' Generating random number from total number of records
Dim intRnd
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random statement
Response.Write "<b>" & rs("statement") & "</b>"

' Closing the database
rs.Close
Set rs = Nothing
%>
</p>
</body>
</html>

=============================================================

The fields in my table are ID, and F1-F10. I'll try to figure this out
myself, but help is definately appreciated.



Bullschmidt
3/2/2005 5:19:10 PM
<<
I'm trying to create an ASP page that will
randomly query the database and show entire entires. I found some code
online that prints on entry from the table, but only one piece of
information
from it, and I'm hoping someone here know what I need to change to make
it
print the entire entry.

Here's what I have:

=============================================================

<%
Option Explicit
Response.Buffer = True
%>
<html>
<head>
<style>
p { font-family:verdana; font-size:11px; }
</style>
</head>
<body>
<br><p align="center">
<%
' ADO Constant. Dont change this
Const adCmdText = &H0001

' Connection string and SQL statement
Dim query, connStr
query = "select ID from numbers"
connStr = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Inetpub\wwwroot\numbers.mdb"

' Opening database
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open query, connStr, 3, , adCmdText

' Generating random number from total number of records
Dim intRnd
Randomize Timer
intRnd = (Int(RND * rs.RecordCount))

' Now moving the cursor to random record number
rs.Move intRnd

' Showing the random statement
Response.Write "<b>" & rs("statement") & "</b>"

' Closing the database
rs.Close
Set rs = Nothing
%>
</p>
</body>
</html>

=============================================================

The fields in my table are ID, and F1-F10. I'll try to figure this out
myself, but help is definately appreciated.
[quoted text, click to view]

Perhaps change this:
query = "select ID from numbers"

To be this instead:
query = "select * from numbers"

And then for this:
Response.Write "<b>" & rs("statement") & "</b>"

You can then perhaps show all the fields you want:
Response.Write "Last Name: <b>" & rs("LName") & "</b><br>"
Response.Write "First Name: <b>" & rs("FName") & "</b><br>"
Response.Write "City: <b>" & rs("City") & "</b><br>"

Best regards,
J. Paul Schmidt, Freelance ASP Web Developer
http://www.Bullschmidt.com
ASP Design Tips, ASP Web Database Demo, Free ASP Bar Chart Tool...


*** Sent via Developersdex http://www.developersdex.com ***
AddThis Social Bookmark Button