This topic is locked

Randomize and loop before add

8/24/2012 9:53:28 PM
ASPRunnerPro General questions
lefty author

Using 7.1 Asprunner Pro.
Haven't used loop statements too often with existing records. Does anybody have a suggestion? My code below

Trying to loop back to exists statement if number already exists and auto add non-existing number in BEFORE ADD EVENT__. As I was using commented code try again duplicate number.!
Randomize

values("fldrandom") = (Int( (8 - 0) * Rnd + 1 ))
'** Check if specific record exists ****

strSQLExists = "select * from Main where fldrandom=" & values("fldrandom") & ""
set rsExists = CreateObject("ADODB.Recordset")

rsExists.Open strSQLExists, dbConnection
if not rsExists.eof then

' if record exists do something
BeforeAdd = false

'Response.write "Duplicate Random Number Try Again"
else
' if dont exist do something else

BeforeAdd = True

end if

rsExists.Close : set rsExists = Nothing

Sergey Kornilov admin 8/28/2012

Try something like this:

Randomize

bDone=false

Do

values("fldrandom") = (Int( (8 - 0) * Rnd + 1 ))

strSQLExists = "select * from Main where fldrandom=" & values("fldrandom") & ""
set rsExists = CreateObject("ADODB.Recordset")

rsExists.Open strSQLExists, dbConnection
if not rsExists.eof then

bDone=true

end if

rsExists.Close : set rsExists = Nothing
Loop While not bDone
BeforeAdd = True
lefty author 9/1/2012



Try something like this:

Randomize

bDone=false

Do

values("fldrandom") = (Int( (8 - 0) * Rnd + 1 ))

strSQLExists = "select * from Main where fldrandom=" & values("fldrandom") & ""
set rsExists = CreateObject("ADODB.Recordset")

rsExists.Open strSQLExists, dbConnection
if not rsExists.eof then

bDone=true

end if

rsExists.Close : set rsExists = Nothing
Loop While not bDone
BeforeAdd = True



Thanks for the reply!

Code Worked but duplicate numbers are showing . With the Randomize statement above I should only get unique nummbers between 1 and 8.

My old code worked but had to stop and write: Try Again. because a non-unique number came up. Now the code above here byAdmin** <img src='https://asprunner.com/forums/file.php?topicimage=1&fieldname=reply&id=67611&image=1&table=forumreplies' class='bbcemoticon' alt=':)' /> also works but once again the numbers area not unique. Seems when it loops it still uses the value left by the generator statement(Randomize) should keep them unique. Sometimes The first time 7 the second time 7 the third time 7. and so on. which are all duplicate numbers. My reocrds have to be random and output like this A 1 A 8 A 4 A 3 A 2 A 5 A 7 A 6
I think maybe the variables are keeping the same number when it loop. therefore not ouput for example 1 2 3 4 5 6 7 8

Sergey Kornilov admin 9/1/2012

Probably there is some sort logic flaw here.
If you have a valid support contract post your application to Demo Account and open a ticket at http://support.xlinesoft.com sending your Demo Account URL. 'Demo Account' button can be found on the last screen in the program.