Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

random number.. always selects first record...

Status
Not open for further replies.

Dre313

Technical User
Jun 4, 2003
219
US
I have a problem..

on my ASP page.. I'm randomly selecting some employees .. I randomly pick out 5 employees to be tested..

the problem is.. When i hit the get employee button.. it picks out 5 employees no problem.. but when i go to view report.. It adds in the first record in the database.. making it a total of 6 employees I see on my report.. which there should be only 5.. I been looking back and forth at this code.. but I don't see it..

could someone take a look .. thanks

<%
STRINGvar = Server.MapPath(&quot;drugscreen.mdb&quot;)
ConnSTRING = &quot;Driver={Microsoft Access Driver (*.mdb)}; DBQ=&quot;&STRINGvar
set rsEMPLOYEES = Server.CreateObject(&quot;ADODB.Recordset&quot;)
rsEMPLOYEES.ActiveConnection = ConnSTRING

Randomize
R=clng(100000*rnd)
rsEMPLOYEES.Source = &quot;SELECT top 5 * FROM tblDupont ORDER BY rnd(-(EmpNumber *&quot; & R & &quot;))&quot;
' This pulls back 5 records, but you can change that dynamically e.g.
' rsEMPLOYEES.Source = &quot;SELECT top &quot;& no_records &&quot; * FROM tblDupont ORDER BY rnd(-(ID *&quot; & R & &quot;))&quot;
' Change ID to the ID field name you use in the table
rsEMPLOYEES.CursorType = 0
rsEMPLOYEES.CursorLocation = 2
rsEMPLOYEES.LockType = 3
rsEMPLOYEES.Open()
counter=0
Do
Response.write &quot;<TABLE><TR>&quot;
Response.write &quot;<TH>Payroll Number</TH><TH>Employee Name</TH><TH>Supervisor</TH><TH>Supervisor Ext</TH>&quot;
Response.write &quot;</TR>&quot;
Do
response.write &quot;<TR><TD>&quot; & (rsEMPLOYEES(&quot;EmpNumber&quot;)) & &quot;</TD>&quot;
response.write &quot;<TD>&quot; & &quot; - &quot; & (rsEMPLOYEES(&quot;EmpName&quot;)) & &quot;</TD>&quot;
response.write &quot;<TD>&quot; & &quot; - &quot; & (rsEMPLOYEES(&quot;Supervisor&quot;)) & &quot;</TD>&quot;
response.write &quot;<TD>&quot; & &quot; - &quot; & (rsEMPLOYEES(&quot;SupervisorExt&quot;)) & &quot;</TD></TR>&quot;
rsEMPLOYEES(&quot;tested&quot;)=Date()
rsEMPLOYEES.update
rsEMPLOYEES.MoveNext
Loop Until rsEMPLOYEES.Eof
Response.write &quot;</TABLE>&quot;

Loop Until rsEMPLOYEES.Eof

rsEMPLOYEES.Close
Set rsEMPLOYEES = nothing

%>
 
Try selecting the top 4 instead of 5 - this might have base 0, so starting at 0 instead of 1.
 
ok now I have here is.. when i randomly select.. its only selects 4 .. and in my report.. it selects 5 with record 1 being selected again .........
 
Uh.... well I'm not very familliar with random stuff, but...

Like every thing in Object Based Languages can be treated as an Array, maybe when you pick randomily 5 numbers you're getting no 1 to 5 but 0 to 5.

Try the same code but with 4 and no 5
(0 to 4 = 5 numbers)

Hope it helps.

-- Webmonkey --
 
I see what you are saying.. but i changed it to 4 i get 4 random picks.. and when i take a look at the report.. i get 5 with the first record getting selected again. .. which i do not want.. i want it to randomly select..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top