[英]How to use this VBA code to use as Public Subroutine in Access VBA
I have got this code below that restricts users to leave an empty field in a form.我在下面有这段代码,它限制用户在表单中留下一个空字段。 Now I want to use this in all of my forms.现在我想在我所有的 forms 中使用它。 I've tried to use in Public Subroutine using a module.我尝试使用模块在公共子程序中使用。 But it doesn't work.但它不起作用。
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
I just want to use this in all of my forms.我只想在我所有的 forms 中使用它。 How do I accomplish this?我该如何做到这一点?
Good question, and good idea.好问题,好主意。
So, keep in mind that "me" is just the current form you are working with.因此,请记住,“我”只是您正在使用的当前形式。
So, create a plane jane standard code module.所以,创建平面简标准代码模块。 And drop in your function like this with a "few" changes.并像这样通过“一些”更改放入您的 function 。
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
Now, in the forms event (which has that cancel), then you do this:现在,在 forms 事件(有取消)中,你可以这样做:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Cancel = CheckRequired(Me)
End Sub
As @June7 mentioned, Me
is only valid behind forms and reports.正如@June7 提到的, Me
只在 forms 和报告后面有效。 It is shorthand alias for the form/report name/object.它是表单/报告名称/对象的简写别名。 To achieve what you are looking for, you can try this concept.为了实现您正在寻找的东西,您可以尝试这个概念。 Create the global routine like below:创建如下全局例程:
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
And to use this from your other forms, do it like below:要从您的其他 forms 中使用它,请执行以下操作:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Validate_BeforeUpdate(Me) = 1 Then
Cancel = True
End If
End Sub
This is not a tested code, if you follow this idea, you should be okay to have what you are trying to do.这不是经过测试的代码,如果你遵循这个想法,你应该可以拥有你想要做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.