Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 558
  • Last Modified:

Moving Code out of Access Form

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

0
jb702
Asked:
jb702
  • 4
  • 3
  • 3
  • +4
2 Solutions
 
chaauCommented:
A question: Why are you doing this?

As for your problem: it is very easy. Create a Sub in your module with the same parameter, as the event procedure:
Private Sub DueDate_handler(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

And in your Form's sub just call the sub from the module:
Private Sub DueDate_KeyPress(KeyAscii As Integer)
     DueDate_handler(KeyAscii)
End Sub

Open in new window

0
 
jb702Author 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.
0
 
chaauCommented:
I am just curious: why do you need it this way? Is it some sort of security measure?
0
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
jb702Author 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.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
"i want to be able to set the forms has module property to "no"
The only way you will be able to do this - get the ASCII value of the key pressed - will be to use the Windows API. Otherwise, you will need to pass the value as shown.

" Plus it should be quicker, faster to load etc."
I seriously doubt that with today's systems.
0
 
mbizupCommented:
<<  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.
0
 
jb702Author Commented:
WOW, Really guys....of course it doesn't but the underlying code that each control has on the form does. Would you agree?
0
 
chaauCommented:
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?
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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).
0
 
chaauCommented:
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.
0
 
mbizupCommented:
<< 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.
0
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
" Would you agree?"
No I would not.
0
 
aikimarkCommented:
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.
0
 
Martin LissRetired ProgrammerCommented:
I've requested that this question be deleted for the following reason:

Not enough information to confirm an answer.
0
 
mbizupCommented:
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.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
I agree.
0

Featured Post

Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

  • 4
  • 3
  • 3
  • +4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now