[AccessD] Synchronize popup

JWColby jwcolby at colbyconsulting.com
Fri Jul 28 00:26:41 CDT 2006


Karen,

This stuff really isn't that advanced.  It appears that I am about to start
a book on this as well as other subjects so stay tuned on that front.
Anyway...

The only "gotcha" in the following is that it does not work in A97 since
that version cannot raise events.....

Objects such as forms and controls raise events all the time.  Think
OnClick, OnCurrent, OnOpen.

Classes can sink events as well.  Your form (which is a class) sinks the
OnClick of a button or other control, OnUpdate of a text box or combo, etc.

If you program, then you are accustomed to handling events from controls and
forms.  The form class sinks events from the form itself as well as from any
controls on the form.

Placing code such as:

'*+ custom events Declarations
Public Event PostAfterUpdate()
Public Event PostCurrent()

In the header of a class (your form for instance) allows you to then "raise"
an event - in this case an event called PostAfterUpdate or PostCurrent.  I
call them "Post"Current because I raise the event AFTER the current event is
processed.  You can call your event anything you want:

'*+ custom events Declarations
Public Event ColbyEvent()
Public Event MailReceived()

You can even define parameters

'*+ custom events Declarations
Public Event PostAfterUpdate(Cancel as integer)
Public Event PostCurrent(MyParam as string)

So...

Having defined my event (informing the class that I intend to raise an event
somewhere in the class) in the header, I can then RAISE my event anywhere I
want to:

Function SomeSpecialProcess()
	Some processing
	RaiseEvent PostAfterUpdate
	Some more processing
End function

OK so... You want to sync a popup form to a main form.  By this I assume you
want to cause the popup form to "move to" the same record as the main form
is on, and you want it to do so even if the main form changes from one
record to the next.  In order to accomplish this, ONE WAY is to raise an
event in the main form whenever the main form changes from one record to the
next.  So, in the main form, tell the form class that you intend to raise an
event:

'*+ custom events Declarations
Public Event PostCurrent(lngMyPK)

Then in the current event, raise your event.  Technically you don't even
have to raise an event at all since the Current event is already being
raised, however doing so gives you complete control over WHEN the event is
raised relative to any other processing you might do in the current event.

The following is actual code (more or less) from my framework:

Private Sub form_Current()
    'Stop
    mScanParentPKVal
    On Error Resume Next
    Fltr pFltrName, mfrm!txtPKID.Value
    mdclsCtlCboRecSel.mFrmSyncRecSel
    mNewRecordCheck
    RaiseEvent PostCurrent MyPK
End Sub

As you can see, my form has a current event, which I use to perform some
specific processing.  I then raise my own event,  "PostCurrent".

Having done that, I now need to sink that event somewhere.  In your case,
the popup form needs to sink the event raised by the main form.  In order to
sink an event in a class (remember that a form's code is in a class) you
need to dimension a variable "Withevents", so in the header of the POPUP
FORM:

Private WithEvents mfrm As Form

You have just told the popup form that you will be getting a pointer to a
form object, and that you will be sinking events from that object (the
WithEvents keyword).

So you open the form, then somehow you set the variable mfrm to point to the
main form.  You can do that in the OnOpen of the popup form for example:

Sub Form_Open()
	set mfrm = forms("MyMainForm")
End sub

Voila, you now have a pointer to the main form contained in mfrm and because
you used the WithEvents keyword when you dimensioned the variable, you can
now sink events from that form.  You now create an event sink in the popup
form's module:

Sub mfrm_PostCurrent(lngMyPK)
	msgbox "The main form's current event just fired and passed in the
PK from the record it is displaying: " & lngMyPK
End sub

NOTICE that we called the event mfrm_ (the name of the variable we
dimensioned in our header) and PostCurrent (the name of the event that the
MAIN FORM IS RAISING!!!)

You are now done.  Well almost anyway...

You have a form variable that is grabbing events from the main form,
SPECIFIC EVENTS that you tell it to grab - by creating event sinks for
exactly the events you want it to sink.  All you need to do now is to use
the PK of the record in the main form (which was passed in by the event) to
"find" that record and move to it.

Simple eh?  Well, it would be if you just practice it a bit.  We programmers
sink events all the time.  If you program it is old hat.  All you need to
understand now is how to raise your own events, how to get a pointer to some
object which is raising events, and how to sink some particular event(s) in
your own module.  This is not rocket science, it is a teeny tiny extension
on what we already do every day.

Disclaimer: The above is "air code" compiled in the outlook editor.  

I can and will create a working demo tomorrow in Access and make it
available.  It is VERY easy to do and requires almost no code at all.  In
fact it takes more code to use the passed in PK to find and move to the
record than all the other code combined.

There are some tiny gotchas, such as the fact that a form won't close
correctly if a pointer to it exists, i.e. the PopUp form has to set its
pointer to the main form to nothing before the main form tries to close.

More tomorrow.  

IN FACT there is another way to do this, which is an intermediary class
which I wrote a demo for previously.  I called it a message class.  The
process still uses raised and sunk events however.  You can go to my web
site: www.colbyconsulting.com , register if you haven't already, Click
Example code / WithEvent Demos, and download MsgClassWithEvents.zip.  This
uses the above mentioned message class to send messages between forms.

Enjoy.

John W. Colby
Colby Consulting
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Karen Rosenstiel
Sent: Thursday, July 27, 2006 10:30 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Synchronize popup

Ah.....um......

JC, I don't know what you're talking about. You are way too advanced for
this amateur. 

Actually, I get the feeling sometimes that you are too advanced for many of
the PROFESSIONALS on this list.

But thanks anyway.

Regards,

Karen Rosenstiel
Seattle WA USA 


-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of JWColby
Sent: Thursday, July 27, 2006 1:37 PM
To: 'Access Developers discussion and problem solving'
Subject: Re: [AccessD] Synchronize popup

Use Withevents / Raiseevents.  Raise an event in the form that is the main
form, in OnCurrent.  When the popup opens, dim a form variable, set it to
the pointer of the main form, then build an event handler for that form.
>From then on, when the main form raises it's event, the popup form will 
>sink
the event and do whatever you want it to do. 


John W. Colby
Colby Consulting
www.ColbyConsulting.com

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of karenr7 at oz.net
Sent: Thursday, July 27, 2006 2:44 PM
To: accessd at databaseadvisors.com
Subject: [AccessD] Synchronize popup

Dear gang,

I am working on a database to track medical records that are sent to the
wrong medical providers.

Three tables: tPatient, tErrors, tChecklist.

Relationships: tPatient.PatientID (PK one-to-many)-->tErrors.ErrorsID (FK
one-to-one) --> tChecklist.ErrorID

The main form is fPatient with fErrors as a continous subform and fChecklist
as a popup to fErrors. The popup is not picking up the FK from
tErrors.ErrosID. I know how to synchronize a subform within the main form,
but how do I syncronize a popup to its master form, which is a subform? I
want it to syncronize continually, not just on open.

Thank you.


Regards,
Karen Rosenstiel
Seattle WA USA


--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com

--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com



--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list