crosmill
Registered User.
- Local time
- Today, 05:58
- Joined
- Sep 20, 2001
- Messages
- 285
Hello all, it's been a while since I was last here, I've been doing alot of ASP recently.
Anyway my problem...
I'm creating a program that the will randomly select multiple records, I'm at a stage where I want to randomly create x number of winners.
Here's the code:
Private Sub Command7_Click()
Dim dbs As Database, rst As Recordset, RanValue, NoOfRecords, noLoop, nolooped, AccNoOfRecords, RanNum, ln As Integer
Dim sqlStr As String
Dim RecordArray As Variant
Set dbs = CurrentDb
'Creates Record ID array
sqlStr = "SELECT * FROM WinnerSelector WHERE [WinnerType] = Null"
Set rst = dbs.OpenRecordset(sqlStr)
While Not rst.EOF
NoOfRecords = NoOfRecords & rst("ID") & ","
rst.MoveNext
Wend
'Removes Trailing Comma
ln = Len(NoOfRecords)
NoOfRecords = Left(NoOfRecords, (ln - 1))
RecordArray = Array(NoOfRecords)
AccNoOfRecords = rst.RecordCount
Set dbs = Nothing
'Selects a Random record excluding those already selected
'and updates Winner Type to that specified
Set dbs = CurrentDb
'Loop no of times requested
noLoop = Me.NoOfPrizes
nolooped = 0
While nolooped <> noLoop
Randomize
RanNum = (Int(AccNoOfRecords * Rnd) + 1)
RanValue = RecordArray(RanNum)
sqlStr = "SELECT * FROM WinnerSelector WHERE [ID] = " & RanValue & " AND [WinnerType] = Null"
Set rst = dbs.OpenRecordset(sqlStr)
rst.Edit
rst("WinnerType") = Me.PrizeType
rst.Update
nolooped = nolooped + 1
Wend
Set dbs = Nothing
End Sub
OK, I haven't worked with Array's before but I'm muddleing through.
The error I get is from this line:
RanValue = RecordArray(RanNum)
Where the record Array is telling me <Subcript out of Range>
I've checked the values and RanNum is between 0 and total number of values in the array.
I don't really know whats wrong.
Cheers everyone.
c
Anyway my problem...
I'm creating a program that the will randomly select multiple records, I'm at a stage where I want to randomly create x number of winners.
Here's the code:
Private Sub Command7_Click()
Dim dbs As Database, rst As Recordset, RanValue, NoOfRecords, noLoop, nolooped, AccNoOfRecords, RanNum, ln As Integer
Dim sqlStr As String
Dim RecordArray As Variant
Set dbs = CurrentDb
'Creates Record ID array
sqlStr = "SELECT * FROM WinnerSelector WHERE [WinnerType] = Null"
Set rst = dbs.OpenRecordset(sqlStr)
While Not rst.EOF
NoOfRecords = NoOfRecords & rst("ID") & ","
rst.MoveNext
Wend
'Removes Trailing Comma
ln = Len(NoOfRecords)
NoOfRecords = Left(NoOfRecords, (ln - 1))
RecordArray = Array(NoOfRecords)
AccNoOfRecords = rst.RecordCount
Set dbs = Nothing
'Selects a Random record excluding those already selected
'and updates Winner Type to that specified
Set dbs = CurrentDb
'Loop no of times requested
noLoop = Me.NoOfPrizes
nolooped = 0
While nolooped <> noLoop
Randomize
RanNum = (Int(AccNoOfRecords * Rnd) + 1)
RanValue = RecordArray(RanNum)
sqlStr = "SELECT * FROM WinnerSelector WHERE [ID] = " & RanValue & " AND [WinnerType] = Null"
Set rst = dbs.OpenRecordset(sqlStr)
rst.Edit
rst("WinnerType") = Me.PrizeType
rst.Update
nolooped = nolooped + 1
Wend
Set dbs = Nothing
End Sub
OK, I haven't worked with Array's before but I'm muddleing through.
The error I get is from this line:
RanValue = RecordArray(RanNum)
Where the record Array is telling me <Subcript out of Range>
I've checked the values and RanNum is between 0 and total number of values in the array.
I don't really know whats wrong.
Cheers everyone.
c