AppActivate
By Richard Rost
4 years ago
Switch Between Open Applications With AppActivate
In this Microsoft Access developer tutorial, I'm going to show you how to use the AppActivate command to switch between open applications so that you can SendKeys to them.
Members
Gold members can download a copy of my database.
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!
Pre-Requisites
Links
Recommended Course
Keywords
access 2016, access 2019, access 2021, access 365, microsoft access, ms access, ms access tutorial, #msaccess, #microsoftaccess, #help, #howto, #tutorial, #learn, #lesson, #training, #database, #fasttips, change the focus to another application, Switching to a specified open application window, activate a program, Switching Between 2 Open Apps, VBA and switching focus between applications, Run Other Applications, Tabbing between different windows
Subscribe to AppActivate
Get notifications when this page is updated
Intro In this video, I will show you how to use the AppActivate command in Microsoft Access to switch focus between open applications, such as Notepad and Calculator, using VBA. We'll start by reviewing how to open external programs with the Shell command and send keystrokes using SendKeys, then see how AppActivate helps control existing windows without opening new copies. I'll demonstrate step-by-step how to activate specific applications by title and discuss some limitations when using task IDs. We'll also cover best practices for using pauses with the Sleep command to ensure smooth automation.Transcript Welcome to another FastTips video brought to you by AccessLearningZone.com. I am your instructor Richard Rost. In today's video, I'm going to show you how to switch between open applications using the AppActivate command in Microsoft Access.
A couple of weeks ago, I released the SendKeys video. In that video, I show you how to open another application from Access and then you can use the SendKeys command to send keystrokes to it as if you were typing at the keyboard. So we learned how to use Shell to open up Notepad and send keys to it as if you were typing.
I've gotten a lot of emails since then from people asking, "Well, what if Notepad is already open?" I don't want to open another copy of it. I want to go back and forth between Access and Notepad or between two different applications. How do I do that?
Well, there's a command called AppActivate that lets you activate another application. Basically, it sends focus to that application. So in this video, I'll show you how to do that.
But first, this is a developer video. What does that mean? That means you're going to need to know a little bit of VBA. So if you've never done any programming before, go watch this video. It's absolutely free. It's on my website. It's on my YouTube channel. It'll teach you everything you need to know to get started programming in VBA in about 20 minutes. Go watch that and come back if you've never done any VBA before and you want to learn.
Also, go watch my Sleep video. We're going to need to have a sleep timer between commands because you can't just open up and switch between different applications and send keys immediately. You've got to open it up, wait a second, then you can send some keys. Wait another second and then switch back. If you do it all without pauses in there, it causes problems. So go watch this Sleep video too and get that code.
Here I am in my TechHelp free template. This is a free database. You can download a copy up on my website if you want to. But for this example, you can use pretty much any database that you want. Just go to design view. Let's just repurpose this button here. We'll make this my AppActivate button. Let's right-click, go to build event, and open up the code builder. Here we go.
Now, in the last class, we learned how to open up Notepad and that's just going to be Shell and then notepad.exe, comma, and you want to use vbNormalFocus so it opens up in the foreground and has focus. You don't want to minimize. You could open it maximized if you want to. Remember, if you're using an application that's not part of Windows, chances are you need the full path in here. If you want to open up your web browser or Microsoft Word or something like that, I talk about all that fun stuff in the Shell video.
So now I can go in here, save changes, open it back up again, click the button and then Notepad pops right up. It actually opened up on my different monitor so I had to move it over here. Let's do it again. There we go. Here's the last spot where you opened and closed it.
If I want to send some text to it, I like to wait a second. I didn't cover the Sleep function in the other video, but usually if you're just opening up one application, you can send keys right to it. But just to be safe, I always do a Sleep 1000 and sleep one second. Those are milliseconds. Then send your keys.
So, SendKeys "Hi, this is Captain Kirk." Save it, come back over here, click, and there you go. See, it opened it, it waited a second, and it sent the keys.
Now let's say after that we want to open up Calculator. So let's go back over here. Again, I'm going to Sleep 1000. Shell and it's calc.exe. Again, part of Windows, vbNormalFocus. Wait a second, and then we're going to send keys. Let's make it add up four plus five. So we need 4. Now, plus is a special character for SendKeys, so it's got to be inside curly braces, and then 5 and then equals. We're going to send that and hopefully it'll be displaying 9. Sleep another second, and then we're done. Save that. Come back in here. Hit the button.
I must make sure Notepad is closed first. Close this. Don't save changes. Ready, here we go. Fresh start. Go. There's that. Hi Captain Kirk. And it opened up Calculator and gave me a 9. This opened up on my other screen too. Let's do it one more time. Ready, go. Perfect.
Now, if I issue another Shell command for Notepad, it's going to open up another copy of Notepad. Watch this. Let's close these both down again. Let's do the same thing again. Just going to copy that, come down here, paste in "This is Mr. Spock" and let's put some entries in here too. Enter, like that. Put a couple entries in. Then I want them on top of each other.
This should open up a second copy of Notepad. Ready, here we go. There's Captain Kirk, Calculator, second copy, there's Mr. Spock.
That's not what we want though. We want both of those to be in the same instance of Notepad. So we're going to use AppActivate to activate this application.
Once again, close these down. Save changes, no. Save changes, no. Close that. Now, in order for this to work, you have to have at least part of the application name. If you open up Notepad, you get "Untitled - Notepad." The AppActivate command does its best to guess what application you want. The best thing you can do is give it this exact caption. Whatever appears down here on your title bar is the caption. You want to try to give it that exactly if you can, but if you can only give it "Notepad," that usually works or the beginning of it, for example.
So what I'm going to do is instead of shelling Notepad, get out of here, I'm going to say AppActivate "Untitled - Notepad." That's the exact caption. I've tried it and just "Notepad" works fine too. We'll talk about these other parameters in just a minute.
Let's see if this works. Ready? I'm back over here, click. There's Captain Kirk. Go over to Calculator, type in that. Go back to Notepad and put in Mr. Spock. See that? Now they're in the same instance of Notepad. Let's go back to Calculator now. Close this, save no, did it. Let's go over to Calculator.
So, Shell, AppActivate, and this is just Calculator. That's what appears in the title bar. Then we'll do something similar. Let's do 2000 plus 555. Save it. One more try. Here we go. Go, Captain Kirk, 4 plus 5 is 9, Mr. Spock, and back to Calculator again for a big one. There you go. And you're done. It's that simple.
Maybe throw a Beep at the end. Beep. If you want two beeps, put a Sleep in the middle of them. If you do two beeps by themselves, you won't hear it. Sleep 1000, and then beep again. I'll talk about that in the Sleep video.
Now, a couple things you have to know about AppActivate. First of all, let's go over to the Microsoft page on that. Here's Microsoft's page on AppActivate. I'll put a link to this down below. You can click on it. Go read this if you want to.
Here we are on Microsoft's page for AppActivate. A couple things you have to know right here. First of all, "title." It's a string expression specifying the title in the title bar of the application you want to activate. We've already covered that.
Now, you can get something called a task ID returned by the Shell function to use in place of "title." However, in my experience, this doesn't always work. The Shell ID is extremely notorious for not working, at least in my case and in my history. I always use the caption.
Let me show you. For example, you're supposed to be able to set up an ID for each of your applications that you open. Let's go
Dim NotepadID as Variant Dim CalcID as Variant
A variant basically means you don't know what the data type is. It can hold any data type.
Now here we'll say
NotepadID = Shell("notepad.exe", vbNormalFocus) CalcID = Shell("calc.exe", vbNormalFocus)
Now, since we're returning a value from Shell, Shell is one of those things that can be both a function and a sub. If you're just calling it as a sub, you don't need the parentheses. But since we're going to be returning a value from it, we do need to put parentheses around it. That's going to get whatever Shell returns to NotepadID. It returns a number representing that window. You can message box it to see it if you want to.
Now down here, instead of saying AppActivate "Notepad," you can just say AppActivate NotepadID, like that. Let's see if it works. We'll still call Calculator the old fashioned way. Ready? Here we go.
Click. OK, open it up. Put the Captain Kirk. Goes over to the Calculator and it finds it just fine and goes back over and puts Mr. Spock in. No big deal.
Now it's the same thing with Calculator over here. OK, we got CalcID. We're going to say here same thing,
CalcID = Shell("calc.exe", vbNormalFocus)
So it's going to get that return value of whatever Shell brings back. Put it in CalcID. Then instead of AppActivate "Calculator," we're going to AppActivate CalcID. You'd think that should work just fine.
Here we go. Wait for it. Back to Notepad. And there's your error right here: Invalid procedure call or argument, debug, that line throws it. There's the number. That's what Shell returned. Why this doesn't work, I don't know. In the past, I've spent hours and hours trying to figure this out and I can't. So I just always stick with the caption.
If one of you knows why this happens, let me know and I'll make a video about it and give you full credit because I have no clue.
But I'm going to leave both of these scenarios in here for the Gold members when you download this database. So what we'll do is we'll undo that, and we'll go back to just the caption. I'll get rid of CalcID.
I'll put in here, "Calling ID doesn't work. Why? No idea." Seriously, I have no idea. It gets the ID just fine from Shell, but it doesn't work with AppActivate.
But anyways, like I said, I've always used the caption and I've never had a problem with it. You can even guess at the caption and it gets it half the time. Maybe it has to do with having two or more applications being in use, I don't know. But I've had that same problem happen with only one application that I'm shelling to, so it's not just the fact that it's Calculator, it's not the fact that it's got two of them. I don't know what it is.
There's another parameter that, again, I've never used. I never had a need to use it. This basically says that if the calling application, in other words, Access, doesn't have focus, it waits until Access has focus to activate the other application. Again, I've never had a need for this one, so I've never used it.
Here are some more examples down here. They use the application ID.
That's pretty much it. So there you have it. There's how you can use Shell to open an application, SendKeys to send keys to it, Sleep to wait, put proper pauses, and AppActivate to go back and forth between them.
If you guys really like this stuff, because I've gotten a bunch of feedback on this, I didn't think so many people were going to be interested in this stuff. If you want to see more, let me know. Post a comment down below.
I personally, even though I don't use SendKeys in Access because there really is no need to, I do use Access to control other applications. I've got a routine like this that I built that opens my web browser, launches my bank website, logs me in automatically, and actually gets my account information. So there's all kinds of things you can do with this.
I'm happy to make more videos on it if you guys are interested. Post some comments down below.
And yes, I know that it's not the most 100 percent reliable way to go about working with web stuff. You want to use an API if an API is available, but not every web service has an API available. Some websites you can open up in a web browser control in Access and control them with JavaScript, but again, some websites don't offer that either. So like my bank website, it's impossible to do anything except just control the browser using SendKeys and AppActivate and all this stuff. If you guys want to learn more about this stuff, let me know.
That's about it. If you like this kind of stuff and you want to learn more about developing applications with Microsoft Access and programming and VBA and all that good stuff, check out my developer lessons. I've got right now up to 42 lessons on all kinds of different stuff. I start from the beginning: Level 1, Level 2, Level 3. We go right through it so you don't have to jump around between different videos. I cover pretty much everything there is to know about developing in Access. Check them out.
There's your fast tip for today. I hope you learned something and we'll see you next time.Quiz Q1. What is the main purpose of the AppActivate command in Microsoft Access VBA? A. To open a new instance of an application B. To activate or bring focus to an already open application C. To close an application D. To minimize all open applications
Q2. When using the Shell function to open an external application like Notepad, which parameter ensures the window opens in the foreground? A. vbMinimizedFocus B. vbNormalFocus C. vbHidden D. vbMaximizedFocus
Q3. Why is the Sleep function often used between commands when automating applications with Access VBA? A. To improve application security B. To provide time for one application to open or process before sending commands or switching applications C. To conserve system resources D. To ensure data is saved automatically
Q4. Which command is used in Access VBA to send keystrokes to applications? A. Shell B. AppActivate C. SendKeys D. InputBox
Q5. What happens if you use Shell to open Notepad multiple times? A. Only one instance opens B. It activates the previous instance C. It opens a new instance each time D. Nothing happens
Q6. When activating an application using AppActivate, what string should you ideally provide? A. The application's installation path B. The file path being edited C. The title or caption in the application's title bar D. The application's publisher name
Q7. What is the issue described in the video when using the Shell function's task ID with AppActivate? A. The ID is required for AppActivate to work B. The ID only works with Microsoft Office applications C. The ID often does not work reliably with AppActivate D. The ID always opens a new instance instead of switching
Q8. What data type is recommended for storing the return value of the Shell function when attempting to use AppActivate with the ID? A. String B. Integer C. Variant D. Boolean
Q9. If you want to send the "+" character using SendKeys in VBA, what must you do? A. Type "+" normally B. Place it in single quotes C. Place it inside curly braces D. Convert it to a number
Q10. The video mentions using Access VBA automation for tasks such as: A. Manipulating large datasets in Excel only B. Automatically logging into websites and gathering account information C. Creating custom controls D. Restricting user input to forms
Q11. Why is using captions generally recommended over using the Shell task ID for AppActivate? A. Caption is more secure B. Caption is easier to remember C. Captions are more reliable and almost always work D. Task IDs are only available in Windows 11
Q12. What is required knowledge before attempting the techniques shown in this video? A. SQL queries only B. Advanced Access Macros C. Basic familiarity with VBA programming D. Hardware configuration
Q13. What is stated as a possible limitation when using SendKeys and AppActivate on websites? A. They cannot interact with any website B. Only available with APIs C. Some websites do not have APIs or cannot be controlled with JavaScript/web browser controls, so automation may be required D. They work with every modern website
Q14. If you want to make two separate SendKeys commands noticeable (audible beeps) in succession, what should you do? A. Place them in a loop B. Use two Beep commands with a Sleep in between C. Use MsgBox after each beep D. Only use one Beep
Q15. What do curly braces in SendKeys represent? A. String delimiters B. Special characters or combinations that require escaping C. Variable assignments D. Loop indicators
Answers: 1-B; 2-B; 3-B; 4-C; 5-C; 6-C; 7-C; 8-C; 9-C; 10-B; 11-C; 12-C; 13-C; 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 focuses on how to switch between open applications in Microsoft Access using the AppActivate command. My goal in this lesson is to help you gain control over moving focus between Access and other applications that are already running, without launching duplicate copies.
A little while back, I created a video on using the SendKeys command. In that tutorial, I demonstrated how you could open another program, such as Notepad, from within Access using the Shell command. Then you could send keystrokes to Notepad as if they were typed on your keyboard. After that video, many people wrote to me asking how to handle scenarios where, for example, Notepad is already open and you do not want to start a new instance, but rather switch back and forth between it and Access, or even between two other applications.
The key command for managing this kind of workflow is AppActivate. AppActivate lets your VBA code give focus to an already-running application by referring to its window title. This is great for orchestrating back-and-forth activity between programs, such as sending data to Notepad, then over to Calculator, and so on.
Before we get rolling, I want to remind everyone that this lesson is intended for developers, so you'll need some basic familiarity with VBA. If you are brand new to programming, I recommend watching my free introductory VBA video on my website or YouTube channel, which takes about 20 minutes to cover the essentials. It's a good primer before diving into this kind of material.
Additionally, you should watch my Sleep function video. Adding pauses between commands is critical when: opening new programs, sending keystrokes, or shuffling focus. If you try to run multiple actions in sequence too quickly, you'll likely run into problems where one program isn't ready yet to accept input. By inserting suitable pauses, you avoid these timing issues.
For this tutorial, I'm using my TechHelp free template database, which you can download from my site if you want to follow along, though any Access database will suffice. Once your database is ready, head into form design and use a button to trigger the demonstration. Inside the event code for your button, you'll recall from earlier lessons how to launch Notepad using Shell, specifying normal focus so the window appears front and center. A quick note: when opening other applications outside standard Windows programs, you'll often need the full file path.
Following this approach, you can open Notepad, wait a moment using the Sleep command, and then use SendKeys to enter some text automatically. The Sleep function ensures Notepad is ready to receive keystrokes by pausing your macro for a set number of milliseconds.
If you want to open Calculator afterward, repeat a similar pattern: launch Calculator with Shell, pause with Sleep, and send the desired keystrokes. For Calculator, keep in mind that some operators, like plus, have special SendKeys syntax requirements, such as enclosing them in curly brackets.
What happens if you issue another Shell command for Notepad? You end up with a second copy of Notepad, each running independently, and your script continues as if each were separate. This is usually not ideal if you want to continue working in the same window.
That's where AppActivate comes in handy. AppActivate lets you bring an already-open application forward by specifying its window title (the exact text in the title bar, such as "Untitled - Notepad"). While AppActivate can often match on a partial title, providing the full caption ensures accuracy, especially when dealing with multiple windows of the same program.
By switching over to use AppActivate instead of launching a new instance, you can direct commands and keystrokes to the original window. In Access, after you've activated Notepad with AppActivate, your SendKeys statements will act on that existing window. The result is a seamless flow, with all your data input going into a single Notepad session instead of several duplicates.
You can repeat the same process for Calculator, using "Calculator" as the window title for AppActivate. This way, you can hop from Notepad to Calculator, and back again, all within your VBA procedure.
As an extra touch, you can use the Beep command to provide audio feedback at the end of your actions. Just remember to space beeps with a Sleep pause, otherwise, you might not hear them individually.
Now, there are a few nuances to know about the AppActivate command. According to Microsoft's documentation, you can use the window's title or the task ID (Process ID) returned by Shell when you open an application. Theoretically, storing the result from a Shell function and passing it to AppActivate should work, but in practice, the task ID is unreliable for many Access users, myself included. It's always worked best for me to reference the window title directly. If you discover the reason behind this inconsistent behavior with task IDs and manage a solution, let me know! I would be happy to make a dedicated video about it and credit you for the find.
There is another parameter for AppActivate, designed to wait until Access itself has focus before shifting control, but I've never had the need for this feature in my work.
In summary, combining Shell, SendKeys, Sleep, and AppActivate gives you powerful control over launching applications, inputting data automatically, and efficiently switching focus between programs without creating unwanted duplicates. I've personally built elaborate routines that use these techniques to automate web logins and interact with programs that don't have robust APIs or integration tools.
If you are interested in deeper coverage of Access development VBA, check out my comprehensive series of developer lessons, which build systematically from the basics up through more advanced material, all logically sequenced.
For a complete video tutorial with step-by-step instructions covering everything discussed here, visit my website at the link below. Live long and prosper, my friends.Topic List Using the Shell command to open external applications Sending keystrokes to external applications with SendKeys Pausing execution between commands using the Sleep function Activating an existing application with AppActivate Passing exact window captions to AppActivate Switching between multiple open applications Manipulating Notepad and Calculator from Access VBA Handling special characters with SendKeys Using Shell to return a task ID for an application Limitations of using task IDs with AppActivate Assigning Shell return values to variables Troubleshooting AppActivate errors Fallback strategies for activating applications Overview of AppActivate parameters and usage Integrating Shell, SendKeys, Sleep, and AppActivate in VBA
|