when is the time right to create a sub to do something?

  • Thread starter Thread starter DawnTreader
  • Start date Start date
D

DawnTreader

hello

i have a project where i have lots of fields that filter things on the
forms. i have the typical apply filter button, but i have also been using a
key catch for the enter key.

when a user updates the filtering field and hits enter i cause it to requery
the sub query that the filtering field creates a filter for. on some forms
the amount of times i put in the code:

If KeyAscii = vbKeyReturn Then
Me.mySubform.Requery
End If

is ridiculous.

so the question is should i make a routine for the form(s) that calls the
little bit of code? it seems so repeatative to put this code in again and
again. would subroutining this speed up the db in general, or save on hard
drive space. i know it would probably be minimal gains, but the question
boils down to when is it more efficient as a subroutine?
 
Two things:

1) I wouldn't suggest in this case to actually use the after update event of
a control to fire your code...not the keydown (and trapping the return key).

The reason why I'm suggesting this is two fold, first the user and most of
the people who used the web today who will usually hit tab to go to the next
field not the enter key. If you use the after update event of the control,
then if the user hits tab OR the enter key, both keys will work for you.

Furthermore another bonus is that if the control is not changed (no data is
modified), and the user hits the enter key to move through the controll(s),
then your code will WILL NOT fire. In your case if the user het the enter
key several times to move through few controls on the screen, your keydown
code will fire...*even* if the control has not been changed.

2) The reason why made that long winded suggestion to the above, is in fact
the second reason and advantage of using the after update event is you can
highlight twelve controls on the screen all at the same time, AND THEN TYPE
in the name of the public function you want in each of those twelve controls
to run.

The significant advantage of this is you don't have to go to the code editor
twelve times and enter the code stub twelve times. What you do is simply
make a public function in the form as follows

Public Function MyRequery1

Me.mySubform.Requery

end funciton.


And, now, in in the after update event you simply type in:

=MyRequery1()

As mentioned if you highlight fifteen controls all the same time, then
you'll only have to type in =MyRequery1() one time, and they will all call
your public function (that public function is of course placed in particular
form)

In your code examples since you really only have one line of code, it
doesn't help whole lot to "move" out the code to a particular function as I
did above. However if you use the after update event of the contorl as
suggested, then you can highlight fifteen controls, type in the function
name once, and not have to enter fifteen little code stubs.
 
Back
Top