Forms and subroutines

jonathanchye

Registered User.
Local time
Today, 07:28
Joined
Mar 8, 2011
Messages
448
Tidying up code - Sub procedures

I am creating a lot of custom procedures used only in one main form. Right now what I am doing in the VBA editor is Insert > New Procedure.
Edit: tidied up post to make my question clearer

This inserts a new private Sub Procedure at the bottom of the General section of my Form's class object. This is fine for short codes but some of my sub procedures have over 50 lines.

If there a better way to organise my code? Most of my code are only used locally to update textboxes after doing some calculations hence a lot of my code looks like the line below:

Code:
Me.myFormName.txtTextBox1 = somecalculated value
I tried creating a new module and pasting my sub procedure codes in but I get errors saying "Invalid Outside parameter".
 
Last edited:
Look closely on the Private Sub-Routines on the Class modules. If you find you are repeating the same kind of actions in several places then you can write a common sub-routine for those actions and call that procedure with a single line of code from individual event procedures.

You can write common Functions in Global modules which are useful across forms or reports.

If you use lot of If....then...else...endif statements then consider using Select Case.....End Select statements.

When you complete your project take a second look at all the VBA Routines you have written you will know what to do.
 
the problem is that your FORM's module knows about the control "txtbox1" - but a general module doesn't, and therefore in a module, you need to construct the code differently.

but it is only worth doing this, if the function has general use for other controls - and clearly assigning a value to a single control is unlikely to be useful elsewhere.

Underneath all this, is why you have so many controls that need to be based on calculated values. The idea is to try and construct queries to return the information you want, and base the controls on the queries, with bound forms.
 

Users who are viewing this thread

Back
Top Bottom