Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Home > TechHelp > Directory > Access > Password Manager 2 > < Password Manager | Install Error >
Password Manager 2
By Richard Rost   Richard Rost on LinkedIn Email Richard Rost   13 days ago

Password Manager & Generator Tutorial Part 2


 S  M  L  XL  FS  |  Slo  Reg  Fast  2x  |  Bookmark Join Now

This is Part 2 of my Password Manager series. In this video, we will walk through adding a password generator button to our Microsoft Access password manager. I will show you how to create a function to randomly generate strong passwords, ensure each password meets strength requirements, validate the results, and properly organize and name the necessary VBA code. We will also discuss using the AreYouSure function to confirm overwriting existing passwords and mention ideas for future features like password strength indicators.

Members

In the extended cut, we will learn how to encrypt your password so that only an encrypted version is stored in your table, and how to have the form automatically encrypt and decrypt passwords on the fly. I will show you how to set this up so that the database only ever saves the encrypted string, and decryption happens as needed, adding a layer of protection for your stored passwords.

Silver Members and up get access to view Extended Cut videos, when available. Gold Members can download the files from class plus get access to the Code Vault. If you're not a member, Join Today!

Prerequisites

Links

Recommended Courses

Learn More

FREE Access Beginner Level 1
FREE Access Quick Start in 30 Minutes
Access Level 2 for just $1

Free Templates

TechHelp Free Templates
Blank Template
Contact Management
Order Entry & Invoicing
More Access Templates

Resources

Diamond Sponsors - Information on our Sponsors
Mailing List - Get emails when new videos released
Consulting - Need help with your database
Tip Jar - Your tips are graciously accepted
Merch Store - Get your swag here!

Questions?

Please feel free to post your questions or comments below or post them in the Forums.

KeywordsHow To Build A Password Manager & Generator In Microsoft Access Step By Step, Part 2

TechHelp Access, Password Manager, Password Generator, generate random password, VBA password function, password validation, ContainsAny function, copy password to clipboard, password encryption, Nz function, AreYouSure function, password strength indicator, VBA random number, uppercase lowercase number special character

 

 

 

Comments for Password Manager 2
 
Age Subject From
3 daysAes255 Encryption is Next to Impossible to CrackJeff Shepard
12 daysShowhide ButtonLen Jolly
12 daysPassword Strength Part 3Tom Petersohn
12 daysRe: Clearing the ClipboardMaggie M
13 daysPassword Generator Part 3, a Definite YesBj Schroeder
13 daysPlease Do a Part 3Dave Clark
13 daysYes, to Part ThreeChris Tyson
13 daysYes to the Part 3Jeffrey Kraft
13 daysPassword GeneratorRoy Dey
13 daysPassword Strength IndicatorKevin Robertson
13 daysPassword Manager Part 3Darrin Harris
13 daysYes! a Part 3Len Jolly

 

Start a NEW Conversation
 
Only students may post on this page. Click here for more information on how you can set up an account. If you are a student, please Log On first. Non-students may only post in the Visitor Forum.
 
Subscribe
Subscribe to Password Manager 2
Get notifications when this page is updated
 
Intro This is Part 2 of my Password Manager series. In this video, we will walk through adding a password generator button to our Microsoft Access password manager. I will show you how to create a function to randomly generate strong passwords, ensure each password meets strength requirements, validate the results, and properly organize and name the necessary VBA code. We will also discuss using the AreYouSure function to confirm overwriting existing passwords and mention ideas for future features like password strength indicators.
Transcript Welcome to another TechHelp video brought to you by AccessLearningZone.com. I'm your instructor, Richard Rost. This is part two of my Microsoft Access Password Manager and Generator video series.

If you haven't watched part one, what are you doing here? Go watch part one first and then come on back so you'll know what we're doing.

All right, so yesterday we got our show and our copy buttons done. Now we're going to make the generate password button. That's going to be the fun one.

We're going to design view. We're going to copy this, copy, paste. We're going to stick it right here. This one's going to say "generate" on it. Generate.

Now before we can actually put the button code in here, we need to actually write a function to generate the password. I don't like doing complicated stuff like that in the buttons themselves. So anywhere, let's go right below it. We're going to say public function. So we have function, generate password as a string.

What does a function mean? Well, a function means it's going to return a value. It's going to return a string value. If you want to learn more about generating your own functions, go see this video. I probably should have put this in the prerequisites yesterday, but I was only thinking about yesterday's stuff. So if you want to learn more, go watch this.

The first thing we have to say is how long of a password do we want? Let's make that a constant. Constant password length as a long equals however long you want it to be. 15, 10, easily changed if you put it right there.

Now we're going to have some lists of characters. I want three separate lists of characters: uppercase characters, lowercase characters, and numeric characters. Yes, we're going to have them stored as characters, and then the special characters. So we need to set up variables for all of those things.

I'll be honest, I already wrote this off camera before and I'm not going to make you sit here and watch me type. So I'm just going to paste it in.

All right. Uppercase characters, upperChars, lowerChars, numberChars, and specialChars. Now we're going to define all of those ourselves just like this.

What's the characters? This is ABCDEF, whatever, all the uppercase characters. There's all the lowercase characters. Here's all the numbers. And here's all the special characters. If you want to take stuff out of here, go right ahead.

For example, on my website, I have a character generator like this when a user signs up for a new account, depending on what order form they use, like some of my special order forms and stuff. To join the learning connection, it just gets your name and address and your billing info, and it just sends you a password, like a temporary password. But I take out i's, I take out o's, I take out zeros and ones, and some of these special characters because people type them in and they always confuse an L for a one.

They look nearly identical, especially in this font. So if you want to take stuff out of here, go right ahead. It won't matter. But for a copy-and-paste computer password on a website, it's just fine. You want to confuse people who are stealing your passwords.

Now we're also going to combine all four of these into one long monster string so that we have our complete set of characters to pick from. So we need one more variable here that's going to be allowedCharacters. We're going to combine all of those into one big long string. AllowedCharacters is basically all four of those put together, so we've got a big long list of stuff to pick from.

Now, since this is going to involve picking random characters from inside these strings, we want to initialize the random number generator. And we do that with a command called Randomize. I can't type today. You don't want to see me type all this. We'd already be at minute 20.

If you want to learn more about generating random numbers in VBA, go watch this video. It's really cool. We build a random number generator for a Dungeons and Dragons dice roller, which is really fun. I had a lot of fun making this one, so go watch this if you want to learn more.

Technically, you only need to call Randomize once when your application starts, but it's fine here. It's just going to run it every time you click the button, not a big deal. It just initializes the random number seed in the computer's memory based on the system timer. You shouldn't run into any problems putting it here. If you want to put it in your database startup, that's fine too. Usually it's only a problem with automated applications that run, especially if your computer restarts at a certain time; a long story. I think I talk about it more in that video. But that's fine - good enough for government work.

Another variable we're going to need is going to be our newPassword. We're going to set newPassword equals blank. Start it at nothing. Now we're going to build the password.

The algorithm here is we're going to basically have a for loop. We're going to go from one to the password length that we specified up here. For each time, we're going to say, I need a random character from inside this string of allowedCharacters, which is however many characters long.

So whatever that is, pick a number from one to X, X representing however many characters there are. Then I want you to find the Xth character inside that long string. Makes sense? If it's one to, let's say, 60 characters there and it randomly picks 37, count and find what the 37th character is, add it to my password string, and do that 15 times.

We need two more variables, and then we're done with the variables. We need L as a long for our loop counter and a random number to represent the index of the character inside here we want to grab.

Now we're going to build the password. Again, I'm just going to copy and paste for you.

For L equals 1 to PasswordLength, so 15 times, for example. RandomIndex, my random number, is going to be Int(Rnd * Len(allowedCharacters)) + 1. We learned about that in the random number video. So that's however many, whatever the length of the allowed character string is, that's what your random number is going to be, from 1 to 60, or from 1 to 55, or whatever this combination up here happens to be.

Since Rnd is zero to one, we've got to add one to it. Let's say there's 60 characters up here. It's going to be from 1 to 60. So we've got a number for the random character inside that string to pick. Now we need Mid of that string, random index, 1. If random index is 10, it's going to count to the 10th character in here, which is J, and bring back one character. It's going to grab that J and add it to newPassword. It will do that 15 times.

Now when we get down to here, we've got that string. Let's just see what we've got so far. I'm just going to message box newPassword just to make sure what we got here is working. We'll just put generate password in that button. You don't have to call it as a function if you don't want to; it's just going to call it. In here, we're going to message box this. Let's see if what we've got so far works.

I like to stop at good spots in my code to test to make sure that what I'm doing so far works. Debug Compile. All right, no in-your-face errors. Close it. Save it. Open it.

All right, when I click the generate button, okay. That looks good. It looks like a password. Do it again. Okay. Yeah. I'm happy with it. Looks like it's working.

Now, odds are with that complete amount of randomness and that number of characters, these are all going to be valid passwords that come up. It is extremely rare to get one that's not valid. But as programmers, we always have to be thinking about edge cases. Edge cases are that one time where it doesn't do it. You get all capital letters back, and then the website's going to yell at you and say your password is not valid.

How many times has that happened to you? We have to check that stuff. That's the next thing: we're going to check and get rid of this message box. Now we're going to check. We're going to validate the password.

And speaking of validation, give me some validation. If you like what I'm doing, if you like my work, give me a little bit of support. Just hit that like and subscribe. That's all you have to do. It helps me, it helps the channel, and gets my videos shown to more people. It gets me some more subscribers, and hopefully, I can keep doing this. I really enjoy it. Thanks for your support.

In here, we're going to check to see: does this password contain any uppercase characters? Does it contain any lowercase characters, and so on? Numbers, special characters. If not, we're going to just rerun the whole password. Is it the best way to do that? No. There are more efficient ways to do it. But for the purposes of this database, that's perfectly fine. We're just going to run the whole thing again because the chances of getting one non-valid password are astronomical. I could do the math, but I do not feel like it. This loop is not going to run more than twice, trust me.

Here's what this code's going to look like. If not ContainsAny(newPassword, uppercaseCharacters), then GoTo RetryPassword.

A couple of things here. What's ContainsAny? We're going to write that. We're going to write a real simple function that takes the newPassword and passes the character set in it. Just make sure that this password contains at least one of those characters. We're going to write that in a minute.

GoTo RetryPassword. Where is that? We're going to put that in. We're going to slide that right here with a colon after it. Boom. Now we've got a jump spot. If it gets down here and the password doesn't contain an uppercase character, it's going to go right back to here and start over.

Like I said, it's not going to do that more than two times. You'd probably have to let this run for years before it came across two consecutive invalid passwords.

If we do get past all four of those, that means we have a valid password. Now we can set GeneratePassword, which is the name of our function, equal to newPassword. We can exit with that.

Now we have to write ContainsAny. Again, I'm not going to make you watch me type. I already wrote this earlier. Here's a private function. Private means only this form can use it. It's called ContainsAny. It's going to return a Boolean, a true or false value. It's going to take in a text value and then a character set.

The text value is this guy, the password. The character set is whatever set we're looking through: numbers, letters, special characters, whatever. Same thing. We're going to loop from one to the length of the character set. If it's the numbers, it's going to be 10. If it's the letters, it's going to be 26, whatever. We're going to have to check each character.

If InStr(textValue, Mid(characterSet, i, 1)) > 0. Remember, InStr returns a number indicating the position of a character inside a string. If it's zero, that means it doesn't exist. If it returns a number greater than zero, it does. InStr function, one of my favorite functions. Use it all the time.

As soon as you see it, then ContainsAny = True, exit the function. I always like to default ContainsAny = False to start because we're going to exit out here. If we find the character, we set it to True and exit out. If not, always default your function return value. I didn't default my value for my function when I first wrote it.

This parameter here is actually optional; that's if you want to start at a location other than one, but I usually don't put that there. I was testing it with some other values earlier on. This should do it.

Debug, Compile. All right. Let's do it. Ready? Click. And show. Oh, we didn't set it. Oh, all right. The button is working. It's doing all of this. We're returning it to here, but we're not setting it anywhere. We're doing just Generate, and that's it. It's just sticking around in memory and then it goes away.

So we need to say here Password = GeneratePassword(). It's considered proper programming to put empty parentheses like that to indicate that's a function.

Then hey, when we're done, let's copy that password to the clipboard because chances are you're going to want to generate it and then paste it right into a website. So let's turn this stuff here into its own subroutine.

Now since we're doing that, I'm going to actually name this button. So we're going to come back out here. This is the copy button. Right-click, design view, copy. We're going to change this to the CopyPassword button.

Now watch what happens though. When you right-click on this, it's empty. What happened? Where did my code go? You've got to bring your code down. Where's the code? All of this stuff here, we have to cut this out and come down here and put it inside this subroutine now. CopyPasswordButton_Click.

You can just call this if you want, but I try not to do that. So what we're going to do is, we're going to say CopyPassword End Sub, and then we're going to make a Private Sub CopyPassword. So now that's just called CopyPassword. Now right in here, we just say CopyPassword.

This is the generate password button, which you could name if you want to. Let's do it. Why not? Let's name it. Let's name them all: WritePasswordButton. Right-click, build event.

Now we're going to take that GeneratePasswordButton stuff, cut it out, and then paste it in here.

Now we have all our stuff named properly. Let's go back up top here and delete these empty ones. Goodbye. Sometimes when you compact and repair, it will get rid of those empty subs, but not always.

Everybody's good. Save it. Debug, Compile. Come back out here. Close it. Open it. Generate. Show it. There we go. Let's generate another one. Show it again. Generate. We have to change that button there too. I think at the end of CopyPassword, we should hide it again. Once you copy it, hide it. Let's see. That's in the button. Let's take a peek. CopyPassword. Actually, it already does that. We just have to put this in here. There we go.

Now one more thing that I want to do is, before we go ahead and regenerate the password again, I want to make sure that the user is sure that they want to do that if there is already a password in there. That's where my AreYouSure function comes into play that I mentioned last time. It was one of the prerequisites that we didn't use last time, but we are going to use it right now. So in here, we're going to stick that.

Basically, we're going to use the Nz function (NullZero). We're going to say, if there is something in password, then ask the user if they're sure they want to replace the current password.

How this works is Nz converts a null value into an empty string. So this now becomes either the password itself or an empty string, then we can just check it against the empty string. This is a faster way. You do not have to check for both null and empty string; you just do that. I have been doing this a lot lately.

My AreYouSure function is basically a message box. If they say no, then it exits before it does all that stuff. If we come out here and we try to generate a password again: oops. Are you sure? I mentioned it, but we didn't get it. Where is it? It's in the code vault. That's why we should debug, compile before we try running stuff. Debug, compile. Oh, can't do it now. You have to make a change and then back over it, and now we should be able to debug, compile again.

There we go. Sub or function not defined. Let's go get it. Let's go to my AreYouSure page. It's right there. If you're a Gold member, you can download the database, or you can go into the code vault. I think I made this one free. Let's see here. Where is code vault? AreYouSure? Here it is right there. It's not free in the code vault, but you can just copy it off the screen. There it is. I'm going to hit copy.

This is best if you place it in a global module so you can use it everywhere in your database. I'll just stick it right there. Now we should be able to debug, compile, and everything works.

Back here. Now I can go generate. This will replace the current password. Are you sure? I'm going to say no, and it does not do anything. But if I say yes, okay. Then show it. That's a different one.

Let's go to Jimmy Kirk. Generate him a new password. Show it. All right, looks good. Oh, that one was very close to not being valid. It just had that one special character in it.

One thing I thought about doing: if you want to type in your own password, is make that little indicator like you have seen before on different websites, where you have little indicators like: okay, it's got at least one uppercase character, at least one lowercase character, and so on. I just did a quick Google search - something like this: at least this many characters long, uppercase, lowercase, number, etc.

If you want to see something like this built in here, it would not be that hard to do. Let me know, and if enough people are interested, post a comment down below. Maybe I'll make a part three and we'll do that in there. That would be kind of fun.

Now, in the extended cut for the members, I'm going to teach you how to encrypt that password in the table itself. The only thing that ever gets stored in the table is a random (well, seemingly random) series of numbers. That's the actual password for your website, but that's what gets stored in the table. We will have the form encrypt and decrypt it on the fly. When the form loads up, you load that record, it will decrypt the password if you want to see it. When it's generated, it's going to encrypt it automatically and save that in the password, so the regular password itself is never saved in the table.

If someone gets a hold of your back end file, they get that password table - nope. As long as they do not get your front end, because then they can just open up the front end and figure out what the encryption method is. But this is good enough, like I said. It's not military-grade encryption, but it's good enough to keep Sue from accounting out of your password list.

That's going to be in the extended cut for the members. Silver members and up get access to all of my extended cut videos. All of the members can download these databases that I have built in the TechHelp videos. Everybody gets some free training, and there is lots and lots of cool stuff on my website and on my YouTube channel for members. So check it out.

Today's big takeaway is you can turn Microsoft Access into your own password manager and even generate strong passwords automatically with just a little bit of VBA.

Post a comment down below. Let me know if you plan to use this and what you thought of today's video. If you want to see that little screen indicate the strength of your password, we can do that too. If enough people are interested, we'll make a part three.

That's going to do it for today. That's your TechHelp video for today, brought to you by AccessLearningZone.com. I'm Richard Rost. I hope you learned something.

Live long and prosper, my friends. I'll see you next time.
Quiz Q1. Why does Richard prefer to write complex code in a separate function instead of directly in the button's Click event?
A. To reduce the amount of code in the form
B. To improve code readability and reusability
C. Because Access does not allow code in buttons
D. To make the button look cleaner

Q2. What is the purpose of the GeneratePassword function?
A. To copy the password to the clipboard
B. To create a new, random password
C. To validate the user login
D. To check if the password is stored

Q3. Why are certain characters like i, o, 0, and 1 removed from the character sets for password generation?
A. They are considered too insecure
B. They can be easily confused for one another
C. They make passwords longer than necessary
D. They slow down the password generator

Q4. What is the purpose of the Randomize command in the password generator code?
A. To shuffle the user list
B. To reset the password count
C. To initialize the random number generator for better randomness
D. To enforce password validation

Q5. Why does the password generator combine all character sets into a single allowedCharacters string?
A. To simplify the code and allow random selection from all possible characters
B. To make passwords easier to read
C. To avoid using numbers in passwords
D. To split passwords into sections

Q6. What is checked during password validation before accepting a generated password?
A. That the password is not already in use
B. That the password contains numbers only
C. That the password contains at least one uppercase letter, one lowercase letter, one number, and one special character
D. That the password is exactly fifteen characters

Q7. What happens if the generated password does not contain the required types of characters?
A. The program exits with an error
B. The user must manually add missing characters
C. The generation process repeats until a valid password is created
D. Random characters are added at the end

Q8. What does the ContainsAny function do?
A. Checks if a string contains only numbers
B. Checks if a text value contains at least one character from a given set
C. Combines uppercase and lowercase characters
D. Encrypts the password

Q9. How does the InStr function help in the ContainsAny function?
A. It determines if a character is uppercase
B. It checks the length of the password
C. It locates whether a character from the set exists in the password
D. It converts the password to uppercase

Q10. What is the purpose of the AreYouSure function?
A. To ask the user if they want to copy the password
B. To validate the password format
C. To confirm with the user before replacing an existing password
D. To encrypt the password

Q11. Why is the Nz function used before calling AreYouSure?
A. To convert a null password value to an empty string for easier checking
B. To encrypt the password field
C. To reset the password field
D. To generate a random password

Q12. What is the recommended place to put the AreYouSure function for maximum usefulness?
A. Inside each form only
B. In a global module so it is available throughout the database
C. Only in the password form
D. Nowhere, it is not recommended to use

Q13. If a user clicks Generate and there is already a password filled in, what will happen?
A. The old password is deleted automatically
B. The user is prompted if they are sure they want to replace it
C. The new password is added to the old one
D. The new password is ignored

Q14. According to the video, what is a potential future feature Richard may add if enough people are interested?
A. Export passwords to Excel
B. A password strength indicator showing what requirements are met
C. An auto-login functionality
D. Military grade encryption

Q15. What is planned for the extended cut for members?
A. Export password lists
B. Encrypting the password in the table so only the encrypted value is stored
C. Adding new fonts for passwords
D. Allowing for blank passwords

Answers: 1-B; 2-B; 3-B; 4-C; 5-A; 6-C; 7-C; 8-B; 9-C; 10-C; 11-A; 12-B; 13-B; 14-B; 15-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 video from Access Learning Zone continues our series on building a Password Manager and Generator in Microsoft Access. If you have not yet watched part one, I recommend doing that first to ensure you are up to speed with what we have already covered.

In the previous lesson, we finished the show and copy buttons for our password manager form. Now, our focus is on creating a button to generate a password - a key feature that brings some fun into the project.

To get started, I head into design view and prepare the new Generate button. Rather than writing the password generation logic directly into the button's code, I build a separate function to handle the details. In Access VBA, a function is a procedure that returns a value, in this case, a string representing our newly generated password.

We first decide how long the password should be. This length is stored as a constant, making it easy to adjust. Next, we prepare lists of allowable characters, splitting them into uppercase, lowercase, numeric, and special characters. This separation is important, both for password variety and ensuring compliance with password complexity rules.

If you want to customize which characters are used, you can remove confusing characters, such as 'i', 'o', '0', and '1', which are often misread. This is something I often do for users on my website when generating temporary passwords or when a high level of clarity is needed.

Once the character lists are set, we combine all of them into a single string. This aggregated list, named allowedCharacters, gives us the full pool of choices for each character in the password.

Since the password generation process relies on randomness, we initialize the random number generator using the VBA Randomize command. This ensures that each password we produce is unpredictable. While Randomize only needs to be run once per application session, there is no harm in having it here, particularly for a simple tool like this.

To actually build a password, we use a loop: for the total number of characters specified by our password length, we pick a random index from the joined character set and grab the corresponding character, appending it to our growing password string. By the end of the loop, we have a candidate password.

Before finalizing the password, it is important to test that our code is functioning as expected. I always advocate for compiling and testing code at logical breaking points to catch errors early. Once the process is working, we verify that the password meets our expectations.

However, there are some rare edge cases to consider. For instance, occasionally the randomly generated password may not include all character types required by certain websites: an uppercase letter, a lowercase letter, a number, and a special character. To account for this, I add a validation step using a ContainsAny function. This function checks whether the password contains at least one character from each required set. If it does not, we simply generate a new password. This brute-force approach is not the most efficient technically, but for our purposes, it is more than sufficient and exceedingly unlikely to ever require more than one retry.

The ContainsAny function itself is straightforward. Given a password and a character set, it loops through each character in the set and checks, using the InStr function, if the character appears in the password. If so, it returns True; otherwise, it returns False.

After ensuring our password meets all criteria, the next step is to streamline the code by consolidating related tasks into subroutines, such as copying the password to the clipboard. This makes the code more organized and maintainable, and it also ensures that actions such as hiding the password after it is copied are handled consistently.

An additional usability feature is to confirm with the user before overwriting an existing password. Here, I use a function called AreYouSure, which presents the user with a message box if a password is already present. The Nz function simplifies the process by converting null values to empty strings so that we can do a single check before proceeding.

If the AreYouSure function is missing from the project, it is easy to add it from the code vault or from one of my training pages. I recommend placing it in a global module so it is available throughout your application.

For further user experience enhancements, I have considered including visual indicators of password strength or compliance as seen on many modern websites, and I may include this in a future video if there is enough interest.

In the extended cut of today's lesson, available to members, I cover advanced topics such as encrypting the stored passwords in your table. The encryption ensures that only the front-end can decrypt and display the actual password for you. This approach is not meant to be military-grade security, but it is effective for preventing casual snooping by other users who may have access to your back-end data.

All in all, this project demonstrates how you can build your own password manager in Microsoft Access and generate strong, complex passwords automatically with just a bit of customized VBA.

If you found this useful or plan to use these techniques, feel free to leave a comment or suggestion. I am always interested in your feedback and ideas for future lessons.

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 Creating a Generate Password button in Access

Creating a VBA function to generate passwords

Defining character sets for password creation

Excluding ambiguous characters from passwords

Combining multiple character sets into one string

Initializing the random number generator with Randomize

Building passwords using random character selection

Validating passwords for required character types

Writing a VBA ContainsAny function for validation

Using GoTo for password regeneration on failure

Assigning generated passwords to a form field

Copying passwords to the clipboard with a subroutine

Renaming command buttons and their event handlers

Prompting user confirmation before replacing a password

Utilizing the Nz function for null and empty string checks

Importing and using the AreYouSure function for prompts

Debugging and compiling to ensure code reliability
Article In this tutorial, we will build a password generator for Microsoft Access using VBA. This is the next step after creating buttons to show and copy passwords in an Access password manager. Now, we want a button that automatically generates strong passwords made up of uppercase and lowercase letters, numbers, and special characters.

To keep things organized, let's start by creating a new button on your Access form called "Generate." Switch to design view, copy one of your existing buttons, paste it, and change its caption to "Generate." It's a good idea to give your button a meaningful name in the properties window, like "GeneratePasswordButton." Now, it's ready for some VBA code to run whenever you click it.

Instead of writing a big chunk of code inside the button's click event, we are going to write a reusable function named GeneratePassword, which returns a password string. This is best practice in VBA, making your code easier to maintain.

Here's a simple version of the password generator function:

Public Function GeneratePassword() As String

Const PasswordLength As Long = 15

Dim upperChars As String
Dim lowerChars As String
Dim numberChars As String
Dim specialChars As String
Dim allowedCharacters As String
Dim newPassword As String
Dim l As Long
Dim randomIndex As Long

upperChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
lowerChars = "abcdefghijklmnopqrstuvwxyz"
numberChars = "0123456789"
specialChars = "!@#$%^&*()-_=+[]{}|;:,.<>/?"

' You can remove confusing characters like O, 0, I, l, and 1 if you want

allowedCharacters = upperChars & lowerChars & numberChars & specialChars

Randomize

RetryPassword:
newPassword = ""
For l = 1 To PasswordLength
randomIndex = Int(Rnd * Len(allowedCharacters)) + 1
newPassword = newPassword & Mid(allowedCharacters, randomIndex, 1)
Next l

' Check that password contains at least one char from each category
If Not ContainsAny(newPassword, upperChars) Then GoTo RetryPassword
If Not ContainsAny(newPassword, lowerChars) Then GoTo RetryPassword
If Not ContainsAny(newPassword, numberChars) Then GoTo RetryPassword
If Not ContainsAny(newPassword, specialChars) Then GoTo RetryPassword

GeneratePassword = newPassword

End Function

You will also need a helper function to check whether the new password includes at least one character from each group. Here is the ContainsAny function:

Private Function ContainsAny(textValue As String, characterSet As String) As Boolean
Dim i As Long
ContainsAny = False
For i = 1 To Len(characterSet)
If InStr(textValue, Mid(characterSet, i, 1)) > 0 Then
ContainsAny = True
Exit Function
End If
Next i
End Function

Now, in your "Generate" button's click event, you'll want to call your GeneratePassword function and put the result into your password field on the form. For example:

Private Sub GeneratePasswordButton_Click()
If Nz(Me.Password, "") <> "" Then
If Not AreYouSure("This will replace the current password. Are you sure?") Then Exit Sub
End If
Me.Password = GeneratePassword()
End Sub

The Nz function handles cases where the password field might be null. The AreYouSure function is a simple confirmation message box that you can define in a module like this:

Public Function AreYouSure(Optional prompt As String = "Are you sure?") As Boolean
AreYouSure = (MsgBox(prompt, vbYesNo + vbQuestion, "Confirm") = vbYes)
End Function

Remember, if you want the password copied to the clipboard automatically after it's generated or when you click a "Copy" button, you can create a subroutine like this:

Private Sub CopyPassword()
If Not IsNull(Me.Password) And Me.Password <> "" Then
Dim objData As Object
Set objData = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
objData.SetText Me.Password
objData.PutInClipboard
Set objData = Nothing
End If
End Sub

Call CopyPassword from any button's Click event as needed.

As you develop and test, always use Debug > Compile to check for errors, and save your progress often. After implementing these changes, open your form and try generating new passwords. The password will meet basic security requirements: it will always include at least one uppercase, one lowercase, one numeric, and one special character. You can tweak the constants at the top of the function to change the length or adjust which characters are allowed.

Additionally, if a password already exists in the field, the function asks for confirmation before replacing it, adding another layer of safety.

This setup gives you a basic but effective password management tool in Microsoft Access. It creates strong, random passwords and ensures that any password generated meets the typical requirements for online services. If you're interested in adding features like password strength meters, or encrypting the stored passwords in your database so only the program can display them, those enhancements are also possible with further VBA work.

With just a bit of code, Microsoft Access becomes much more than a database tool. It can serve as your own password manager, helping protect your online accounts with unique, hard-to-guess passwords generated on demand.
 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 5:14:38 PM. PLT: 0s
Keywords: TechHelp Access, Password Manager, Password Generator, generate random password, VBA password function, password validation, ContainsAny function, copy password to clipboard, password encryption, Nz function, AreYouSure function, password strength indicat  PermaLink  How To Build A Password Manager & Generator In Microsoft Access Step By Step, Part 2