C
Chris A
I have created a Sub that randomly chooses a loan record
ID from a table successfully, but I need it to check the
table it is the random Id to make sure the ID is not a
duplicate. If it finds a duplicate I need it to not
increment the K and look for another ID that is not a
duplicate. I also need to somehow create a message box
when the random function does not find 5 ID's at random
that says it only found 4, or 3 due to there were not
enough records to create 5 at random.
Below is the code
-Chris
Sub RandomList4()
Dim D As Database, R As Recordset, T As Recordset
Dim Top, Bottom, K
Dim strRecordID As String
Set D = CurrentDb
Set R = D.OpenRecordset("Select * from [tblMake] Where
[Application_User_Name]='" & strcboValue & "';",
dbOpenSnapshot)
Set T = D.OpenRecordset("tblLoans")
DoCmd.SetWarnings False
DoCmd.OpenQuery ("qryDeleteTblLoans")
DoCmd.SetWarnings True
R.MoveLast: R.MoveFirst
Top = 0
Bottom = R.RecordCount - 1
For K = 1 To 5
R.MoveFirst
R.Move Int((Top - Bottom + 1) * Rnd + Bottom)
Randomize
strRecordID = R![Loan_Record_ID]
--> If strRecordID = T![Loan_Record_ID] Then
--> K -1
--> Next
--> Else
T.AddNew
T![Loan_Record_ID] = strRecordID
T.Update
Next
End Sub
ID from a table successfully, but I need it to check the
table it is the random Id to make sure the ID is not a
duplicate. If it finds a duplicate I need it to not
increment the K and look for another ID that is not a
duplicate. I also need to somehow create a message box
when the random function does not find 5 ID's at random
that says it only found 4, or 3 due to there were not
enough records to create 5 at random.
Below is the code
-Chris
Sub RandomList4()
Dim D As Database, R As Recordset, T As Recordset
Dim Top, Bottom, K
Dim strRecordID As String
Set D = CurrentDb
Set R = D.OpenRecordset("Select * from [tblMake] Where
[Application_User_Name]='" & strcboValue & "';",
dbOpenSnapshot)
Set T = D.OpenRecordset("tblLoans")
DoCmd.SetWarnings False
DoCmd.OpenQuery ("qryDeleteTblLoans")
DoCmd.SetWarnings True
R.MoveLast: R.MoveFirst
Top = 0
Bottom = R.RecordCount - 1
For K = 1 To 5
R.MoveFirst
R.Move Int((Top - Bottom + 1) * Rnd + Bottom)
Randomize
strRecordID = R![Loan_Record_ID]
--> If strRecordID = T![Loan_Record_ID] Then
--> K -1
--> Next
--> Else
T.AddNew
T![Loan_Record_ID] = strRecordID
T.Update
Next
End Sub