Redundant VBA code

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I have several buttons on various forms which 1. close
the current form, 2. open the main form and 3. maximize
the form. Isn't there a way to have these buttons all
address the same code instead of each button having it's
own code?

Thanks
 
I have several buttons on various forms which 1. close
the current form, 2. open the main form and 3. maximize
the form. Isn't there a way to have these buttons all
address the same code instead of each button having it's
own code?

One handy way to do this is to put the code in a Module, *as a
Function* - let's call it fCloseMe for example.

In the button's Click event you can type

=fCloseMe()

to call the function. If you're using DoCmd.Close bear in mind that
it's safest to explicitly tell Access *what* you're closing - it may
guess wrong about what object is open; for safety I'd suggest

=fCloseMe([Form])

and in the code,

Public Function fCloseMe(frmToClose As Form)
DoCmd.Close acForm, frmToClose.Name
<whatever else you want to do>
....
End Function
 
Or, to be clearer fo the OP, put the code within a >procedure< in a module,
then call the >procedure<. The OP should read online help for "procedures"
(not "modules").

HTH,
TC
 
Or do it as functions, as John suggested.

TC


TC said:
Or, to be clearer fo the OP, put the code within a >procedure< in a module,
then call the >procedure<. The OP should read online help for "procedures"
(not "modules").

HTH,
TC
 
Back
Top