Hi I have been developing a form in access in which all textboxes have similar onenter events and afterupdate events
'tbxproductname
Private Sub tbxProductName_Enter()
mdlvarValueBeforeUpdate = Nz(Me.tbxProductName, 0)
End Sub
Private Sub tbxProductName_AfterUpdate()
If Nz(Me.tbxProductName, 0) <> mdlvarValueBeforeUpdate Then
mdlbolRecordUpdated = True
End If
End Sub
'tbxproductdescription
Private Sub tbxProductDescription_Enter()
mdlvarValueBeforeUpdate = Nz(Me.tbxProductDescription, 0)
End Sub
Private Sub tbxProductDescription_AfterUpdate()
If Nz(Me.tbxProductDescription, 0) <> mdlvarValueBeforeUpdate Then
mdlbolRecordUpdated = True
End If
End Sub
I found some code which allows me to use two class modules to programatically set the desired eventprocedures for all textboxes in a particular form. Besides being a good excuse to learn abit about classes it will also be helpful as I will want to use a copy of this form again in this and other databases. Basically I wont have to worry about copying the events for new textboxes etc.
'--------------------------------------------
'CODE ADDED TO FORM
Private Sub Form_Open(Cancel As Integer)
mobjFormControls.Init Me
End Sub
'--------------------------------------------
'CLSFORMCONTROLS - CODE
Option Compare Database
Option Explicit
Private WithEvents Fm As Access.Form
Private mcolControls As New Collection
Public Sub Init(ByRef rfrm As Access.Form)
AttachEvents rfrm
End Sub
Private Sub AttachEvents(ByRef rfrm As Access.Form)
Dim ctl As Access.Control
Dim objTxt As clsTextbox
Set Fm = rfrm
For Each ctl In Fm.Controls
If TypeOf ctl Is Access.TextBox Then
Set objTxt = New clsTextbox
objTxt.Init ctl
mcolControls.Add objTxt
End If
Next
End Sub
'--------------------------------------------
'CLSTEXTBOX - CODE
Option Compare Database
Option Explicit
Private WithEvents mtxt As Access.TextBox
Dim modvarValueBeforeUpdate
Public Sub Init(ByRef rtxt As Access.TextBox)
Set mtxt = rtxt
With mtxt
.OnEnter = "[Event Procedure]"
.AfterUpdate = "[Event Procedure]"
End With
End Sub
Private Sub mtxt_Enter() 'EVENT PROCEDURE FOR ENTER EVENT OF TEXTBOX
modvarValueBeforeUpdate = Nz(mtxt, 0)
End Sub
Private Sub mtxt_AfterUpdate() 'EVENT PROCEDURE FOR AFTERUPDATE EVENT OF TEXTBOX
If Nz(mtxt, 0) <> modvarValueBeforeUpdate Then
gblbolRecordUpdated = True
End If
End Sub
'---------------------------------------------
I have got the code working fine my only issue is that the afterupdate event of the textbox use to set a module level variable (mdlbolRecordUpdated)in the form to true, to get the code to work I have changed the variable to a global variable (gblbolRecordUpdated)so both the form and the class can access it, this gives me the issue
that if I have another form that uses the same code I will end up with the global variable being set to true from another form.
Im just wondering how i would go about keeping a module level variable for the particular form and setting it to true from the previous afterupdate event.
Any help would be greatly appreciated.
Thanks
Justin
'tbxproductname
Private Sub tbxProductName_Enter()
mdlvarValueBeforeUpdate = Nz(Me.tbxProductName, 0)
End Sub
Private Sub tbxProductName_AfterUpdate()
If Nz(Me.tbxProductName, 0) <> mdlvarValueBeforeUpdate Then
mdlbolRecordUpdated = True
End If
End Sub
'tbxproductdescription
Private Sub tbxProductDescription_Enter()
mdlvarValueBeforeUpdate = Nz(Me.tbxProductDescription, 0)
End Sub
Private Sub tbxProductDescription_AfterUpdate()
If Nz(Me.tbxProductDescription, 0) <> mdlvarValueBeforeUpdate Then
mdlbolRecordUpdated = True
End If
End Sub
I found some code which allows me to use two class modules to programatically set the desired eventprocedures for all textboxes in a particular form. Besides being a good excuse to learn abit about classes it will also be helpful as I will want to use a copy of this form again in this and other databases. Basically I wont have to worry about copying the events for new textboxes etc.
'--------------------------------------------
'CODE ADDED TO FORM
Private Sub Form_Open(Cancel As Integer)
mobjFormControls.Init Me
End Sub
'--------------------------------------------
'CLSFORMCONTROLS - CODE
Option Compare Database
Option Explicit
Private WithEvents Fm As Access.Form
Private mcolControls As New Collection
Public Sub Init(ByRef rfrm As Access.Form)
AttachEvents rfrm
End Sub
Private Sub AttachEvents(ByRef rfrm As Access.Form)
Dim ctl As Access.Control
Dim objTxt As clsTextbox
Set Fm = rfrm
For Each ctl In Fm.Controls
If TypeOf ctl Is Access.TextBox Then
Set objTxt = New clsTextbox
objTxt.Init ctl
mcolControls.Add objTxt
End If
Next
End Sub
'--------------------------------------------
'CLSTEXTBOX - CODE
Option Compare Database
Option Explicit
Private WithEvents mtxt As Access.TextBox
Dim modvarValueBeforeUpdate
Public Sub Init(ByRef rtxt As Access.TextBox)
Set mtxt = rtxt
With mtxt
.OnEnter = "[Event Procedure]"
.AfterUpdate = "[Event Procedure]"
End With
End Sub
Private Sub mtxt_Enter() 'EVENT PROCEDURE FOR ENTER EVENT OF TEXTBOX
modvarValueBeforeUpdate = Nz(mtxt, 0)
End Sub
Private Sub mtxt_AfterUpdate() 'EVENT PROCEDURE FOR AFTERUPDATE EVENT OF TEXTBOX
If Nz(mtxt, 0) <> modvarValueBeforeUpdate Then
gblbolRecordUpdated = True
End If
End Sub
'---------------------------------------------
I have got the code working fine my only issue is that the afterupdate event of the textbox use to set a module level variable (mdlbolRecordUpdated)in the form to true, to get the code to work I have changed the variable to a global variable (gblbolRecordUpdated)so both the form and the class can access it, this gives me the issue
that if I have another form that uses the same code I will end up with the global variable being set to true from another form.
Im just wondering how i would go about keeping a module level variable for the particular form and setting it to true from the previous afterupdate event.
Any help would be greatly appreciated.
Thanks
Justin