I have a string of code to create a new table and to populate it with four different randomly generated upper case letters (A-Z), using the Rnd function and then compare the result generated to previously generated letters within the same record. If a duplicate is detected a new letter is then generated and the checking process repeated.
I found that the more records I tried to add to the table the higher the likelihood that the fourth generated letter had already been generated in the first three letter fields (not surprisingly), but unlike the second and third letter checking process (using identical code) the Rnd function would continue to generate the same letter, over and over again and result in an infinite loop causing Access to crash!
I tried resolving the problem by first setting the variable (l4) initially to Null, then to 0 (zero), and adding the Randomize function prior to using the Rnd function, but still Rnd produced the exact same value. I also put in lots of messages to pinpoint exactly where the error was occurring. If anyone can suggest the source of the problem and a solution I may not have a nervous breakdown after all!! The function Code is below...
Thanks, SiciliandoBlue.
Function CreateWorkRateTest()
Dim myDB As DAO.Database
Dim rst As DAO.Recordset
Dim bytCodeCount As Byte
Dim i As Variant, j As Variant, k As Variant, l1 As Variant, l2 As Variant, l3 As Variant, l4 As Variant
Dim strSQL As String, strPrompt As String, strTitle As String
Set myDB = CurrentDb()
myDB.Execute "DROP TABLE CodeTable;"
myDB.Execute "CREATE TABLE CodeTable (CodeID BYTE, Letter1 TEXT(1), Number1 TEXT(1), Shape1 TEXT(1), " & _
"Letter2 TEXT(1), Number2 TEXT(1), Shape2 TEXT(1));"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Letter3 TEXT(1);"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Number3 TEXT(1);"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Shape3 TEXT(1);"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Letter4 TEXT(1);"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Number4 TEXT(1);"
myDB.Execute "ALTER TABLE CodeTable ADD COLUMN Shape4 TEXT(1);"
strSQL = "SELECT * FROM CodeTable;"
Set rst = myDB.OpenRecordset(strSQL, dbOpenDynaset)
On Error GoTo ErrorCode
strTitle = "Number of Codes"
strPrompt = "Enter the number of Codes to be generated"
bytCodeCount = InputBox(strPrompt, strTitle, 20) 'Prompt for the number of Codes to be generated (20)
For i = 1 To bytCodeCount
rst.AddNew
rst("CodeID") = i
Randomize
k = Int((26 * Rnd) + 1) ' Generate random value between 1 and 26.
l1 = k + 64
'MsgBox "l1 = " & l1
k = Int((26 * Rnd) + 1) ' Generate random value between 1 and 26.
l2 = k + 64
Do Until l2 <> l1 ' loop if a duplicate letter is generated
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l2 = k + 64
Loop
'MsgBox "l2 = " & l2
k = Int((26 * Rnd) + 1) ' Generate random value between 1 and 26.
l3 = k + 64
Loop1:
If l3 = l2 Then ' loop if a duplicate letter is generated
MsgBox "l3 = l2 (" & l3 & ")"
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l3 = k + 64
MsgBox "New l3 value = " & l3
GoTo Loop1
End If
If l3 = l1 Then ' loop if a duplicate letter is generated
MsgBox "l3 = l1 (" & l3 & ")"
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l3 = k + 64
MsgBox "New l3 value = " & l3
GoTo Loop1
End If
'MsgBox "l3 = " & l3
k = Int((26 * Rnd) + 1) ' Generate random value between 1 and 26.
l4 = k + 64
Loop2:
If l4 = l3 Then ' loop if a duplicate letter is generated
MsgBox "l4 = l3 (" & l4 & ")"
k = 0
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l3 = k + 64
MsgBox "New l4 value = " & l4
GoTo Loop2
End If
If l4 = l2 Then ' loop if a duplicate letter is generated
MsgBox "l4 = l2 (" & l4 & ")"
k = 0
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l3 = k + 64
MsgBox "New l4 value = " & l4
GoTo Loop2
End If
If l4 = l1 Then ' loop if a duplicate letter is generated
MsgBox "l4 = l1 (" & l4 & ")"
k = 0
Randomize
k = Int((26 * Rnd) + 1) ' Generate new random value between 1 and 26.
l3 = k + 64
MsgBox "New l4 value = " & l4
GoTo Loop2
End If
'MsgBox "l4 = " & l4
MsgBox "l1 = " & l1 & Chr(13) & _
"l2 = " & l2 & Chr(13) & _
"l3 = " & l3 & Chr(13) & _
"l4 = " & l4
rst("Letter1") = Chr(l1) 'Generates a letter between A and Z
rst("Letter2") = Chr(l2) 'Generates a letter between A and Z
rst("Letter3") = Chr(l3) 'Generates a letter between A and Z
rst("Letter4") = Chr(l4) 'Generates a letter between A and Z
rst.Update
Next i
i = 0
Set rst = Nothing
Set myDB = Nothing
DoCmd.OpenTable "CodeTable"
Exit Function
ErrorCode:
MsgBox Err.Description
End Function