Is Folder Open
By Richard Rost
9 months ago
Check if a Folder is Open Before Opening in Access
In this Microsoft Access tutorial, we will learn how to efficiently detect if a folder is already open to avoid cluttering with multiple copies of the same folder. I'll guide you through creating a custom function to return a true or false value when checking if a specific folder is open. Using the AppActivate command, we can activate an open folder instead of repeatedly opening new ones. Additionally, you'll learn how to adjust your Windows settings to display the full folder path in the title bar, essential for distinguishing between folders with similar names.
Samantha from Riverside, California (a Platinum Member) asks: How can I check if a folder is already open in Microsoft Access before trying to open it again? I keep accidentally opening multiple copies of the same folder and it's getting really messy. Is there a way to detect if it's open so I don't end up with a bunch of duplicate windows?
Members
There is no extended cut, but here is the file download:
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
Recommended Courses
Keywords
TechHelp Access, detect open folder, is folder open function, app activate command, create custom function, global module, Windows taskbar settings, display full path title bar, shell application object, check if folder is open, folder duplicate windows, VBA file explorer, switching open folders, function Boolean return value, debug compile code, VBA code vault, working with file paths, shell Windows collection, module for database functions
Subscribe to Is Folder Open
Get notifications when this page is updated
Transcript
Yesterday, I showed you how to open a folder for a specific customer. Today, I'm going to show you how to detect if a folder for that customer is already open so you don't keep opening multiple copies of it. Today's question comes from Samantha in Riverside, California, one of my platinum members. Samantha says, how can I check if a folder is already open in Microsoft Access before trying to open it again? I keep accidentally opening up multiple copies of the same folder, and it's getting really messy. Is there a way to detect if it's open so I don't end up with a bunch of duplicate windows?
Yes, of course, there is. And a lot of people ask me how did I know to make this video for today? How did Samantha know that yesterday's video was opening the folder? Well, a lot of these questions come from my full course that's years and years old. So sometimes I'll get a bunch of questions that are similar and group them together like I'm doing with this one and tomorrow's one too. In fact, I think this question was asked before the other one, and so I'm kind of putting them together in the order that I think they were the best. So, all right, how do we do this?
First up, as always, some prerequisites. Go watch yesterday's open folder video if you have not watched it already. Watch my video on for each loops. We're going to create a custom function that'll return a true or false value if that folder is open already. So make sure you watch this video, so you understand how to create your own function. And go watch this video called app activate. This is the command we're actually going to use to open that window if it's already open. It's called app activate. I'm going to show you how to use it in just a minute.
These are all free videos on my website. They're on my YouTube channel. But watch all of those and then come back. All right, here we are, and by my database folder, let me bring that back up here. There we go. Now, for this to work, you have to change a Windows setting that shows the full path up here. Let me move this down so you can see it, the full path to that folder in the Windows Explorer folder name on the taskbar. Where is that? Well, come into file explorer, go to options, go to view, and you have to have this right here display the full path in the title bar. This guy up here, and it'll also show it down in your Windows taskbar on the bottom.
This is necessary, otherwise, if you don't have that, then that's just my database. It's just the name of whatever folder you're in. We need the full thing to be able to open this. Because otherwise, if you got two folders called one or two folders with any particular name, it won't know which one to open. This ensures that it's got the exact name there because what we're going to do is we're going to use app activate. And if this guy exists, we're going to open it. But app activate will only activate the window if it's already open. It won't open it for you. So we're still going to have to open it with the other code from yesterday. But we need to somehow determine if it's open or not. So do we open another copy, or do we just switch to the one that's open?
Let's go back into our database from yesterday and go to our customer form. Here's our open client folder button. Let's go to design view, right-click build event. Go back into here. All right, here's our code we wrote yesterday. Now, one minor modification we have to make is we have to get rid of that trailing backslash because this does not have it. And we have to match exactly what shows up in this title bar here. Let me see if I can get it back. Come here. There it is. See, there's no trailing backslash. It's got to be exactly what shows up there. I discovered this as I was poking around running through this beforehand. So just come in here and get rid of this. This should not affect opening the folder at all. It should still open it. It'll see it as a folder.
What we're going to do down here, we're going to leave this code alone. We're still going to determine what the file path is, if it doesn't exist, create it. But then after that, down here, we're going to say, hey, is it open? Is it already open? If it is, then we're going to have to activate. If not, we're going to shell it and open it up. But before we can do that, we need our function called is folder open. Now, you can put that in here if you want to write in this customer F. Remember the customer F code. This is called a form module, and it only exists for this form. So if you want the entire database to be able to use it, come over here and find a global module. If you don't have one already, you can go to create and then module, not class module, regular module. But I already have one. So I'm going to open up my global module. This is where all the functions that work everywhere in the database go. And right down here, I'm going to put my is folder open function.
Now, you guys don't want to sit here watching me type, so I'm just going to copy and paste it from my code vault. Here's the code vault. I'm going to come down here and copy it. By the way, gold members get access to this. There's all kinds of stuff in this code vault, lots of neat stuff. We're going to come back up here, and I'm going to paste it in. Anytime you ever copy and paste some code from somebody else, first thing you should always do is debug compile it. Make sure it compiles. I don't know how many times I've wondered why something wasn't working. And I didn't bother to compile it, and they got maybe a variable spelled wrong or something's missing. So always compile it first.
Gold members, you can copy and paste it. Everybody else, get typing or screen OCR or whatever you got to do. This is a public function. Public means the whole database can use it. Function means it's going to return a value to whoever called it. The value is going to be a Boolean. So this is going to return a true or false value. We're going to dim two objects, O and W and D. We're going to set O equal to create an object shell.application. It basically says it's going to create an object that represents a Windows file explorer window. Basically, in a nutshell, that's not exactly right, but okay, that's good enough for right now.
Now, for each object in that shell Windows collection, all of the open windows that are basically open on your Windows desktop, each one is going to get assigned to W and D one at a time. Now, if W and D dot name equals file explorer, if the name of that window is file explorer, then we're going to take a closer look at it. Note in some different versions of Windows, it could be Windows Explorer. I'm using Windows 11. I think Windows 10 is the same. If you go back to like XP or Windows 7, it might be Windows Explorer. So you have to change that.
Now if we get any here, and I see my tabbing wasn't perfect, let me fix that. Oh, I'm very meticulous with my tabbing. Sometimes when I copy and paste stuff to my web onto the website, it doesn't get it exactly. So we'll shift tab and then tab it back. That's much better. Your indenting is important folks. Access doesn't care about it, but I care about it. I don't know how many times I've missed an end if because I didn't tab in right.
If we get to this line, we now know that the window that we're looking at is a file explorer window. So now we're going to see if it's named what we're looking for. Folder is the name we send into it. That's that big long name off the tab. It's going to be WND document that folder that's self that path. Why that's just what the people who made windows did. That's what they call it. It's a window. It's document property has a folder property as a self and then a path property. Yeah, okay. I don't remember this stuff either folks. I have to look in my notes too or Google it. You're not going to remember this. That's what the code vault is for.
If the name of that window, if it's title in the title bar is the same as what we're looking for, that means that the window we're looking for is open and we can exit the function. Otherwise, if you go through all of the open windows and if you want some fun, message box all of these and you'll see all the different windows that are open. If it gets to the end and doesn't find it, is folder open is false and it'll exit out. And it'll return a false.
Let's go back over here. What I do is I just click on this little button here. And then it opened up. It took me back to the code window that was behind it, which is the customer app. Right here now. We can say if is folder open file path. That's the file path that we're looking for. Then app activate. File path. Otherwise, open a new one. And that's it. Check to see if this file path is open already if you see its title in the title bar of all the windows file explorers that are open. If so, app activate it. If not, open a new one. Save it. Another debug compile, please. Debug compile wants it a while. Let's come back out here. Close it. Open it. Click the button. There's folder one. I'm just going to slide it down over here now. And click it again. Oh, look at that. It switched to it instead of opening up another one. Because it sees it as one of the open folders. Let's go to somebody else. Let's go over here to. I'm going to move this out of the way so you can see that this folder is still back here. I hit that. It just brings it forward. Now if I go to a different client. And hit that. It opened another folder because that's a different folder name. This guy is folder two. Come here. Give me. There it is. See? Two different windows. Now if I go back to folder one or client one hit the button. It brings that one forward. If I go to this guy and hit it, it brings that one forward.
You can use this technique to say, hey, is this application? Is this specific folder open? If so, bring it forward. If not, open up a new one. See how cool that is? There you go. That's not hard. See, a lot of this stuff, a lot of this stuff, you know, this is folder open stuff. This is stuff that you're going to write once. I researched this. I had a client that I needed to do this for like, I'm going to say, 15 years ago. And I figured out how to do it from books and I think Google at the time. And you're going to write it once and you're not going to have to write this again. This is not stuff that you remember. Certain things, yeah, you're going to do a lot like deal up statements, SQL statements, even like record sets. Certain things you're going to do over and over and over again. And it's just to become second nature stuff like this. No, not so much. Even I have to look this kind of stuff up. So that's okay. That's what I got the code for.
And if you like learning this stuff, I got tons and tons of VB courses. A lot of them deal with working with files in Microsoft access. File input output reading and writing files, dealing with directories, folders, permissions, all that stuff. Even up to the advanced stuff like working with a file system object lots and lots of lessons on this stuff. Of course, I'll put links to all of this down below.
But that's going to do it folks. That TechHelp video for today. Hope you learned something. Live long and prosper my friends. See you next time.
TOPICS: Detect if a folder is open in Windows Enable full path display in Windows settings Modify VBA code to match folder path Create custom VBA function 'IsFolderOpen' Use 'AppActivate' to switch to open window Implement code in MS Access form module Use global module for reusable functions Debug and compile VBA code for errors āϰliberate on 'IsFolderOpen' function logic Test solution by opening and switching folders
COMMERCIAL: In today's video, we're discussing how to prevent opening multiple copies of a customer's folder in Microsoft Access. First, I'll show you how to detect if a folder is already open using a custom function. We'll modify our existing code to ensure it works smoothly by removing the trailing backslash and matching the folder's path exactly. You'll learn how to activate an already open folder window with the app activate command or open a new one if needed. This technique is user-friendly and saves time by avoiding duplicate windows. You'll find the complete video on my YouTube channel and on my website at the link shown. Live long and prosper my friends.
Quiz
Q1. What is the primary goal of the video tutorial? A. To open a specific customer folder in Microsoft Access B. To check if a folder is already open in Microsoft Access and avoid duplicating it C. To delete unnecessary folders in Microsoft Access D. To create a new database in Microsoft Access
Q2. Which prerequisite video is NOT mentioned as necessary before proceeding with the task in the tutorial? A. Open folder video B. For each loops video C. App activate video D. Introduction to VBA video
Q3. What Windows setting must be changed to ensure the folder path appears in the title bar? A. Enable file extensions B. Show hidden files C. Display the full path in the title bar D. Use small icons on the taskbar
Q4. What does the "app activate" command do in the context of the video? A. Opens a new file explorer window B. Checks if a folder is open and opens a new one if it is not C. Activates the file explorer window if it is already open D. Deletes duplicate folders
Q5. Where should the "is folder open" function be placed to make it accessible across the entire database? A. Customer form module B. Global module C. Class module D. Standard form
Q6. What type of value does the "is folder open" function return? A. Integer B. String C. Array D. Boolean
Q7. When checking if a folder is open, why is the exact title in the title bar important? A. To ensure the file explorer can create a new folder B. To differentiate between folders with the same name C. To open the folder in a new window D. To save the folder path automatically
Q8. How are the open windows looped through in the "is folder open" function? A. Using a while loop B. Using a for loop C. Using a for each loop D. Using a do-while loop
Q9. What is a suggested method to test if the pasted code from the code vault is correct? A. Run it immediately to see if it opens a folder B. Use a spell checker on the code C. Debug compile the code D. Ask a colleague to review it
Q10. What aspect of code formatting is emphasized as important in the tutorial? A. Color-coding variables B. Proper indenting and tabbing C. Using short variable names D. Minimizing comments in the code
Answers: 1-B; 2-D; 3-C; 4-C; 5-B; 6-D; 7-B; 8-C; 9-C; 10-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 Access Learning Zone addresses a common issue users face when they inadvertently open multiple copies of the same folder in Microsoft Access. Samantha, a platinum member from Riverside, California, posed an intriguing question: how can one check if a folder is already open before attempting to open it again, to avoid creating duplicate windows?
Yes, there's a way to handle this, and today we'll explore how to achieve it. It's worth noting that the topics I cover often stem from recurring questions in my courses, which have been running for many years. This particular question naturally follows yesterday's tutorial on how to open a folder, arranging them in what I believe is the most logical sequence.
Before we proceed, there are some essential prerequisites to cover. If you haven't already watched the previous session on opening folders, I recommend you do so, along with my video on "for each" loops. Today, we're creating a custom function that will determine if a folder is already open by returning a true or false value. Watching my "app activate" command video is also crucial since this is the command used to focus on an already open window.
Now, to make this work, there's a Windows setting you need to change so that the full path to the folder is displayed in the Explorer's title bar and on your taskbar. Without this setting, the system can't differentiate between folders with the same name. To change it, navigate to File Explorer options, under view, and select the option to display the full path in the title bar.
Incorporating this setting allows us to use the app activate command effectively. This command won't open a new window but activates an already open one. We'll still need yesterday's code to open folders when necessary but first must determine if the folder is open.
Returning to your database, access the customer form, specifically the open client folder button. Modify the existing code to ensure it matches the folder's title as it appears without a trailing backslash. This match is crucial for functionality. Down in the code, after determining the file path, the system will check if the folder is open. If it is, app activate will bring it forward. If not, a new instance is opened.
To support this, we need a function called is folder open, found in a global module that can be accessed database-wide. If you lack such a module, create one in the module section, not class module. I'll place my is folder open function here. This function uses shell.application to loop through open windows and check if any match the folder's full path. If found, it returns true; if not, false.
It's essential to remember to debug compile any pasted code to avoid errors. The function is public, meaning any part of the database can call it, returning a true or false Boolean value regarding the folder's open status. This function traverses all open windows to check each window's title against the folder's path. If a match is found, it signals that the folder is open; otherwise, it returns false.
Implement the function in your existing customer form code to determine if the folder is open. If it is, app activate it; if not, proceed to open a new folder. Always debug compile after changes for smooth execution. Testing the functionality should show that the system switches to an open folder if available or opens a new one if not, distinguishing between different client folders by name.
This utility avoids clutter and enhances efficiency by managing window instances, underscoring how once-infrequent tasks can streamline routine processes. If interested in more of this, I offer extensive VBA courses focused on file management within Access, covering everything from basics to advanced topics like file system objects.
For further insight, a comprehensive video tutorial with step-by-step instructions on all we've discussed is available on my website at the link below. Live long and prosper, my friends.
Topic List
Detect if a folder is open in Windows Enable full path display in Windows settings Modify VBA code to match folder path Create custom VBA function 'IsFolderOpen' Use 'AppActivate' to switch to open window Implement code in MS Access form module Use global module for reusable functions Debug and compile VBA code for errors āϰliberate on 'IsFolderOpen' function logic Test solution by opening and switching folders
|