PDA

View Full Version : Solved: Data binding through class module instead of form



adam_r_davis
01-20-2007, 06:12 PM
Hello,

I have had a small DB that has grown non stop for several years and the cracks are getting bigger. I now want to start coding in a more elegant fashion, but am having trouble finding examples to work from.

Goal1: Use class modules to get sensible functions like ThsiPerson.MergeDuplicatePerson(iOtherPersonID)
Goal2: Use a DirtyData table to avoid data clashes and Access' horrible "Do you want to drop changes" functionality replacing it with "JoeBloggs is currently editing this person's details"... Retry / Cancel.

Solution to date:
1: Create a class module clsPerson with various methods like Load and Save. ThisPerson.Load writes a "Reader" lock to a table "tRowLocks" and the various Let properties write a "Writer" lock to the same table. The ThisPerson.Save method clears the locks etc.
2: Create a form fmPerson that has a data source of "Select PersonID from tPeople" so there are no fields on the form that are bound, Equally a listbox could do the trick of supplying a PersonID, but either way I don't want to use the Access data binding.

Problem: I want to create an instance of my clsPerson when I open the form, load it based on Me.RecordSet!PersonID and have it persist until the form.OnCurrent or close events fire and then do a SaveChanges type process. That is, if the Surname or whatever is loaded up and the user edits it, I would like to just say:


Option Compare Database
Dim ThisPerson As New clsPerson

Private Sub Form_Current()
ThisPerson.Load (Me.Recordset!PersonID)
End Sub
...
private sub txtSurname_Update()
ThisPerson.Surname = txtSurname
end sub
'...etc

But of course ThisPerson is always nothing, forcing me to do:

private sub txtSurname_Update()
ThisPerson.Load
ThisPerson.Surname = txtSurname
end sub

Because I am forced to load on every field change, the locking process fires more often than nessesary, wasting time and leaves me with a data sync problem because firstname may have also been edited, so I have to loop through every control in case they were changed on the form in the event before the current event.

This defeats the purpose of using a class module in the first place...??!!?@?@? So I clearly have no idea to use a class module with the form...

Question: How can I have an instance of the object ThisPerson that remains loaded throughout the life of the form, or is there a different way of managing the data while still using class modules?

Thanks in advance for any help
Adam

XLGibbs
01-23-2007, 06:37 PM
If the form is bound to the data set at run time, I am not sure why the need to reinvent the wheel? A bound form, one that is created using the wizard can be later modified to suit your needs, but the wizard pre-builds the necessary class.

XLGibbs
01-23-2007, 06:38 PM
If the form is bound to the data set at run time, I am not sure why the need to reinvent the wheel? A bound form, one that is created using the wizard can be later modified to suit your needs, but the wizard pre-builds the necessary class.

adam_r_davis
01-23-2007, 06:59 PM
Hello XL Gibbs,

Thanks for responding.

Why do I want to bind manually rather than use the wizard:
1: I HATE trying to teach user muppets how to cope with Access 2003 default "This record has been edited by another user. Do you want to drop changes..." message. Do I definately want to track who is currently editing and ensure that the data collisions are handled more gracefully. I could still use the form wizard and wrap a series of function around each control, but doing that in the class module is more scalable.
2: I have lots of different types of people... if I want to have a "De-Duplicate" function then I have to implement it for people who are customers, relatives of customers (health application), referrers, debtors, employees and suppliers... and I have to code it on each version of the form for each type, be it the list, the calendar, the overview embedded in other detail or the high level detail view... this is the core issue and one that should probably force me to VB.Net (but I am a bit scared of the devil I don't know). So rather than code each form or do a module that I have to remember to call in the 20 or so places that surname will appear I want it to just be part of the class module. Then if I add a 21st place where surname is used then the form just behaves in a predictable fashion.

By the way I kept stuffing around and now have a working chunk of code... if anyone is interested I can post it, but by the sounds of it I am doing it the hard way anyhow.

Adam