【生成指定范围随机整数】【生成重复代码】【生成自定义编号】
生成指定范围随机整数
Private Sub Command生成_Click()
If Me.最小值 <> "" And Me.最大值 <> "" Then
Dim minnum As Long
Dim maxnum As Long
minnum = Me.最小值
maxnum = Me.最大值
If minnum > maxnum Then
MsgBox "最小值不能大于最大值"
End If
Randomize
Me.随机数 = Int(minnum + (maxnum - minnum + 1) * Rnd())
Else
MsgBox "请输入范围"
End If
End Sub
生成重复代码
Private Sub Command清空_Click()
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 项目表"
DoCmd.RunSQL del_sql
Me.数据表子窗体.Requery
End Sub
Private Sub Command生成_Click()
If Me.替换变量 <> "" And Me.生成代码 <> "" Then
Me.生成结果 = ""
Dim search_rs As DAO.Recordset
Dim search_sql As String
search_sql = "Select * From 项目表"
Set search_rs = CurrentDb.OpenRecordset(search_sql, dbOpenDynaset)
Dim addcodetext As String
Dim codeitem As String
Do While search_rs.EOF = False
codeitem = Replace(Me.生成代码, Me.替换变量, search_rs!项目.Value)
addcodetext = addcodetext & vbCrLf & codeitem
search_rs.MoveNext
Loop
search_rs.Close
Set search_rs = Nothing
Me.生成结果 = addcodetext
Else
Me.生成结果 = ""
End If
End Sub
生成自定义编号
Private Sub Command添加_Click()
On Error GoTo 添加失败错误
If 记录编号 = "" Or IsNull(记录编号) = True Then
MsgBox "记录编号值为空!"
Exit Sub
End If
If 类型 = "" Or IsNull(类型) = True Then
MsgBox "类型值为空!"
Exit Sub
End If
If 金额 = "" Or IsNull(金额) = True Then
MsgBox "金额值为空!"
Exit Sub
End If
If 客户 = "" Or IsNull(客户) = True Then
MsgBox "客户值为空!"
Exit Sub
End If
If 日期 = "" Or IsNull(日期) = True Then
MsgBox "日期值为空!"
Exit Sub
End If
Dim add_rs As DAO.Recordset
Set add_rs = CurrentDb.OpenRecordset("销售数据表", dbOpenTable)
With add_rs
.AddNew
!记录编号.Value = 记录编号.Value
!类型.Value = 类型.Value
!金额.Value = 金额.Value
!客户.Value = 客户.Value
!日期.Value = 日期.Value
!备注.Value = 备注.Value
.Update
.Close
End With
Set add_rs = Nothing
Me.数据表子窗体.Requery
Exit Sub
添加失败错误:
MsgBox "添加失败!"
MsgBox Err.Description
End Sub
Function 生成自定义编号(ByVal numdate As Date, ByVal numtype As String) As String
On Error GoTo 错误
Dim search_num As String
If numtype = "销售" Then
search_num = "XS-" & Format(numdate, "YYYYMMDD")
Else
search_num = "TH-" & Format(numdate, "YYYYMMDD")
End If
Dim num_count As Long
num_count = Nz(DCount("记录编号", "销售数据表", "记录编号 like '*" & search_num & "*'"), 0) + 1
生成自定义编号 = search_num & "-" & Format(num_count, "000")
Exit Function
错误:
生成自定义编号 = ""
End Function
Private Sub 记录编号_DblClick(Cancel As Integer)
If 类型 = "" Or IsNull(类型) = True Then
MsgBox "类型值为空!"
Exit Sub
End If
If 日期 = "" Or IsNull(日期) = True Then
MsgBox "日期值为空!"
Exit Sub
End If
Me.记录编号 = 生成自定义编号(Me.日期, Me.类型)
End Sub

