我在下面有這段代碼,它限制用戶在表單中留下一個空欄位。現在我想在我的所有表單中使用它。我嘗試使用模塊在公共子程式中使用。但它不起作用。
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In Me.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
Cancel = True
Exit For
End If
End If
Next
End Sub
我只想在我的所有表格中使用它。我該如何做到這一點?
uj5u.com熱心網友回復:
好問題,好主意。
因此,請記住,“我”只是您正在使用的當前形式。
所以,創建平面簡標準代碼模塊。并像這樣通過“一些”更改來添加您的功能。
Public Function CheckRequired(MyMe As Form) As Boolean
Dim msg As String
Dim Style As Integer
Dim Title As String
Dim nl As String
Dim ctl As Control
nl = vbCrLf ' crlf gives you one line
CheckRequired = False ' assume everything ok
For Each ctl In MyMe.Controls
If ctl.ControlType = acTextBox Then
If ctl.Tag = "*" And Trim(ctl & "") = "" Then
msg = "Data Required for '" & ctl.Name & "' field!" & nl & _
"You can't save this record until this data is provided!" & nl & _
"Enter the data and try again . . . "
Style = vbCritical vbOKOnly
Title = "Required Data..."
MsgBox msg, Style, Title
ctl.SetFocus
CheckRequired = True
Exit Function
End If
End If
Next
End Function
現在,在表單事件(有取消)中,你可以這樣做:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = CheckRequired(Me)
End Sub
uj5u.com熱心網友回復:
正如@June7 提到的,Me僅在表單和報告后面有效。它是表單/報告名稱/物件的簡寫別名。為了實作您正在尋找的東西,您可以嘗試這個概念。創建如下全域例程:
Public Function Validate_BeforeUpdate(frm As Form) As Integer
Dim msg As String, Style As Integer, Title As String
Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In frm.Controls
'''' your other code
Validate_BeforeUpdate = 1
Exit For
Next
End Sub
要從您的其他表單中使用它,請執行以下操作:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Validate_BeforeUpdate(Me) = 1 Then
Cancel = True
End If
End Sub
這不是經過測驗的代碼,如果你遵循這個想法,你應該可以擁有你想要做的事情。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/505138.html
