Run Code in Group

  • Thread starter Thread starter F1stman
  • Start date Start date
F

F1stman

Hello All,

I am not new to Access but have not used modules, etc. very much. On a form
I have a script about 80 lines long that I want to run on the lost focus
event of 20 different text boxes. It checks all values against new
calculations. The code has been tested and runs fine.

The Goal: How can I save that code and then simply enter one line of code
for each lost focus event? Is there a way to save it as a script?

Please let me know if I have not been clear. Thank you in advance for the
help.

Adam Kemp
 
Put the code in a standard module. Make sure it is identified as a Public
Function or Sub.

Call the code from the event. Are you sure you want to use Lost Focus?
Describe what you are doing, please.
 
Thanks Dave,

I have a form which lists cost information for a construction project. Each
value is calculated as a percentage of other values. There is also a manual
override for each field. The manual and calculated are unbound. The script
calculated and checks to see if each cost has a manual entry. If not it
enters the calculated amount into the bound text box. If it does have a
manual it enters that. It has to be on each since changing the value for one
will change it for the others. I also would like to run the script when the
form is opened.

Thanks again, Adam
 
Use the Form's Load event to run it the first time.

Instead of the Lost Focus event, use the After Update event. If you use the
Lost Focus event, you will be running the code when maybe it isn't necessary.
The Lost Focus event fires every time you leave the control. The After
Update event only fires if data in the control has been changed. Using the
Lost Focus event means that you will run the code even if no changes have
been made.
 
Dave,

I feel foolish having to ask this but how do I run the module from the event?

Adam
 
Well, you don't run a module. A module is a contain for Functions and Subs,
so you will be running one of those.

To put code in an event, open the form in design view. Select the control
you want the procedure to run from and open the properties dialog. Select
the Events tab. Select the event you want to use. In this case, the after
update event. Click on the small command button to the right with the 3
dots. When the dialog opens, select Event Procedure. The VB editor will
then open with the cursor positioned in the event sub. Enter the code there.
 
Back
Top