如何从模块 (VBA) 中的 MS Access 获取实际表单的实例 [英] How to get instance of actual form from MS Access in a Module (VBA)
问题描述
我在 MS Access 2010 中有一堆表单.它们都是绑定表单,但我欺骗它们只在我单击 btnSave
后才保存.
I have a bunch of forms in MS Access 2010. All of them are bound forms but I tricked them to only save once I click btnSave
.
主要问题是我有很多代码对于某些事件中的每个表单都是相同的,例如 form_beforeUpdate()
或 form_afterUpdate()
或 <代码>form_Load().一个例子:
The main problem is that I have quite a lot of code that is the same for every form in some events such as form_beforeUpdate()
or form_afterUpdate()
or form_Load()
. An example:
Private Sub btnSave_Click()
'Check first that all fields have been filled
If CheckAllFields = True Then
Exit Sub
End If
TempSaveRecord = True
Me.Dirty = False 'this will make an attempt of form_Update()
Form_Load 'After Saving we restart the data
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer)
If TempSaveRecord = False Then
Me.Undo
Exit Sub
End If
End Sub
CheckAllFields
只是一个检查所有必填字段是否不是 null
的例程:
CheckAllFields
is just a routine that checks whether all the required fields are not null
:
Private Function CheckAllFields() As Boolean
Dim Ctrl As Control
CheckAllFields = False
'Check for unfilled fields
For Each Ctrl In Me.Controls
If Ctrl.Tag = "required" And IsNull(Ctrl) Then
MsgBox "The field " & Ctrl.Name & " has not been filled."
CheckAllFields = True
Exit For
End If
Next Ctrl
End Function
我想在一个模块中完成所有这些,但我无法找到一种方法来获得表单的实际实例.任何帮助将不胜感激.
I would like to do all of this in just one Module, but I can't find a way to get the actual instance of the form in that moment. Any help would be greatly appreciate it.
谢谢
推荐答案
我一直这样做.
这是一个例子:
'in a main module
Public Sub mFormLoad(p_form As Form)
'do stuff with p_form
end sub
然后,在实际的表单中:
Then, in the actual form itself:
Private Sub Form_Load()
mFormLoad Me
End Sub
<小时>
对于您的示例,将您的函数更改为 public 并为表单添加参数:
For your example, change your function to be both public and add the argument for the form:
public Function CheckAllFields(p_form as form) As Boolean
Dim Ctrl As Control
CheckAllFields = False
'Check for unfilled fields
For Each Ctrl In p_form.Controls
If Ctrl.Tag = "required" And IsNull(Ctrl) Then
MsgBox "The field " & Ctrl.Name & " has not been filled."
CheckAllFields = True
Exit For
End If
Next Ctrl
End Function
然后将您调用的行更改为:
and then change the line you call it to be:
If CheckAllFields(me) = True Then
这篇关于如何从模块 (VBA) 中的 MS Access 获取实际表单的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!