Repeated code (1 Viewer)

fat controller

Slightly round the bend..
Local time
Today, 00:31
Joined
Apr 14, 2011
Messages
758
If a portion of code is repeated over and over in different controls on a form, can the code be stored in a module and then called upon at the point it is needed in the control's individual code?
 

CJ_London

Super Moderator
Staff member
Local time
Today, 00:31
Joined
Feb 19, 2013
Messages
16,612
yes, but it can be in the same module as the form module if you are only talking about one form.

Code:
private sub doeseverything()

    do something here

end sub

private sub ctrl1_afterupdate()

    doeseverything

end sub

private sub ctrl2_afterupdate()

    doeseverything

end sub
 

Galaxiom

Super Moderator
Staff member
Local time
Today, 09:31
Joined
Jan 20, 2009
Messages
12,852
You can also pass the Control to the function.

Code:
Private Sub whatever(ctrl As Access.Control)
 
    With ctrl
        dothestuff
    End With
 
End Sub

Then call it with

whatever Me.ActiveControl

Another way is to use a function. The function can be called directly in the Event.
 

fat controller

Slightly round the bend..
Local time
Today, 00:31
Joined
Apr 14, 2011
Messages
758
Thank you both :) I have the same process (essentially just requery & update field values) that take place on a number of occasions on a form, so that will save my old fingers wearing out :D
 

Users who are viewing this thread

Top Bottom