Avatar of Rob4077
Rob4077Flag for Australia

asked on 

How do I avoid copying functions into different objects

I am developing a system that displays data in several different ways for different groups of users. However many of the fields and associated functions are the same. E.g. when a CompletedDate is updated for a field I need to do several other functions depending on the value of data in related fields. To accomplish this I have created an AfterUpdate event and associated function in the form's VBA library. Both the AfterUpdate event and Function refer to other fields on the form (e.g. me.AssignedTo).

When I implement the same functionality on another form I can copy the AfterUpdate event and function into the new form's VBA but every time I change the code I have to remember to change it in multiple places.

I know I could put the Function in a standard module but then I would need to feed to the function multiple field values; I can't just reference them by me.AssignedTo nor can I specifically reference them by using the form name because the same function could be called by multiple forms.

Is there any way to easily feed, or reference, all the fields to a function or what is the generally accepted approach to use in this case to keep the code as tidy as possible?
Microsoft Access

Avatar of undefined
Last Comment
Rob4077
Avatar of Scott McDaniel (EE MVE )
Scott McDaniel (EE MVE )
Flag of United States of America image

A function can accept input values:

Function MyFunction(StartDate As DAte, EndDate As Date) as Double
  MyFunction= DateDiff(StartDate, EndDate)
End Function

So if your function is identical, just create it in the module, and then in each AfterUpdate event call that function. For Example:

Msgbox MyFunction(Me.txtJobStart, Me.txtJobEnd)

If those fields are name differently on another form:

Msgbox MyFunction(Me.txtHireDate, Now)

You can also include optional arguments:

MyFunction(StartDate As Date, EndDate As Date, Optional AddDays As integer) As Double

In the example above, you can call it like this:

Msgbox MyFunction(Me.txtStartDate, Me.txtEndDate)

Or

Msgbox MyFunction(Me.txtStartDate, Me.txtEndDate, 7)
Avatar of Rob4077
Rob4077
Flag of Australia image

ASKER

Thanks for that but my functions need multiple parameters and they also change values of other fields. I can list all the parameters but how do I change all the associated fields on the calling form?
ASKER CERTIFIED SOLUTION
Avatar of Jim Dettman (EE MVE)
Jim Dettman (EE MVE)
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rob4077
Rob4077
Flag of Australia image

ASKER

That's awesome! That would have saved me sooo much duplicated code over the years. Thank you sooo much!!!
Microsoft Access
Microsoft Access

Microsoft Access is a rapid application development (RAD) relational database tool. Access can be used for both desktop and web-based applications, and uses VBA (Visual Basic for Applications) as its coding language.

226K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo