Basic question! Sharing a funtion across two forms

Hi,
This is fairly fundamental.

I have 2 forms - frm_A and frm_B.

On the "Before Update" event of frm_A I set "me.CustomerDate = now()"   (and a few other changes).

QUESTION: How do I "share" the update code with frm_B?   Where do I put the code?  Will the "me.CustomerDate" syntax work?
Patrick O'DeaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

IrogSintaCommented:
To update a control on another form you would use the syntax:
Forms!FormName!ControlName = ValueYouwantToPass

For Instance, if frm_B also has a CustomerDate that you want to update you would use:
Forms!frm_B!CustomerDate = Now()

I would think however that these should be done on the AfterUpdate event rather then the BeforeUpdate event.  You use BeforeUpdate in the likelihood that you would want to cancel your update to the control you are editing.

Ron
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
If a function is to be used in more than one place, the traditional way to do this is to create a STandard Module and put your code there, then modify it to work in a generic manner. Specifically, the "Me" syntax won't work, and you'll instead have to pass in a different object. In your case, you could pass in a Form object:

Function MyCoolFunction(frm As Form) As Boolean
  frm.CustomerDate = Now()
  frm.CustomerName = "Nothing"
  MyCoolFunction = True
End FUnction

You then call it in your Form as needed:

MyCoolFunction Me

In the line able "ME" refers to the Form ...

As an aside: You really shouldn't be modifying data in the BU event. This can cause endless loops (at best) or can result in invalid data (at worst). If you need to modify data before updating it on the form, find another Event to use.
0
Dale FyeCommented:
For something as simple as this, I would just keep putting that code in the BeforeUpdate event of the form.  It makes no sense to create a public function or subroutine that can be used by both of these forms.

However, if you want to share code between many forms or other code segments, you need to create a Public Subroutine or code module and place it in a standard code module (not a form or class module).

If you right click in the Project window of the Visual Basic window, you will see a shortcut menu that includes an option for Insert => Module (image below)
Insert ModuleClick on the "Module" option and a new code window will be opened to a new module.  Then you create your code that you want to be able to reuse.  In this case, I'll assume you are going to use this to do what you asked in your question.

Public Sub UpdatedWhen(ctrl as control)

    ctrl.Value = Now()

End Sub

Open in new window

This code would allow you to update any date control with the value associated with now.  I have declared the passed argument as a control because I want to be able to pass it a control value from within any form in your application.  To call it from your forms, you would simply use:

    UpdatedWhen me.CustomerDate

Hope this helps
Dale
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Dale FyeCommented:
Scott,

I've been using the Form_BeforeUpdate() event for years to:
1.  Check for valid entries in required fields
2.  If not cancelled for problems identified in #1, then add values to the "LastModified" and "ModifiedBy" fields in tables

Why would this would cause a loop?
0
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
It's really just an opinion, and I should have stated that as such, so:

In my opinion, the BU event is best used to validate data and to Cancel the process if the validation fails. It really shouldn't be used to modify fields on the form, since that could cause the BU event to be continually called. It's fine to call external events/functions/methods, like an Audit Trail function.

Modifying data through VBA won't have any effect on the BU event, of course, unless you run into a conflict (i.e. you try to update the same records/fields as Access is).

And this phenomenon doesn't always happen; I've seen plenty of instances where methods such as you're using work perfectly fine. As with everything, used properly the method you suggest poses no troubles.

And, again - it's just my opinion, and I should have stated it in that manner.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
PatHartmanCommented:
I would think however that these should be done on the AfterUpdate event rather then the BeforeUpdate event.  You use BeforeUpdate in the likelihood that you would want to cancel your update to the control you are editing.
It really shouldn't be used to modify fields on the form, since that could cause the BU event to be continually called.
I disagree.  It is not just an opinion.  The form's Before and After update events cannot be interchanged if you are validating data or making changes to fields.

The Form's BeforeUpdate event is the LAST event that runs before a record is saved.  It is the proper place to do any validation that relates to multiple fields such as comparing one date to another or to ensure that a field actually contains data.  Both of these types of validation are either awkward or impossible to do in the form level events.  This event is also used to log changeby and changedate if your app does that as do all of mine.  Updating fields of the form's RecordSource whether bound to controls or not does NOT cause any loop when done in the Form's BeforeUpdate event.  

The event that people mistakenly use that DOES cause looping is the form's AfterUpdate event.  The AfterUpdate event runs AFTER the record is saved.  Therefore, if you change a field value in this event, the record MUST be saved again and so you will put the form into a never ending loop.  So, the record is saved, the AfterUpdate event runs, it changes the record which forces the BeforeUpdate event to run and then the AfterUpdate event which changes the record again and forces the BeforeUpdate event to run and then the AfterUpdate event runs and changes the record again which forces the BeforeUpdate event to run and then the AfterUpdate event changes the value one more time.  Are you dizzy yet?    Newer versions of Access are able to recover from this by analyzing the stack when it overflows and then gracefully ending the loop.

However, changing a control value in the CONTROL's BeforeUpdate event will cause a problem.  Perhaps this is what is causing the confusion.
0
Patrick O'DeaAuthor Commented:
Thanks folks for all the comments.  I've solved my immediate problem and am still digesting your valuable contributions.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.