Controlling Macro Execution with a Password

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

Under some circumstances you may want to provide controlled access to a procedure in your solution. For example, suppose you have a procedure that only you or some authorized user, such as an administrator, should be allowed to run. You can control access to this procedure by creating a simple password logon dialog box that passes the entered password to a procedure that checks the password before continuing. In the simplest case, you use an If statement that checks a hard-coded password against the password entered by the user. If you do this, you should lock the VBA project so that users can't easily view the code to discover the password.

You can prompt for the password by using the VBA InputBox function, but if you want to create a typical input box that is masked to prevent others from seeing the password entered, you need to use a UserForm. You can set the PasswordChar property of a TextBox control on a UserForm to the character you want to use to mask the input; for example, an asterisk (*). For an example of how to prompt for a password and mask the input, see the frmGetPwd UserForm and the CallGetPwd procedure, available in the modGetPwd module in PasswordDialog.dot in the ODETools\V9\Samples\OPG\Samples\CH17 subfolder on the Office 2000 Developer CD-ROM. To run the sample code, place the insertion point (cursor) in the CallGetPwd procedure and step through it by pressing the F8 key.