Big Button Form in Microsoft Access
Got a good one for you starting today, folks. We're going to be building a big button form. What's that? Well, we're going to be creating a dynamic form with lots of big buttons on it so you can select options in your Microsoft Access database.
Now I'm not just talking about a bunch of different forms with a bunch of different buttons on it. I'm talking about one form that you can control dynamically using VBA code. So for example, let's say you want to pick a vehicle. Make, model, and year. Normally you could do this with cascading combo boxes or something like that, but if you're working in a garage, as we'll discuss in a minute, this might be easier.
You click a button, you pick the make. Ford, you pick the model, Mustang, and picture someone with a touchscreen right just tapping on it. You pick 2022, and you can have more options if you want, and then it puts the value right here in that combo box for you. Right, that replaces cascading combo boxes. So this is going to be our end result, this thing right here.
But we got a lot of work to do to get to this point. So let's take a look at the question first. Today's question comes from Adam in Fort Dodge, Iowa. Adam is one of our platinum members, is one of the moderators on my website, and he's our resident badge geek. We call him something different than badge geek, but badge geek is more appropriate for a video.
Anyhow, since he's so proud of his badges, I figured I'd put them all here in the video. On the website, we got different badges for all kinds of things like whether you're a Trekkie, what your rank is, what country you're from, all kinds of different stuff.
Alright, so Adam's question is, have you covered creating button controls on the fly anywhere? Can Access even do that? I need to select the year, make, and model of a vehicle. I agree, cascading combo boxes or list boxes would be so much simpler, but for the people that would be using this with tablets and touchscreens, they are not as friendly as buttons I've found, especially covered in grease. LOL.
Yes, I do understand exactly what you're talking about. I've had to build some interfaces before that were touchscreen friendly and you need lots of big buttons because picking from a combo box or a list box isn't quite as good. You can make the list boxes really big, but if people have to scroll up and down it gets cumbersome.
Now, a couple of years ago I put together this Tech Help video and database called Vehicle Maintenance and we did something similar here where we pick a make and model and these cascaded. So for example, if you pick Audi, you'd see only the Audi models here. You wouldn't see Mustang, for example, under Audi. So go watch this video if you want to get a little heads up on what we're doing today.
And of course, cascading combo boxes are where you pick a value in the first box and that will limit the options in the second box. So if you pick Florida here, you'll see only cities from Florida, for example. I've got another video that explains these in a lot more detail. This is what we're going to be replicating with buttons.
You should also watch my custom message box video as well, because in this video, I teach you how to open a form (in this case, a custom message box form) and then have this form return a value to whoever called it, using the AC dialog command. So this guy opens up, execution stops, the user clicks a button, and then execution can continue after this guy returns its value. This is covered in this video, go watch this.
There are a lot of ways to set and return those values. You can use global variables, you can use form arguments with the open args, there's lots of different ways you can do it. I chose to use temp vars. I think it's the best method and of course Adam loves temp vars. In fact, I almost called this video Adam's big button form featuring temp vars but I figured this would be fine.
Now there's going to be a lot of VBA in this series so this is pretty much for more advanced developers. If you are not a VBA developer and you want to learn how to program in VBA, go start here. Go watch this video. It's about 20 minutes long. It'll teach you everything you need to know to get started and I've got lots more beginner VBA videos on my website as well.
But today is going to be for the developer developers. So those of you who have been asking me for more advanced developer stuff, well strap in. Here we go.
Alright, so let's get started. I'm going to start with my TechHelp free template. This is a free database. You can download it off my website if you want to. It's got a lot of things in it that I like that already have my options and stuff set. I've got some sample code that I use in here and I've covered this in a lot of my other videos.
But let's start out by making our tables that we're going to need for today's video. We need a make table, a model table, and a year table. Yes, we need a year table. So bear with me as we go through the setup here. We're going to start off with VMakeID. That's our auto number. Auto number should be in every table pretty much. VMakeName, and that'll be short text. Why V? Because I don't feel like spelling out vehicle every time. That's one of those words I always type wrong. I know how to spell it, but I just, my fingers don't always type it in right. Okay?
For VehicleMake, we could probably index this guy right, no duplicates. You wouldn't want two Fords in this table, right? We're gonna save this guy as VMakeT, my vehicle make table, right? Primary key is the ID. Let's put some sample data in it. We got, what do we got here? Ford, we got Jeep, and I personally, I'm an Audi driver, so I'm going to put Audi in there. Alright. I like the great German engineering.
Alright, now I'm going to leave this table open and just slide it over here because I'm going to want to be able to see what these IDs are to put them in my model table. Alright, now normally, you'd make data entry forms for all this. You wouldn't have to know what the IDs are. You guys are developers, you know all about that. I'm not going to waste time doing that in class, but you can do that if you want to. You know, make a form with a couple of sub-forms on it, fill all this stuff out, but we're just going to put the tables together for now.
So, create table design, this will be our model table. So vehicle model ID, that's my auto number. I like to keep all my IDs up top. We need to relate this back to the make. So VMakeID, that's a number, that's our foreign key. Right, that'll link back to the make table. And then VModelName, that's where we'll put the model name in there. Alright, save this. VModelT, model T, see what I did there? It's a database about cars and they got a model T in there. Just kidding, that's stupid. Alright, hit OK, primary key, close this guy.
Alright, let's put some data in this one. Now this is where you're going to want to see the makes over here. So you come over here. You're going to put, okay, so we'll put, we'll do one, we'll do, what do we got, F-150, that's Ford, we'll do another Ford, we'll do the Mustang, we'll do the Explorer, we'll do the Escape and the Bronco. You need data for this one, Bronco. You need data for this database to actually see that it's working, what it's doing. If you want to cheat, if you want to get a list of these somewhere, you can.
I actually went out to chat GPT and I just told it to give me a list of these that I could just type in. Watch this, I'm gonna show you a little trick. So I went to chat GPT, right, and I said, please give me a list of models of vehicles made by Chevy. Please display them in a format that I can easily copy and paste into an MS Access table with line breaks. Because if you don't tell it to give you the line breaks, you won't get the line breaks. Hit OK, and it should just put a list together for you. There you go.
I don't need that many. And in fact, let's update that a little bit. Let's say, give me this, copy that. Give me five models and put them in a code window. If you put something in a code window, it gives it to you like this in one of these code blocks. You just go copy code, makes it easier. Now that I've copied that, watch what happens. Come back over here, now click on just this cell, just like that, I know this is access, these are not cells, but just make sure you got just that highlighted in it. Paste. Boom. There you go. See that?
Now we just got to come over here and fill all the twos in. This is just a simple one-time data entry thing. That's pretty cool. And now I'll say do the same thing for GMC. Give me seven of whatever, there you go, GMC. And you could do the IDs first if you got like one, two, three, four, five, six, seven. You can come in here and select, watch this, move right over the border and select just those cells, we'll call them, and then it pastes.
Well, they are cells. Well, I'll say give me five for Jeep. Beautiful. Copy code, paste it in there. Jeep is four. It's these little tricks like this that sometimes make your job as a programmer, a developer, a lot easier knowing little tricks even just sometimes a shortcut key. And this is something you're probably just going to do once and never have to do it again.
And give me ten for Audi. I'm always polite to chat GPT because you never know when they become our overlords so I want them to be on good standing with these guys. Copy code and I will come here and we'll paste. And Audi, what's, what's with the model names, Audi? Come on. Awesome German engineering, not too creative with the naming. I got a Q5 and an S3, which I didn't even see on the list there. Alright, we'll put the Audi in there. Alright, so we got that table all set. Okay.
Now, when it comes to this table, you don't necessarily want to index no duplicates this. I know technically the car manufacturers try to not step on each other's toes, so you're not going to see like a GMC Mustang, for example, but you never know, it could happen. You know, maybe both car manufacturers comes out with a Raspberry model or something, and two of them do it in the same year and oops.
But if you want to prevent duplicates for a specific manufacturer, you can set up a composite index that's a lot more detailed. I do have videos on it. It's actually easier to check this in code if you have a form where you put this stuff in. Just do a quick D lookup and say if this is already in here, at least give the user a warning. That's a whole separate video though.
Alright, so next up is going to be the year table. For that, we don't really need to see this right now. Save changes, yes. Save changes, yes. Let's create table design. We'll have a VYearID auto number. Yes, we're going to put an auto number in this table too. This is going to relate back to the model, VModelID. And this will also be a number, that's our foreign key. And then just VYear, because we're going to make this a number as well, that won't be text.
Now what you got to do is, let's actually save as VYearT. You have to put a year in here for each model for each make. I know it's a ton of data entry, but is this necessary? Yeah, it really is. If you want to make sure you get all of the available years for a specific model of car, then you've got to have a table for it. If you don't care, if you're not that particular about it, you know, if you've just got maybe a service shop and you just want to type it in, that's fine.
I don't care but if you if it matters to you you need a table for it. Not every model was made every year, for example the Chevy Camaro they made it from 93 to 2002 then they stopped making it until 2010 so there is no such thing as a 2008 Chevy Camaro so that would be something you put in this table.
Now, restoring the model in the year table, which if we know this we can go back to the model table. Do we need to store the make in this table? No because as long as we know the model, we can go back to the model table to find what make that belongs to. That's why these are cascading tables.
Okay, so we know the make, okay? We know the model and now we got to put in the years for each of those. Let me make this a little bit smaller here. I guess we do need to see these right now. Now this can be extremely cumbersome. You know, the makes were tough to get in here right? The years are even worse.
What you have to do is say okay the model all right the F-150 is model one. Alright now you got to put all the years in that you want for that vehicle. Let's just do 2020 right? 2021, 2022, 2023, 2024. Okay and you got to come over here, fill all these in. And yes, you can do this with code if you want to or if you want to just block, copy, and paste this in here.
Excel is really good at this. You can use Excel because it can auto fill and find the patterns. So if you want just let's say 2020 to 2024, say that 10 times fast, 2020 to 2024. And you want that for all of your models that are currently in here. What are we up to? Model number, let's see. So we need 32 of them. For this, let's go over to Excel.
I'm just gonna open Excel up. And I'm gonna come over here and let's say, for each of these, so I need, let's do 2, 2022. Let's just say we're gonna do 2021, 2022, 2023, and 2024. Okay? 2023 and 2024.
Okay now here, I'm going to say equals this plus one, that's three. I'm going to auto fill this down like that. Okay, and for here, I'm just going to say this equals that press enter and I'm going to auto fill that down. Right, see how that works?
Now just auto-fill this down as far as you think it needs to go. So I'm going to bring it down to here maybe. There's 4, 5, 6, keep going. What do we need, 32 of these guys? So drag it down. Let's see this form 32, about 120 rows worth. Let's see. I'm going to write it down here somewhere. There we go. Okay, good. So there's 32. We can get rid of these ones. Delete.
Alright, now just copy all of these cells. Ready? Copy. Switch back over to Access. And now I'm going to select these two cells together and then hit paste. Boom. And there's all my data that I need. See how that works? See, selecting these cells in table view that's handy and now I've got all of the years in for all of those cars right model thirty which is the Q5 right and I've got all those in here and you can come in here and tweak whatever you want to tweak you want to have more years less years years don't exist that's up to you but this is just getting the initial data setup and yes I know Adam was saying in the forums that they basically their shop gets a big, huge Excel spreadsheet with all of this in it so he doesn't have to fill it in.
But we need data to play with. We need data for our data setup. Alright, so our tables are all set. Good to go. And again, you could put together a fancy VBA solution to do that. Sure, okay, but if you're only going to do it once, just do it in Excel real fast like I just did.It took, what, two minutes? I am thinking of making an add-on for this later on. Maybe I'll do a video on it where the next year comes around and the 2025s are released. Now you've got to take all of last year's data and just copy it and make a whole new set of models from all the existing ones and just add that year. Maybe we'll do that in a future video. It's on my list of possible stuff for the future.
But I'm always of the mindset that if it's a task I'm only going to do once, I don't bother automating it. If it's a task that I do every day, I don't care how easy it is. If it's a three-minute task that I do every day and I can simplify that with some VBA code, if it takes me two hours to write that code just one time but it saves me three minutes a day, well after what, 40 days, now I'm in net positive time gain.
Right, so I just click my button. But it's not just that. That's three minutes off of every day. So I'm all about automating stuff if it's warranted. That's a good make, model, year. Could we do more stuff? You could do whatever you want. You could do styles like is it a sedan, is it a coupe, is it a hatchback, is it an SUV, is it a van? Rule number one, in my van, it's rush, all rush, all the time. You could do drive type, you could do fuel type, you could do all that stuff. You just make more cascading tables and just add to what I'm showing you right in this video.
You could do things like color, although I wouldn't necessarily make that a table. You could just enter that or make a list of colors to pick from, unless you care about stock factory colors, because people can always custom paint their cars. So you might get a purple Ford F-150, I don't know, whatever.
Now, like I mentioned earlier, when it comes to proper data entry, if you don't want to deal with the tables or if you want your end users to be able to modify the make, model, year tables, you don't want them working in your tables. End users do not get access to tables. That's a rule of mine. They have to work in forms. So you could use nested subforms. Yes, you can have multiple nested subforms: make, model, year. This video explains how.
You could also use synchronized subforms. This is a little more advanced, but essentially you'd have a parent form that doesn't have any data in it. Then you'd have, you know, make over here, model in the next subform over, and then maybe have a third form over here with the years. You can add stuff like that, and these are all synchronized. So check this form out if you want to learn about that. But we're not going to focus on those kinds of forms in this series unless we do later. If you guys want to see that, let me know. If you guys get hooked on this database and want to see how those forms are integrated into this one, let me know and I'll make more videos. Squeaky wheel gets the grease; post a comment down below.
But for the rest of this video series, we're going to focus on this form, this pretty guy. We're going to take those makes and load them into this form. This is one dynamic form that we're going to control with some VBA code. Right, you pick your make, you pick your model; this comes out of the model table based on the value that the user picked in the previous step. Same form, we just change the parameters. Mustang. And then you pick your year. And it puts that here.
And we're going to get to this part in tomorrow's video. So tune in tomorrow, same bad time, same bad channel. If you remember, of course, you can watch them as soon as they're finished. I'm going to try to record part two right now. It's getting late though. I might actually record part two tomorrow, but we'll see. But we've got lots to go. This is probably going to be at least a five or six, maybe seven, eight, nine, ten. I don't know how many parts. This is going to be a long series. I've got tons and tons of notes. Lots of VBA coding coming up.
So that's going to be your TechHelp video for today. I hope you learned something. Live long and prosper, my friends. I'll see you tomorrow for part two. Oh, and wait, someone's beaming in. Hold on. A special thank you and shout out to our diamond sponsor Juan Soto with Access Experts software solutions. They're manufacturing experts specializing in Microsoft Access and SQL Server. Juan is a 13-time Microsoft Access MVP. Check them out at accessexperts.com.
For a complete video tutorial on this topic, please visit https://599cd.com/BigButtonForm
No comments:
Post a Comment