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
.
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
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.
Thanks