访问VBA随机功能不起作用 [英] Access vba random function not working
问题描述
我有一个子,应该负责一些作业(此数字每天更改),并将其分配给7位同事。这样做有一些条件:
I have a sub that is supposed to take a number of assignments (this number changes daily) and assign them to 7 associates. There are some conditions to this:
如果赋值#小于7,则将所有这些赋值给一个随机关联。
如果#可被7整除,它将为每个数字分配相等的数字。
如果不能被7整除,它会平均分配,然后将其余部分分配给一个随机联营公司。
If the # of assignment is less than 7, it assigns all of them to a random associate. If the # is divisible by 7, it assigns an equal number to each. If it is not divisible by 7, it assigns equally and then gives the remainder to a random associate.
问题是随机部分。我真的不明白vba中随机性的工作原理,或者至少看起来应该很简单,但这不是(也许)。但是我已经写好了,但是没有用。 (Associates(Int(Rnd()* 7)+ 1))
。这是我的相关代码:
The problem is the random part. I really don't understand how random works in vba, or at least it seems like it should be super easy, but it's not (maybe). But I have this written and it's not working. (Associates(Int(Rnd() * 7) + 1))
. Here is my relevant code:
在子区域中,我创建了一个关联数组,并使用一些dcount来获取当天的总分配:
Earlier in the sub I create an array of the associates and I use some dcounts to get the total assignments for that day:
Dim Associates(6) As Integer
Associates(0) = 4687 'Anita
Associates(1) = 4247 'Alberto
Associates(2) = 2167 'Jeff
Associates(3) = 4334 'Lisa
Associates(4) = 4441 'Carrie
Associates(5) = 2052 'Bobby
Associates(6) = 4657 'Simona
'
Dim Person As Variant
'
TotalPop = DCount("LNo", "qry_PT_Assign")
FractionPop = Int(TotalPop / 7)
LeftPop = TotalPop - (FractionPop * 7)
'
,然后我尝试实际分配它们。
and then I try to actually assign them.
'Assign to Associates
If TotalPop < 7 Then
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
ElseIf LeftPop = 0 Then
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
Else
For Each Person In Associates
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & Person & " WHERE LNo IN (SELECT TOP " & FractionPop & " LNo FROM tbl_Assignments WHERE AudTellerID Is Null)"
Next
DoCmd.RunSQL "UPDATE tbl_Assignments SET AudTellerID = " & (Associates(Int(Rnd() * 7) + 1)) & " WHERE AudTellerID IS NULL"
End If
推荐答案
根据我的评论,尝试首先生成随机数,将其分配给变量,然后将变量传递给 Associates()
。
As per my comment, try generating the random number first, assign it to a variable, and then pass the variable into Associates()
.
Dim rndInt as Integer
rndInt = Int(Rnd() * 7) + 1
Associates(rndInt)
然后,如@Chips所说,您可以使用
Then as @Chips said, you can use
Debug.print rndInt
或
Msgbox rndInt
检查其值
这样,您将能够看到实际生成的数字
That way you'll be able to see what number is actually being generated
这篇关于访问VBA随机功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!