Link to home
Start Free TrialLog in
Avatar of yhwhlivesinme
yhwhlivesinme

asked on

Trap Printing in form view

I have some users who were trained before to press ctrl-p to print out their reports because the toolbars were restricted.  Now occasionally (about once a month) a user hit's ctrl-p in a form and prints out like 100 pages, is there a way to trap the printing in a form?
Avatar of Jim Horn
Jim Horn
Flag of United States of America image

Define 'trap the printing in a form'.

In many applications, a form will have a Print button (with .Click event code) with a caption of &Print.  This means that if the user hits {Ctrl} + P, then the code behind the Print button's .Click event.
Avatar of yhwhlivesinme
yhwhlivesinme

ASKER

so if I add a button to the form that's a print button, it will execute if the user hit's ctrl p?
(1) Add a button, and give it a name that makes sense, like cmd_print, btn_print, etc.
(2) In the button's Caption event type &Print
(3) Type VBA code in the button's .Click event that performs a printing-like task (DoCmd.Report "Some Report Name", acViewNormal, ...)

Then, when the user selects {Ctrl} + P, the code in (3) will execute, just as if the user clicked on the button.

Hope this helps.
-Jim
what's the caption event type? do you mean just caption? I'm using acc 97 btw
I added a button, in the caption I added &Print, and then in the onclick event I put a messagebox, but it doesn't execute it!
That is because Access has captured the Print. remove the Print Option from the Menu (customize).
Or add a KeyPress event handler to the form and cancel the keypress if it is Ctrl P
keypress event wont work (yes I did try it) because another control cant have the focus, the form must have the focus, but you can press ctrl-p anytime.
I just tried adding a custom toolbar as well and that doesn't work. I also tried creating a new form and just adding a button on that (for simplicity) and it didn't work either.  Let me walk you through the steps so you can see if I'm doing anything wrong

create a button
change it's caption to &print
create an onclick event for this button in vb
Remove Print from the Access built-in File menu - othewise you will get 2 controls trying to use the same accelerator key - better yet - go to options and disable built-in menus and keyboard short-cuts
I want to leave keyboard shortcuts on for other forms, and I want users to be able to use ctrl-v and ctrl-c for copy and paste, I did remove print from the access built in file menu
I removed the print button from the file menu, I changed the toolbar to a custom toolbar and it still isn't working
raised points to 500, didn't know it was going to be this difficult
If you made your caption &Print - the key combination to make it run is Alt + P

To have Ctrl + P capture - create a custom menu Item and assign Ctrl + P as the keyboard shortcut
I just realized that I led you astray; &P is actually {alt} + P as andrewbleakley states.

I think Ctrl - P is Microsoft application-wide default for print, and prints whatever is active, whether it ends up being 1 page or 100. I'm not entirely certain you can override this.

Sorry for the confusion.
-Jim
Thanks Jim, I believe you are right, I tried what andrew said, and it doesn't seem to be working
ASKER CERTIFIED SOLUTION
Avatar of Jeffrey Coachman
Jeffrey Coachman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Boag, Nice!!! thanks a lot, that worked great!

A couple of questions, that didn't disable the use of ctrl-v and ctrl-c to copy and paste did it (I think I tested it and it still works - which is what I want).

Is there a way to put in the conditions of that macro if the active object is a form or report? because I would (Ideally) like users to be able to press ctrl-p to print reports.
I've used this before to determine if an object is open, can I do it for all forms?

SysCmd(acSysCmdGetObjectState, acForm, "frmDlySummaryRptInfo")


or is there another way to determine if the active object is a form or not?
nevermind, this is what I did, (for completion sake) in the conditions of the macro that I created I put a function name, here's the function:

Function currObject() As Boolean
If Application.CurrentObjectType = 3 Then
    currObject = True
Else
    currObject = False
End If
End Function

Application.CurrentObjectType  will return the following:
acTable (0) The active object is a table.
acQuery (1) The active object is a query.
acForm (2) The active object is a form.
acReport (3) The active object is a report.
acMacro (4) The active object is a macro.
acModule (5) The active object is a module.
acDataAccessPage (6) The active object is a data access page.
acServerView (7) The active object is a server view.
acDiagram (8) The active object is a database diagram.
acStoredProcedure (9) The active object is a stored procedure.

then in the action part of the query I put printout so that it only prints out if the active object is a report!

Thanks for all the help!
yhwhlivesinme,

I see you have been very busy!
:)

If you had made another macro:
^c
Yes, that would have disabled the Copy command.

Similarly:
^v
Would have disabled Paste.

Also note: This will disable the commands in the "Access" environment only. They will still work in the Visual Basic Editor.

I only use this to "Totally" disable a command.
What I found is that many users became confused as to when Ctrl-C would work and when it would not.
And what if they had a Report and Form open at the same time! Most users don't know what "The Focus" of an "Active Object" means!
:)

But I am impressed with your "Macro" (If you could call it that now!)
:)

But anyway....

Glad I could help!