Fitness 40
By Richard Rost
3 months ago
How to Ignore Arrow Key Presses in Combo Box Events In this Microsoft Access tutorial I will show you how to improve your fitness tracking database by handling up and down arrow key presses in a combo box, using temp vars to save the database state and prevent unwanted changes when navigating through the list of options. This is part 40. MembersThere 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!
PrerequisitesLinksRecommended CoursesUp Next
Keywords TechHelp Access, ignore arrow keys combo box, key down event, temp var, save state, up arrow key, down arrow key, FoodCombo_Change, After Update, union query, combining tables, tracking meals, combo box navigation, key code 38, key code 40, prevent value overwrite
Subscribe to Fitness 40
Get notifications when this page is updated
Transcript
Today's part 40. Can you believe we're up to 40 of these already? Part 40 of my fitness database series. Whether or not you care about tracking fitness, this is a video about building databases. Databases first; fitness is just the backdrop.
Today we're going to see how to ignore arrow key presses in a combo box because we've got some other stuff going on in that combo box. If the user starts typing in chicken and then presses the down arrow, it messes everything up. Today we're going to fix that.
In yesterday's class, part 39, we went over the key down event, so make sure you watch that one and then come back and watch this one. Yesterday we fixed the tab order. It's still today for me. This is yesterday's video, so let's get rid of that guy since I didn't actually eat that. Now let's tackle something a little more difficult.
I mentioned yesterday we've got a similar problem down here. If I type in chicken and then hit the down arrow, it populates that value into the box, erases the other items, and I can't go below that. What I want to do is say, if the user hits the up or down arrows, I don't want to do the stuff where I replace the text in the box and drop the box down again; just ignore all of that.
The only way to do that is if we save that state. The database needs to know between the time it gets to the key down event and the actual change event that handles all that, so we have to be able to say, you need to ignore that. You can save that state. We can do it in a module-level variable, in a hidden form field, or in my favorite, temp vars.
Let's use a temp var. Some people say temp vars are overused. I disagree, and I really like temp vars because you can store pretty much anything in them as far as regular values go, and they save state even if you get an error in your database. I like to use them for startup values and things like that because if you do pop an error, you don't lose all your setup variables. You don't have to restart the whole database. Whereas with global variables, even module-level variables, they get erased.
The first thing to do is to indicate whether or not the user has hit the key down or key up (the up arrow or the down arrow) because they're navigating through the options in the box. For this guy's key down event, we're going to come in here and say: if this user hit up or down (now I don't know what those key codes are and I don't feel like Googling them), here's what we're going to do. Watch this.
We'll just say status. The key code is key code, just like that. We're going to have the database tell us what keys the user is pressing. It's that simple. I don't ever Google this. I just know vbKeyTab because I use it a million times and I remember it. But there's vbKeyInsert and a bunch of others. I don't know and I don't care. Just click here.
Now, if I hit the A on the keyboard, look at that: key code 65. If I hit the space bar, that's 32. I remember those. If I hit the up arrow, it's 38. If I hit the down arrow, it's 40. Where 39 went, I don't know. Okay, 39 is left and right. I don't care about left and right. All I care about is up, which is 38, and down is 40. If you see those characters, don't change anything in the box. Just ignore it. That way the box stays open. It keeps the same values in it. We don't have to worry about all this.
Now we know we need 38 and 40. If key code equals 38 or key code equals 40, then (and we'll say this is the up and down arrows), now we need to pass up to the key change event: hey, don't do anything. Temp vars: let's call it arrow key pressed equals true. End if. So this will tell on change to not process the keystroke.
Now let's go find on change. Where are you? Right here: FoodCombo_Change. So this is where we initialize that other temp var, because remember I said last time: if you're going to check a temp var, you have to make sure it's initialized first. We'll do the same thing with this guy.
What did we call that? I forgot already: arrow key pressed. If arrow key pressed is null, then set it to false. And while we're thinking about it, at the very end of this too, we're also going to make sure we set it to false as well as we process it, to return it to its initial state.
Now in the middle here, here's the After Update. We're just resetting the box to all items. Else if my temp var key pressed is true, then what do we do in here? Nothing. User pressed up or down arrow. Do nothing. Get rid of that. You don't have to put anything in there. I'm just putting that in there so it doesn't run the rest of this code.
Save it. Debugging a file once in a while. Close it. I'm going to close this and reopen it just so it's a clean, fresh start. Now if I come in here and I drop this down, everything works as it normally does.
Now if I come in here and type in chicken, and I hit the down arrow now, look at that. It doesn't modify the box and I can scroll up and down between these items. Because the temp var said, hey, the user is pressing the down arrow key. Don't do the rest of that stuff I told you to do before.
Now I can come down here to fish, salmon, hit enter, enter, and it goes up on the list. Now I can type P and which P, oh, it's that P, enter, enter. See how we just made this thing so much easier to use. Fruit? Oh, it's pears. Enter, enter. See, these are the things I'm discovering as I'm working with this.
I did not eat all of this, so we're going to delete, delete, delete, delete. I thought about making it so we could select a bunch of items and then hit delete once, but these delete so fast, I'm not worried about it. Except for making videos for you guys where I've got to delete the stuff I didn't actually eat, I haven't really had a big need. Sometimes I thought about leaving on the record selector so I could select four of them and hit delete, but it's not a problem. It's not an issue. I find that record selectors cause more problems than they solve.
That was a short one for today. Tomorrow I promise we're going to start meals. I know I found a couple of other issues I wanted to fix before we did, but tomorrow, tomorrow being Wednesday the 24th. I know, don't pay attention to this. I'm way ahead with recording right now. Well, not way ahead. I'm a week ahead. We're going to start tracking meals.
What we're going to do is I've given this a lot of thought. Instead of making a second combo box where this one's got food in it and the second one's going to have meals in it, no, we're going to put it all in one box because this box is already doing some really cool stuff. We're going to use a union query, put all of that together in one query, and then put the stuff up here based on what it is. If it's a meal, it'll drop five items up here.
If you want to study ahead before tomorrow's video drops, go watch my union query video. It will explain how union queries work. You can take information from multiple sources (multiple tables or queries) and put them together so they look like they're in one query. Then we can use that to feed the combo box. But then we have to know what it is once it's in there, and there are some tricks we're going to play. Go watch this before tomorrow's video drops.
That's going to do it for part 40, folks. See you back tomorrow for part 41 and we'll start with meals - put meals in the little box thingy. That's your TechHelp video for today. Hope you learned something. Live long and prosper, my friends. See you tomorrow.
TOPICS: Ignoring arrow key presses in Access combo boxes Using the KeyDown event to detect up and down arrows Getting key codes for keyboard events in VBA Storing state with TempVars in Access Conditionally processing combo box Change events Initializing and resetting TempVars in VBA events Preventing unwanted text replacement in combo boxes Allowing navigation within combo box dropdown using arrows Maintaining combobox values when navigating with arrows
COMMERCIAL: In today's video, we're continuing with part 40 of the Access Fitness Database series. You'll learn how to ignore up and down arrow key presses in a combo box so users can scroll through options without accidentally overwriting text. I will show you how to capture the key down event, use temp vars to keep track of the arrow keys, and make sure the combo box works smoothly without interfering with your data entry process. We'll wrap up with a look ahead to combining foods and meals in a single combo box with a union query. 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 was the primary issue being solved in the video with the combo box? A. Allowing users to select multiple items at once B. Preventing arrow key presses from disrupting text entry C. Saving changes automatically after selection D. Enabling auto-complete for user input
Q2. Why did pressing the down arrow key in the combo box cause problems? A. It caused the database to save changes prematurely B. It erased other items and changed the text in the box C. It closed the application unexpectedly D. It prevented data from being saved
Q3. What solution was proposed for keeping track of whether an arrow key was pressed? A. Using module-level variables B. Using a hidden form field C. Using temp vars to save the state D. Storing the value in the combo box
Q4. Why does the video creator prefer temp vars over module-level or global variables? A. Temp vars work only in forms and not in modules B. Temp vars can store any values and persist even after errors C. Temp vars are easier to type D. Global variables require more memory
Q5. Which key codes correspond to the up and down arrow keys, as found through debugging? A. 32 and 65 B. 38 and 40 C. 39 and 41 D. 50 and 60
Q6. In the video, what is the purpose of setting a temp var called arrow key pressed to true? A. To activate a special search feature B. To prevent regular processing of the combo box when an arrow key is pressed C. To display a message to the user D. To reset the combo box to default values
Q7. When is the arrow key pressed temp var set to false in the process? A. Before processing the new selection B. Only during form initialization C. Never, it stays true until the form is closed D. At the end of the combo box change processing
Q8. What is the anticipated next step for the database in the following video? A. Adding more food items to the combo box B. Implementing the ability to track meals C. Creating graphical reports of progress D. Changing the color scheme of the database
Q9. What method is planned for combining food and meal items into the same combo box in the future? A. Copying and pasting items manually B. Using a union query to combine sources C. Adding a second combo box for meals D. Importing data from an Excel spreadsheet
Q10. Why does the presenter choose not to let users select multiple records at once for deletion? A. The combo box cannot support multiple deletions B. Deleting individual items is already quick enough C. It is impossible in Microsoft Access D. It requires too much code
Q11. What does a union query allow you to do? A. Restrict access to sensitive records B. Combine data from multiple tables or queries into one result C. Automatically update related tables D. Back up the database on a schedule
Q12. According to the video, what role does the combo box play in the fitness database? A. It is used to log exercise routines B. It stores only user contact information C. It is the interface for selecting foods or meals D. It tracks daily calorie totals
Answers: 1-B; 2-B; 3-C; 4-B; 5-B; 6-B; 7-D; 8-B; 9-B; 10-B; 11-B; 12-C
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 focuses on part 40 in my fitness database series. Even though the topic is built around a fitness app, the lessons here are about advanced database techniques. Fitness just serves as the context for our learning.
Today's problem revolves around handling the arrow keys in a combo box. When users start typing, say "chicken," and then use the arrow keys to navigate the list, it can disrupt the combo box's behavior. Pressing the down arrow can end up replacing the box's contents and removing other choices, which is not what we want. The goal for today is to prevent the box from changing its content when the user navigates up or down with the arrow keys.
In the previous lesson, part 39, we reviewed the KeyDown event and fixed some issues with the tab order. It's important to review that material to understand the context of today's improvements. Now, let's delve into managing key press behavior so that the up and down arrows do not interfere with data entry and navigation in our combo box.
The issue is that when someone types a value and then uses the up or down arrow, the box tries to update its content and the handling gets mixed up. Instead, I want the program to recognize when the user is navigating with those keys and temporarily stop updating the box's value or list.
To accomplish this, we need to save the state indicating whether an arrow key was pressed. The key is for the database to remember between the KeyDown event and the code that actually processes changes, that it should ignore those presses. There are several ways to do this: a module-level variable, a hidden form field, or my preferred method, temp vars. Temp vars are versatile and reliable - they do not get erased if your database encounters an error, unlike global or module-level variables.
Here's the process: in the combo box's KeyDown event, when we detect that the key pressed corresponds to the up or down arrows (key codes 38 for up and 40 for down), we set a temporary variable, for example "arrow key pressed," to true. This serves as a flag to the Change event that it should skip processing for these particular keystrokes.
To detect which keys are being pressed, I simply have the database report the key code as users press keys. This helps identify which codes correspond to which keys on the keyboard without needing to look up the information separately. The letter "A" has a code of 65, space bar is 32, up and down arrows are 38 and 40 respectively.
Once we know which keys are relevant, we add the logic to track when those keys are pressed. Then, in the Change event for the combo box, we check the status of this flag. If the temp var "arrow key pressed" is true, we instruct the event handler to do nothing, bypassing the normal behavior that would otherwise update box contents. After processing, or at the end of the event, we reset the variable back to false so that other key presses (like alphanumeric typing or Enter) behave as usual.
After saving and testing these improvements, the combo box now allows users to type and navigate with the up or down arrows without any unwanted changes. You can freely scroll through items without overwriting your typed input, and selection becomes a lot friendlier. This streamlines the workflow, making the process more intuitive for users.
I also touched briefly on deleting items from the list. While it is possible to add the ability to select multiple items and delete them all at once, deleting individual records works quickly enough for now. Record selectors can cause other issues, so I prefer to keep things simple.
Looking ahead, tomorrow we will finally start tracking meals. Instead of creating a second combo box for meals, I'm planning to combine foods and meals into a single combo box using a union query. This allows us to pull information from multiple sources and display them together seamlessly. If you want to prepare in advance, check out my video on union queries. This will help you see how to join data from multiple tables or queries into one unified set that the combo box can use. This approach will make our system even more powerful and flexible.
That's all for part 40. We'll be back in part 41 to begin incorporating meals into the combo box. If you want a full step-by-step walkthrough of everything discussed here, you can find the complete video tutorial on my website at the link below.
Live long and prosper, my friends.
Topic List
Ignoring arrow key presses in Access combo boxes Using the KeyDown event to detect up and down arrows Getting key codes for keyboard events in VBA Storing state with TempVars in Access Conditionally processing combo box Change events Initializing and resetting TempVars in VBA events Preventing unwanted text replacement in combo boxes Allowing navigation within combo box dropdown using arrows Maintaining combobox values when navigating with arrows
|