Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

new to classes, how to avoid using global variable

Status
Not open for further replies.

Juddy58

Technical User
Jan 21, 2003
176
AU
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
 
Hi,

If you need to know that fields within a table have been updated, then you must store that info along with the record.

Why would you want to know this anyway?

ATB

Darrylle




Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
How about including a hidden field that is set in the update event and reset in the on current event?
 
Sorry,
How about including a hidden field control that is set in the update event and reset in the on current event?
 
Hi Darrylles, basically the code is for an unbound form I have designed, I have got the form working exactly how I want, but im trying to make it so I can reuse the form again within the database and for other tables etc, with a minimum of fuss. Im trying to avoid using the global variable as it will be altered from other forms if i use the same code, with a module variable it will prevent this issue.

thanks
Justin
 
Thanks for the reply Remou, I think a control could possibly be a good and easy solution for me, I will have a look at it in the morning.
I still wouldn't mind knowing if this can be done directly with code and a variable though, as im still learning about classes and my curiousity has got the better of me.

Thanks
Justin
 
Hi,

I'm just wondering at what stage does the 'updated' data reset to 'not updated'?

Are you only interested in data-changes during the application session? If so - why is it not important on the next day that that data has been changed?

ATB

Darrylle


Never argue with an idiot, he'll bring you down to his level - then beat you with experience.
 
Hi Darrylles, basically the code for the before update and after update is to track whether the user updates the current unbound record they are viewing, if they only view the record then the variable gblbolRecordUpdated will remain false, before the user goes to a different record a check is done to see if they have changed the current one, if they have then an ado procedure is run to update the record, if not there is no point in resaving a record that hasn't changed.
It is just a small thing to increase performance over a slow network.
Thanks
Justin
 
I guess you could keep gblbolRecordUpdated local and pass it to the class in a suitable event?

Call SaveMe (gblbolRecordUpdated)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top