
Function Call Itself?
Is it safe for a function (that returns an threedigit integer value) to call itself? What I'm attempting to do is to randomly generate a three digit integer and then check to make sure the number it created isn't already in a table. If it is, then it needs to repeat the cycle until the number is unique. An autonumber field isn't an option.<BR><BR>Thanks,<BR><BR>Defiant005

Could, but slow...
Recursive functions are slow. Use a FOR loop. Much faster. <BR><BR>You say you want a 3 digit number, so that's easy:<BR><BR><%<BR>Dim numbers(1000) ' a little overkill, but easier this way<BR><BR>RANDOMIZE<BR><BR>Function ThreeDigit( )<BR> Do While True ' loop forever!<BR> num = 100 + INT( 900 * RND ) ' gets a number from 100 to 999<BR> If numbers(num) <> num Then ' this number not chosen yet<BR> numbers(num) = num ' set the flag saying it is now chosen<BR> ThreeDigit = num ' this is the right answer<BR> Exit Function ' and quit immediately<BR> End If<BR> ' if we get here, then the number was already in the table...<BR> ' and so it was already chosen...<BR> ' so just loop to choose another!<BR> Loop<BR>End Function<BR>%><BR><BR>Okay?<BR>

Not Quite, But Whatever I Did...
I don't quite understand. I was actually trying to do it with a loop, but I couldn't get it to work. Below is the function, in its entirety, I'm using to try to make this work.<BR><BR>Function CreateMemberID()<BR> <BR> Dim intRandomInteger, intCounter, intMemberID, sqlCheckMemberID, rsCheckMemberID<BR> <BR> Randomize<BR> <BR> For intCounter = 1 To 3<BR> <BR> intRandomInteger = Int((9  0 + 1) * Rnd + 0)<BR> <BR> intMemberID = intMemberID & intRandomInteger<BR> <BR> Next<BR> <BR> Call OpenDatabaseConnection("mydatabase.mdb","mypasswor d")<BR> <BR> sqlCheckMemberID = "SELECT MemberID FROM Roster WHERE MemberID = " & intMemberID<BR> Set rsCheckMemberID = oConn.Execute(sqlCheckMemberID)<BR> <BR> If Not rsCheckMemberID.eof Then<BR> <BR> Do While Not rsCheckMemberID.eof<BR> <BR> For intCounter = 1 To 3<BR> <BR> intRandomInteger = Int((9  0 + 1) * Rnd + 0)<BR> <BR> intMemberID = intMemberID & intRandomInteger<BR> <BR> Next<BR> <BR> sqlCheckMemberID="SELECT MemberID FROM Roster WHERE MemberID = " & intMemberID<BR> <BR> Set rsCheckMemberID = oConn.Execute(sqlCheckMemberID)<BR> <BR> Loop<BR> <BR> rsCheckMemberID.Close<BR> Set rsCheckMemberID = Nothing<BR> <BR> Call CloseDatabaseConnection()<BR> <BR> CreateMemberID = intMemberID<BR> <BR> Else<BR> <BR> rsCheckMemberID.Close<BR> Set rsCheckMemberID = Nothing<BR> <BR> Call CloseDatabaseConnection()<BR> <BR> CreateMemberID = intMemberID<BR> <BR> End If<BR> <BR>End Function<BR><BR><BR><BR>After playing with it WHILE I was posting this, I figured it out after all. The function SEEMS to work fine (I've changed the above code to reflect the changes; look it over if you could and see if any red flags (or even yellow ones) jump out at you). It was my debugging code that was creating problems. Go figure...<BR><BR>But not all was lost: I found several typos (thanks to Option Explicit), I learned something new yet again from you, and the function seems to be doing what it's supposed to.<BR><BR>Thanks Bill!<BR><BR>Defiant005

Well, you could write it a lot shorter...
Randomize ' SHOULD ONLY BE USED ONCE PER PAGE!<BR><BR>Function CreateMemberID()<BR><BR> ' if you do anything at all with this DB outside this<BR> ' function, then this *REALLY* ought to be called outside<BR> ' this function...at page global scope, presumably<BR> ' please don't open and close a connection for each db query/usage!<BR> Call OpenDatabaseConnection("mydatabase.mdb","mypasswor d")<BR><BR> Dim intCounter, intMemberID, sqlCheckMemberID, rsCheckMemberID<BR><BR> Do While True ' loop forever<BR> intMemberID = 0<BR> For intCounter = 1 To 3<BR> ' shame on you...your intMemberID was a *string*!<BR> ' so it should have been strMemberID<BR> intMemberID = intMemberID * 10 + Int(10 * RND() )<BR> Next<BR><BR> sqlCheckMemberID = "SELECT MemberID FROM Roster WHERE MemberID = " & intMemberID<BR> Set rsCheckMemberID = oConn.Execute(sqlCheckMemberID)<BR><BR> If rsCheckMemberID.eof Then ' if nobody has that number yet<BR> CreateMemberID = intMemberID ' then assign it now<BR> Exit Function ' and we are done<BR> End If<BR> Loop<BR><BR>End Function<BR>

I should point out...
...that this is all bogus.<BR><BR>You execute that code to get a random member id.<BR><BR>And two users (or more) hit your site at the same time.<BR><BR>And by random chance they both manage to get through all that code and they both decide that number 732 is not in use.<BR><BR>So then they both add themselves to the DB using 732.<BR><BR>Can you say "OOPS"?<BR><BR>

RE: I should point out...
This is true, but the probability of that happening out of 1,000 numbers, AND the chances of the database being added to within two seconds of each other (especially for the small size of my organization) would make such a happening less likely than being struck by lightening or winning the lottery. (Or getting a raise??)<BR><BR>I'll try to clean up my code a bit. Thanks Bill!
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

