Disable Printing
By Richard Rost
2 years ago
Disable Printing in MS Access, Part 1: CTRL-P In this Microsoft Access tutorial, I will show you how to restrict printing to only using a designated Print button, thereby disabling CTRL-P and other shortcuts. We tackle intercepting key presses with VBA and setting up user controls, perfect for ensuring proper invoice marking and report management. This is part 1 of 3. Lorenzo from Downers Grove, Illinois (a Platinum Member) asks: I have a user that likes to hit CTRL-P to print reports instead of using my Print button which also runs code to mark invoices as having been printed. I've already disabled the Ribbon and the right-click menu using the techniques you've shown in your Security Seminar and Developer classes. However, is there any way to disable CTRL-P and force him to have to use my Print button? I still want to allow them to preview a report before printing it so they don't waste paper, but I want to force them to use my Print button. PrerequisitesRecommended CoursesUp Next
Keywords TechHelp Access, disable CTRL-P, print button code, VBA key press interception, Access print shortcut, disable Access right-click menu, printing permissions, restrict Access printing, VBA programming Access, disable printing Access, custom workflow Access, Access security features, password protection Access, custom ribbons Access.
Intro In this video, we will talk about how to temporarily disable printing in your Microsoft Access databases by blocking the Control P shortcut as well as the P key so users are forced to use your custom print button. I'll show you how to use the Key Down event and Key Preview property in Access reports to intercept print commands, how to use VBA to suppress printing actions, and how to make sure users can still preview reports without being able to print them directly. This is part 1.Transcript In today's video, we're going to talk about how to disable printing in your Microsoft Access databases. Today's question comes from Lorenzo in Downers Grove, Illinois, one of my Platinum members. Lorenzo says, "I've got a user that likes to hit Control P to print reports instead of using my print button which also runs code to mark invoices as having been printed. I've already disabled the ribbon and the right-click menu using the techniques you've shown in your security seminar and developer classes. However, is there any way to disable Control P and force them to have to use my print button? I still want to allow them to be able to preview a report before printing it so they don't waste paper, but I want to force them to use my print button to actually send the report to the printer."
I get it. I do this in a lot of my databases too. I want the user to be allowed to see the preview so they can look it over and make sure it's okay before they actually waste paper by sending it down to the printer and ink. Of course, you know ink is expensive too. But I do want them to have to click my print button because you can make another button for them like, you know, "Mark this invoice as printed" or "Mark this order as shipped," but people don't click it.
So I get it. Lorenzo, he wants to make it so that when the user prints the actual invoice, it marks it as it's been printed. Okay.
So let's see how we can disable the different ways to print stuff in Microsoft Access without having to use your button. Okay, because you can do it with VBA code very easily.
So what are the different ways to print a report? Let's say once you've got it previewed, right, because I've shown you guys in a million different videos how to actually preview or print a report. It's just do command open report and then the report name and then how you want to print it. You can send it right to the printer or you can open it up in preview mode. But once you put it in preview mode, what's to stop the user from printing it right from there?
How can you print it? Well, you can hit Control P on the keyboard. That's one thing we have to disable. There's the right-click menu, which is this thing. You right-click on the report and you go to print. That's going to bypass your button too. There's the ribbon. The print preview ribbon pops up anytime you go into preview. And of course, there's your own button.
So what we want to do is make our own button and disable these three things. Have a button where they can preview it, right, but they can't print it from there. And then you'll make another button that they can actually print it from.
And I've also seen companies that wanted to completely disable printing altogether. They want to have reports so they can send them as PDFs or whatever, but they don't want stuff being printed and leaving the building. So I get it. But just keep in mind, anything you can screen capture with the screen capture utility, you can print that too. So I mean, that's, you know.
Alright. So in this lesson, let's start off by seeing how we can disable Control P on the keyboard. This is, of course, a developer-level video. What does that mean? Well, that means if you've never done any VBA programming before, go watch this guy. It's about 20 minutes long. It'll teach you everything you need to know to get started. And also go watch this one too, my if-then video. We're going to use an if-then statement. These are both free videos. They're on my website. They're on my YouTube channel. Go watch them and come on back.
Alright. So here I am in my TechHelp free template. This is a free database. You can grab a copy off my website if you want to. And in here, I've got a customer form and customers can have orders. And on your order, you've got a button right here to preview the invoice for that order. And as you can see, you get the print preview ribbon. You can right-click on here and go to print or you can hit Control P on the keyboard and this thing comes up, the print dialog. This is what we're going to disable first, this guy.
How do we do that? Well, we have to intercept the keystrokes in this report. So how do we do that? Well, we go to design view. We bring up the properties for this report. Now on the event tab, you're going to find a couple of keyboard-related events. There's key up, key down, and key press. We're going to use key down. Key down fires when a key is pressed. Key up fires when that key is released. And then key press is something kind of different where you can actually get the ASCII character code that was pressed. We don't want that for now. We're going to focus on key down. Oh, key down. Oh, key? Oh, key. Key down.
Now, here's a tricky thing, and a lot of people don't know this, but in order for this event to fire on either a form or a report, you have to turn this key preview property on. Very important step, don't forget that, otherwise, none of this will work. Why this doesn't default to yes, it's a long story. But now that we have that, we can go to the key down event, go to the dot, dot, dot, that's going to bring up your VBA code builder.
Alright, here we are. Now, the key down event gets two bits of information. The key code, which is a code representing the key that was pressed, and the shift property. Shift will tell you whether control, shift, or alt was also pressed. So watch this. I'm going to just type in here "message box key code." So it's going to message box whatever key was pressed. Okay, that's all for now. Alright, save it. Come back out here. Let's close this and open the report again.
Okay, now make sure you get focused on here. I'm going to press the P key. Alright, 80. So P is character 80. Alright, I'm going to press the A key and oh, see okay, we didn't--we're still going to get this running because we're not interrupting this at all, which we're going to interrupt this in a minute, so hit cancel. Now I'm going to hit the A key. A is 65. See that? Every character, that's the ASCII representation for it, every character has its own key code. If I press the number 1, it's got a key code, 49. If I press A, it's got a key code, 65, and so on.
Okay? And if you press P, which it turns out just P by itself also opens up the print dialog box. I don't know when they changed this. Alright, Control P does it and also just P by itself. So that's new to me. I tried it in a couple of different databases, and it looks like that's the case. So that they must have changed that recently. Okay?
So we have to intercept both P and Control P. Now, back to our code. In order to intercept these key codes, we're going to use the key code in here. So we're going to say now, if key code equals 80, then we're going to say "message box, 'P was pressed,'" and then "end if." So now it should ignore everything except if the P is pressed. All right, you ready? Let's come back over here. Always a good idea to close and reopen these things.
OK, I'm going to press A, nothing. B, nothing. D, nothing. P, there it is. "P was pressed." And it still fires up that thing. Likewise, if I press, let's say, Control P, look at that. "P was pressed" also. Now normally, if you wanted to get just Control P, then you'd come in here and say, "and shift equals 2." Shift being 1 is the shift key was pressed. Shift being 2 means the Control key was pressed. And shift being 3 means the Alt key was pressed. But we don't even need that because we're going to trap all instances of P being hit. Alt P, Control P, and Shift P, any of those.
Okay? Now, instead of saying "P was pressed," in here we're going to say, "don't press Control P, use the Print button instead." Alright, the Print button that you have put on the form. Okay? Now, before that happens, I want to cancel the key code. I want to set the key code equals zero. That's going to effectively stop whatever is going on in the background. Make sense? Okay, ready? Save it. Let's give it a quick debug compile. Come back over here. Let's close that and reopen it.
OK, now come over here, hit A, B, nothing, hit P. Ah, "don't press Control P, use the Print button instead." Oh, okay, hit OK. And now, see, notice that, no dialog. Because we set the key code to 0, and that will stop the key from continuing. The key down event actually runs before the key is processed.
Alright, let's make sure Control P works too. Control P. All right, "don't press P, use the print button instead." Set it to zero. See? Maybe Alt P, let's see. Yeah, that's true. We're shutting down all instances of P, folks. All of the P's are stopped. So if Alt P is something on the menu that you want, then that's going to be blocked too. But if you want to block just Control P, like I said, I don't know when they did that. It used to be just Control P printed something. But now apparently just hitting P by itself will launch the print dialog box. That's new. That's new for me.
Alright. Close that. So essentially what you'll do is you'll give the user two buttons here. You'll give them a print preview button and then an actual print button. And the actual print button will be the one that sends it to the printer when they're all set for it. It'll open up the print dialog box.
Alright. And if you look at the button, we built this in my invoicing video. I'll put a link to that down below if you want to learn how this was built. We refresh the record first so it saves everything to the table in case you have any changes, and then it's do command open report, the name of the report, and then acViewPreview.
Okay, acViewPreview. There are different ones in here. Preview gives you print preview. Normal opens up the print dialog and sends it right to the printer. Okay, and then there's all kinds of other stuff in here too. Alright, we're going to go back to preview. And you're just going to make a second button to actually do the printing.
Okay, so we've covered Control P or just regular P. There are still two more ways your user can print this thing though. They can use the right-click menu or they can use the ribbon. Alright, when you open this thing up, you get this. There's the print dialog right there. Or you can go right-click and then print. How do we shut these off? Well, we'll cover the right-click menu in tomorrow's video, part two.
So tune in tomorrow, same bat time, same bat channel. Or if you're a member, you can watch it right now because members get to watch stuff as soon as it's made. You don't have to wait for it to be released to the general public. It's one of the benefits of being a member. And then in part three, I will show you how to disable the print preview ribbon across the top of the screen. So that's all coming up. And I also cover a lot of stuff like this in my security seminar where I show you how to disable menus and create custom workflows and control who can do what in your database. We set up passwords, all that good stuff. So that's covered in my security seminar.
And in my Access Developer 44 class, I show you how to build custom ribbons and then we continue on with right-click menus and all that good stuff. So lots of stuff in my full courses. But this is part one for disabling printing. Part two will cover the right-click menu. Part three will cover the ribbon. That's going to be your TechHelp video for today.
I hope you learned something. Live long and prosper, my friends. I'll see you tomorrow for part 2.
TOPICS Disabling printing in Microsoft Access databases Disabling the Control P keyboard shortcut for printing reports Leaving the option to preview reports open in Access Forcing users to use a specific print button that includes custom code The importance of a print button that also marks invoices as printed Stopping users from using the right-click menu to print in Access Disabling the ribbon's print preview feature in Access Allowing report preview without the ability to directly print Different ways reports can be printed in Access Using VBA to intercept print commands and disable certain shortcuts Key events in Access reports: Key Down, Key Up, and Key Press Key Preview property's role in Access's key event handling Using the Key Down event to suppress printing shortcuts Intercepting keystrokes using the Key Preview event Handling the KeyCode attribute in VBA for print commands Blocking the Control P command to prevent behind-the-scenes printing Using a message box to warn users about disabled print shortcuts Setting KeyCode to zero to prevent default printing actions Making sure all instances of the P key are intercepted Creating custom print preview and print buttons in Access forms Distinguishing between different viewing modes in AccessQuiz Q1. Why does Lorenzo from Downers Grove, Illinois want to disable Control P for printing reports in his Microsoft Access database? A. Because his database users are wasting ink and paper B. To encourage users to use keyboard shortcuts C. To force users to use his print button that marks invoices as printed D. Because he wants to enable the right-click menu for printing
Q2. What is an essential step to ensure the key event you're programming in Access will work? A. Set the Key Down event to "No" B. Set the Key Preview property to "Yes" C. Always use the Key Press event over Key Down D. Remove all default print buttons from Access
Q3. Which VBA event is used in this tutorial to intercept print commands? A. Key Press B. Key Up C. Key Down D. On Click
Q4. What should be done if you only want to stop the Control P keyboard shortcut and not any other combinations with the P key? A. Set KeyCode to 80 B. Set Shift to 2 C. Remove the message box D. Eliminate the Key Preview property
Q5. What message is displayed to the user when they try to print using a disabled shortcut after implementing the tutorial's VBA code? A. "P was pressed" B. "Printing is not allowed" C. "don't press Control P, use the Print button instead" D. "Key code equals zero"
Q6. After which action does the instructor recommend creating a debug compile of the VBA code? A. Before changing the Key Preview property B. After setting the KeyCode to zero C. Before saving the VBA code D. After writing the message box code
Q7. In the VBA code provided, setting KeyCode equals to what value stops the print command? A. 1 B. 0 C. 2 D. 80
Q8. What is the KeyCode character for "P" according to the video? A. 49 B. 65 C. 80 D. 2
Q9. Which tutorial does the instructor recommend for developers if they have never done any VBA programming? A. The security seminar B. The Intro to VBA video C. The Access Developer 44 class D. The invoicing video
Answers: 1-C; 2-B; 3-C; 4-B; 5-C; 6-B; 7-B; 8-C; 9-B
DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.Summary Today's TechHelp tutorial from the Access Learning Zone focuses on how to disable printing in Microsoft Access databases, particularly so you can force users to use your custom print button. Many of us are familiar with situations where we want users to be able to preview reports—giving them a chance to review documents before sending them to a physical printer—but we also need to track or otherwise process when a document is printed by tying that action to a specific button. Simply relying on users to manually mark invoices as printed or orders as shipped by clicking a separate button often fails, so integrating these functions into your print workflow is important.
The challenge comes when users bypass your controls by using built-in shortcuts and menus. Even if you have already disabled the ribbon and right-click menus, as discussed in my security seminars and developer classes, there are still ways users can print reports behind the scenes. Lorenzo, who submitted this question, asked specifically about disabling Control P to keep users from printing reports except through his designated button. At the same time, he still wants to allow users to preview reports.
Let's run through the various methods a user could use to print a report in Access once they are in print preview mode. Typically, these include using the keyboard shortcut Control P, right-clicking on the report and selecting Print, accessing print functions in the ribbon, or using a custom button you provide. Our goal is to allow users to preview the report but only permit actual printing through your designated print button, not these alternative methods.
It's worth noting that in some environments, organizations may want to go even further and disable all printing, sending reports only as PDFs, for example. Just remember that anything displayed on screen can be screenshot and printed elsewhere, so complete lockdown is never absolute.
Our first step is to disable the Control P shortcut using VBA code. This is a developer-level task. If you have never worked with VBA before, I recommend watching my beginner tutorials on getting started with VBA and using if-then statements. You can find these free on my website or YouTube channel.
Now, to illustrate this process, I am working in my TechHelp free template, a sample database you can download from my site. In this example, there's a customer form linked to orders, and for each order, there is a button to preview invoices. When you use this preview button, the print preview ribbon appears, and you are still able to right-click and select Print or press Control P, which brings up the print dialog. This is what we want to disable.
To block these print commands, we need to intercept keyboard input at the report level. You need to open the report in design view and access its property sheet. Under the event tab, there are three keyboard-related events: Key Up, Key Down, and Key Press. For our purpose, we use the Key Down event, since it triggers when a key is pressed. However, for the Key Down event to work on a report (or form), you must set the Key Preview property to Yes. This is critical; otherwise, your event code will not fire.
When you implement the Key Down event in the report's code, it gives you access to two pieces of data: the KeyCode (which tells you which key was pressed) and the Shift property (which indicates whether Shift, Control, or Alt was held down). For example, pressing the "P" key registers as KeyCode 80, "A" as 65, "1" as 49, and so on. Interestingly, in recent versions of Access, pressing just "P" as well as Control P can bring up the print dialog, so both need to be intercepted.
In your Key Down event code, you check if KeyCode equals 80 (which covers P and all P combinations, such as Control P, Shift P, and Alt P), show a warning to the user telling them to use the Print button instead, and then set KeyCode to zero. Setting KeyCode to zero stops Access from processing the key any further, so the print dialog is never triggered. This interception covers P, Control P, and most alternate key combinations. If you want to allow certain combinations through, you can refine the logic using the Shift argument, but given the recent behavior of Access, it's best to trap all P key events.
After implementing this code and testing it, you will find that pressing P, Control P, or similar combinations is blocked, and users see your warning message instead of the print dialog. This approach ensures users can only print through your custom button, which can then do whatever you need, such as marking the invoice as printed.
To complete your workflow, provide two buttons: one for print preview and one for actual printing. The print preview button lets users review the document without sending anything to the printer. The print button handles preparation—such as saving records—and then sends the report to the printer, perhaps after showing a print dialog, depending on your needs.
We have now addressed blocking Control P (and any P key variations), but users can still print with the right-click menu or from the ribbon. Those will be covered in the next segments. Tomorrow's lesson will focus on disabling the right-click menu, followed by a third lesson on removing the print preview ribbon. If you're a member, you can watch these right away as early access is one of the benefits of joining.
If you are interested in more details about database security, user interface control, or custom ribbon development, I cover these topics thoroughly in my security seminar and advanced developer classes.
You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below.
Live long and prosper, my friends.Topic List Disabling printing using VBA in Microsoft Access Disabling the Control P (and P) keyboard shortcut Allowing users to preview reports without printing Forcing users to use a custom print button Ensuring print button marks invoices as printed Identifying all printing methods in Access reports Using the Key Preview property in reports Configuring the Key Down event for print suppression Intercepting and handling the KeyCode in VBA Displaying a message box when print is attempted via shortcut Setting KeyCode to zero to block printing Demonstrating effects of intercepting the P key Explaining the difference between acViewPreview and acViewNormal Adding separate preview and print buttons to forms
|