We help IT Professionals succeed at work.

Moving Code out of Access Form

579 Views
Last Modified: 2013-12-09
I'm trying to move all of my code that is in the form module to a standard module. The only thing i'm stuck on is how do i move the following to a regular module and still get the keypress ascii value.

Private Sub DueDate_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
    Case 45
    KeyAscii = 0
  Screen.ActiveControl = Nz(Screen.ActiveControl, CDate(Format(Now(), "Short Date"))) - 1
    
    Case 43
    KeyAscii = 0
  Screen.ActiveControl = Nz(Screen.ActiveControl, CDate(Format(Now(), "Short Date"))) + 1

End Select
End Sub

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2013
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
That doesn't work, i want to be able to set the forms has module property to "no".  All controls have to be event driven functions for this to happen.
CERTIFIED EXPERT
Top Expert 2013

Commented:
I am just curious: why do you need it this way? Is it some sort of security measure?

Author

Commented:
I'm always updating the database, reports, more functionality, for the office personnel etc. It would seem to me to be a lot easier to make updates to only in  code and never really have to make any modifications to the form.  Plus it should be quicker, faster to load etc.
Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
<<  Plus it should be quicker, faster to load etc. >>

In addition to mx's comments, the KeyPress event specifically does not affect page load time.

Author

Commented:
WOW, Really guys....of course it doesn't but the underlying code that each control has on the form does. Would you agree?
CERTIFIED EXPERT
Top Expert 2013

Commented:
The will be somewhere anyway, either in the event procedure or in the function. If you believe the event procedure affects the performance why don't you think the function would?
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
Once the app is compiled it won't make much (if any) difference. When the form loads, a series of events fire, and then it waits for user input. Those events (the Load, Open, GotFocus, etc events) need to be as streamlined as possible, but after that it makes no difference. For example, the KeyPress event of a button is of no consequence in regard to load times.

I have some VERY heavy forms, and they load just as quickly as forms with no modules, assuming I've got the right pieces in place to allow this (i.e. not too many subforms, restricting lists and combos, etc). If you're having issues with your form load times, it's not the code behind that form that's causing your issues, it's what that code DOES that is the issue. Calling code that fires in the Open event from a Module vs calling it from the Form's Class Module isn't going to make a difference.

As to being easier - like beauty, that's in the eye of the beholder, so it's sorta hard to argue that. But having to work down through a chain of code to find out what really happens is always more difficult than being able to directly access the code in the code environment (and you can't do that with events declared directly in the Event Properties window).
CERTIFIED EXPERT
Top Expert 2013

Commented:
Just wanted to add to the previous comment. If your form is data driven then the underlying table/query may slow down the load time if the records being loaded are not properly indexed.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
<< WOW, Really guys....of course it doesn't but the underlying code that each control has on the form does.  >>

Sure - everything adds up, but I'd be very surprised if moving that particular event would make a noticeable performance difference.

This is worth taking a look at if you haven't seen it already:
http://www.fmsinc.com/tpapers/faster/#Forms

It does list Lightweight/no-code forms as a tip, but if improving performance is the primary goal, I'd personally work on optimizing the form to an acceptable point with just about any of the data related tips first.  ie: I'd focus on the queries, number of controls (especially subforms) and other data-related issues before resorting to API functions for code that built-in Events like the KeyPress event are perfectly suited to handle (just a matter of priority).

Personal preferred style is another matter, of course, if that is the driving factor.
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database Architect / Application Developer
CERTIFIED EXPERT
Top Expert 2007

Commented:
" Would you agree?"
No I would not.
aikimarkSocial distance; Wear a mask; Don't touch your face; Wash your hands for 20 seconds
CERTIFIED EXPERT
Top Expert 2014

Commented:
The only time I move code out of a form is when I refactor the code so that it can be invoked by multiple forms/controls.
Martin LissSocial distance - Don't touch your face - Wash your hands for 20 seconds
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2018

Commented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2013

Commented:
Recommend splitting the points equally between mx and chaau's comments, with mx's comment at  http:#a39647031 marked as Accepted and chaau's comment at http:#a39646972 as Assisted.

The bottom line is as mx pointed out that this cannot be done by the author's method, the way the author wants.  

Alternative solutions were posted in both comments.
Scott McDaniel (EE MVE )Infotrakker Software
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2014

Commented:
I agree.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.