[AccessD] Pushing an object reference vs pulling a reference.

jwcolby jwcolby at colbyconsulting.com
Thu Feb 12 15:07:21 CST 2009


 > That's pretty slick, JC. I have often had to fire events on another form, but this approach makes 
is ludicrously simple.


Just fine other than the method of obtaining the reference back to the control...

Another method is to create a method on the target form that the calling form can use to pass in the 
control.  This allows the target form to be opened from any other form and the control to sink 
passed in.

Paste the following code into frmDemoSinkingCommandButtonEvent.  NOTICE that you are replacing the 
Open event of the form, and creating a brand new function mCmd() to use to pass the control that you 
want to sink events for.

Public Function mCmd(lcmd As CommandButton)
     Set cmd = lcmd
     MsgBox cmd.Name
End Function

Private Sub Form_Load()
     On Error Resume Next
End Sub

Now, over in frmDemoCtls replace the form_Open with the following code:

Private Sub Form_Open(Cancel As Integer)
      Set fclsFrm = New clsFrm
      fclsFrm.mInit Me
      DoCmd.OpenForm "frmDemoSinkingCommandButtonEvent"
      Forms!frmDemoSinkingCommandButtonEvent.mCmd Me!Command15
End Sub

Close both forms and open frmDemoCtls.  Notice that frmDemoSinkingCommandButtonEvent opens "before" 
frmDemoCtls.

      DoCmd.OpenForm "frmDemoSinkingCommandButtonEvent"

In fact the form is being opened by frmDemoCtls and so appears to open first.

      Forms!frmDemoSinkingCommandButtonEvent.mCmd Me!Command15

In any event it then passes in me!Command15, a pointer to it's button and voila, instant 
communication.  Done this way ANY form can open frmDemoSinkingCommandButtonEvent since 
frmDemoSinkingCommandButtonEvent no longer depends on a specific form being opened.

As might be imagined, the first method is called "pulling" since the reference to the command button 
is being "pulled" into frmDemoSinkingCommandButtonEvent  as it opens.  The second method is called 
"pushing" since the opening form is pushing the control reference into 
frmDemoSinkingCommandButtonEvent after it opens it.

John W. Colby
www.ColbyConsulting.com


Arthur Fuller wrote:
> That's pretty slick, JC. I have often had to fire events on another form,
> but this approach makes is ludicrously simple.
> 
> A.



More information about the AccessD mailing list