Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I avoid copying functions into different objects

Posted on 2014-12-25
4
Medium Priority
?
172 Views
Last Modified: 2014-12-26
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?
0
Comment
Question by:Rob4077
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 85
ID: 40518306
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)
0
 

Author Comment

by:Rob4077
ID: 40518348
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?
0
 
LVL 58

Accepted Solution

by:
Jim Dettman (Microsoft MVP/ EE MVE) earned 2000 total points
ID: 40518419
You can pass references to objects as well as discrete values:

Sub ProcessDates(frm As Form)

  frm.NumberofDays = DateDiff("d",frm.StartDate, frm.EndDate)

End Sub

 and the call in the AfterUpdate property sheet would be:

ProcessDates([Form])

  You can pass references to any object; controls, reports, recordsets, etc.

Jim.
0
 

Author Comment

by:Rob4077
ID: 40518439
That's awesome! That would have saved me sooo much duplicated code over the years. Thank you sooo much!!!
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question