【生成指定范圍隨機整數(shù)】【生成重復(fù)代碼】【生成自定義編號】
生成指定范圍隨機整數(shù)
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.隨機數(shù) = Int(minnum + (maxnum - minnum + 1) * Rnd())
Else
MsgBox "請輸入范圍"
End If
End Sub
生成重復(fù)代碼
Private Sub Command清空_Click()
DoCmd.SetWarnings (False)
Dim del_sql As String
del_sql = "Delete From 項目表"
DoCmd.RunSQL del_sql
Me.數(shù)據(jù)表子窗體.Requery
End Sub
Private Sub Command生成_Click()
If Me.替換變量 <> "" And Me.生成代碼 <> "" Then
Me.生成結(jié)果 = ""
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.生成結(jié)果 = addcodetext
Else
Me.生成結(jié)果 = ""
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("銷售數(shù)據(jù)表", 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.數(shù)據(jù)表子窗體.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("記錄編號", "銷售數(shù)據(jù)表", "記錄編號 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