Moving Controls to a Different Form

D

dhstein

I moved some Controls to a different form and I had to copy the VB code from
one Class Object to the other. Is there a better way to do this, or is that
the only way. Thanks.
 
T

Tom van Stiphout

On Thu, 27 Nov 2008 10:58:08 -0800, dhstein

That is the way.

-Tom.
Microsoft Access MVP
 
K

Ken Sheridan

You can put the code for event procedures in functions in a standard module
and call the function as the event property. In the function you can't
reference the form (or more correctly the current instance of the class) with
Me, however, or simply refer to a control or other property of the form by
its name; you'll need to specifically reference the object. As a simple
example, say you have a button with the following code it it's Click event
procedure:

Dim strForm As String
Dim strControl As String
Dim strMessage As String

strMessage = "You have pressed button " & _
Me.ActiveControl.Name & " on form " & Me.Name

MsgBox strMessage

Instead of using the control's event procedure you could put the following
function in a standard module:

Public Function MyButtonFunction()

Dim strForm As String
Dim strControl As String
Dim strMessage As String
Dim frm As Form
Dim ctrl As Control

Set frm = Screen.ActiveForm
Set ctrl = frm.ActiveControl

strMessage = "You have presssed button " & _
ctrl.Name & " on form " & frm.Name

MsgBox strMessage

End Function

and set the On Click event property of the button to the following in its
properties sheet:

=MyButtonFunction()

The function can then be called from a button on any form.

Another way would be to pass the form and control as arguments into the
function by amending the function to:

Public Function MyButtonFunction(frm As Form, ctrl As Control)

Dim strForm As String
Dim strControl As String
Dim strMessage As String

strMessage = "You have presssed button " & _
ctrl.Name & " on form " & frm.Name

MsgBox strMessage

End Function

and the button's On Click event property to:

=MyButtonFunction([Form],[Form].[ActiveControl])

Whichever of the two ways you do it, if you copy the button to another form
it will work the same without having to copy any code.

Ken Sheridan
Stafford, England
 
Top