Dim rs1 As DAO.RecordsetPrivate Sub Command生成数字_Click(),今天小编就来说说关于access中vba程序编写?下面更多详细答案一起来看看吧!
access中vba程序编写
Dim rs1 As DAO.Recordset
Private Sub Command生成数字_Click()
Dim minint As Single
Dim maxint As Single
If Me.最小数字 <> "" And Me.最大数字 <> "" Then
minint = Me.最小数字
maxint = Me.最大数字
If minint >= maxint Then
MsgBox "最小数字要小于最大数字"
Exit Sub
End If
Else
MsgBox "请输入最大最小数字"
Exit Sub
End If
If Me.生成数量 < 1 Or Me.生成数量 = "" Then
MsgBox "生成数量必须大于0"
End If
Dim rndcount As Long
rndcount = Me.生成数量
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 生成表"
DoCmd.RunSQL del_sql
Dim add_rs As DAO.Recordset
Set add_rs = CurrentDb.OpenRecordset("生成表", dbOpenTable)
With add_rs
Dim i
For i = 1 To rndcount
.AddNew
!生成结果.Value = 生成随机数字(minint, maxint)
.Update
Next i
.Close
End With
Set add_rs = Nothing
MsgBox "生成完成"
End Sub
Function 生成随机数字(ByVal minnum As Single, ByVal maxnum As Single) As Single
Randomize
生成随机数字 = Round((maxnum - minnum 1) * Rnd minnum, 2)
End Function
Private Sub Command生成项目_Click()
Dim itemcount As Long
itemcount = Nz(DCount("项目", "随机项目"), 0)
If itemcount < 2 Then
MsgBox "随机项目必须大于1"
Exit Sub
End If
If Me.生成数量 < 1 Or Me.生成数量 = "" Then
MsgBox "生成数量必须大于0"
End If
Dim rndcount As Long
rndcount = Me.生成数量
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 生成表"
DoCmd.RunSQL del_sql
Dim add_rs As DAO.Recordset
Set add_rs = CurrentDb.OpenRecordset("生成表", dbOpenTable)
With add_rs
Dim i
For i = 1 To rndcount
.AddNew
!生成结果.Value = 获取随机项目(itemcount)
.Update
Next i
.Close
End With
Set add_rs = Nothing
MsgBox "生成完成"
End Sub
Function 获取随机项目(ByVal maxnum As Long) As String
Dim Record_count As Long
Record_count = maxnum
Dim rnd_i As Long
Randomize
rnd_i = Int((Record_count - 1 1) * Rnd 1)
rs1.MoveFirst
rs1.Move (rnd_i - 1)
获取随机项目 = rs1.Fields("项目").Value
End Function
Function 生成随机整数(ByVal minnum As Long, ByVal maxnum As Long) As Long
Randomize
生成随机整数 = Int((maxnum - minnum 1) * Rnd minnum)
End Function
Private Sub Command生成整数_Click()
Dim minint As Long
Dim maxint As Long
If Me.最小整数 <> "" And Me.最大整数 <> "" Then
minint = Me.最小整数
maxint = Me.最大整数
If minint >= maxint Then
MsgBox "最小整数要小于最大整数"
Exit Sub
End If
Else
MsgBox "请输入最大最小整数"
Exit Sub
End If
If Me.生成数量 < 1 Or Me.生成数量 = "" Then
MsgBox "生成数量必须大于0"
End If
Dim rndcount As Long
rndcount = Me.生成数量
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 生成表"
DoCmd.RunSQL del_sql
Dim add_rs As DAO.Recordset
Set add_rs = CurrentDb.OpenRecordset("生成表", dbOpenTable)
With add_rs
Dim i
For i = 1 To rndcount
.AddNew
!生成结果.Value = 生成随机整数(minint, maxint)
.Update
Next i
.Close
End With
Set add_rs = Nothing
MsgBox "生成完成"
End Sub
Private Sub Form_Load()
Set rs1 = CurrentDb.OpenRecordset("随机项目", dbOpenTable)
End Sub
,