28. VBA - Writing Our First Code (Programming In Microsoft Access 2013) 🎓

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone and welcome back to programming in axis 2013 my name is Steve Bishop and today we're going to be continuing the section on VBA or Visual Basic for applications and let's hop back into our database here and you'll see that I've created a new form here called frm form and I've given it two different controls I'll give it a textbox and I've given it a command button or sometimes just called a button now in the textbox I went ahead and gave it a name and if you click on the other tab okay on the other tab of the properties of that of that text box you'll see I can change the name of it here so go ahead and change your name of your text box and on the button I went ahead and changed it to BT and go so this has a name of txt output and this has the name of BT and go now you'll notice that I prefixed the name with the with a three letter indication of what kind of control it is and this will become important when you're going through the code because you're going to be calling and assigning things to different objects on your form and it's helpful to have some sort of way of knowing what is the control I'm connecting to and what's the name of the control I'm connecting to so by naming it BTN go that kind of helps me understand this is the button go and this is the textbox txt standing for textbox output all right so you could name it anything that you really want but it's important that you go ahead and name it if I drop a new text box in here you'll notice that the default is just to give it t text and then a numerical number of to give it some sort of new unique value and that's not really helpful at all when we're going to go into coding we need to reference this object it's going to be hard to remember that text 5 is this particular text box so that's not what we want to do we want to make sure that we're naming our objects appropriately so that we can find them later all right so I'm going to click on the Go button here in my design view and if I click on the event tab you'll notice that there's a series of different events that come up now in this particular case I'm going to want to use the on click event and what these all are for are certain events are going to happen on this control in this particular case for a button I have the option to click on it so if I click on the button then what will happen is if I create some sort of code behind it in the on click event that code will happen when this button is clicked so that's how access handles event the you know handles events on the form so I've got on got focus on lost focus on double-click Mouse down mouse up mouse move and you'll see that if you don't know what all these are it's okay there's kind of actually down here at the bottom left here you'll notice macro or function that runs when the mouse moves all right so that's essentially going to give you a brief little description of what that particular event handler does and when it's being triggered okay so for this particular example again I've selected the go button here and I'm going to go to the on click event here and select the ellipsis I'm going to go to the code builder and just so that you know what these our macro builder is basically where you can build a macro which is similar to code but it's a it's definitely a whole different syntax and way of handling events and doing different things that access and we're not going to go into that then there's the expression builder which you've already seen before you can build expressions in there or in this case we want to go to the code builder so I'm going to go and click OK and now we'll see that access automatically creates some code for me and one of the things that we want to notice is remember how we were talking about the way that code is going to be grouped together and and kind of organized in the background well this is what we're starting to see form underscore frm form is the name of the module that is associated with our frm form form okay I hope that makes sense to you that's it's kind of important to understand that warm underscore form is the module that's automatically created by access that is attached to the frm form and we can see the same thing as true for form frm customer addresses which is that form that we were working on before and we got the me require e okay OOP so let's go back in here and here's where we can see a few key words I'm not I'm going to cover this a little bit later but for right now let's just talk about what we see here the private is kind of a key phrase that helps indicate how much access other parts of the application have to it and again we'll go into that much later down in the series here but what is important understand is this sub stands for subroutine okay and the other option is what we call a function but what automatically is created and generated by access is a subroutine for the BTN go control on the click event so this is very important this is how access is automatically named this particular this particular subroutine so that it knows that this is the event that should be fired when somebody clicks on a button the button Go button all right so what we need to do is let's take some of the things that we've already learned let's take a variable so we're going to dim ourselves a good old variable we're going to call it STR text and we're going to give it a value type of string all right so there we've got our syntax to dim and then there's this the variable name as a string now that we've got this variable created in memory we need to assign it a value we're going to assign it a value by using STR text STR text equals and then we're going to pass it a string hello world all right so what's happening here is we've created our variable in our memory and then we've assigned okay this equal sign here means we're assigning the hello world string which is a string of characters surrounded by quotation marks indicates that this is a string and we're assigning it to the variable STR text so now hello world is going to be assigned to the STR text variable in memory then what we need to do is we need to have some way of displaying this out to the to the user because if you think about it all we've done here is we've just basically made HelloWorld part of the computer's memory but we're not showing it to the user in any way and that's what the other textbox is for if you remember I had a txt output is the name of the textbox that I created back here I'll just show you real quick remember this textbox here notice that it is named txt output all right so back in our code behind you'll notice that if I you know if I go back in here there's going to be a different data here we're going to have the format we're going to have the data we're going to have the other section here and all of these are properties of that particular textbox so we have the name property we have the vertical property we have the tab stop property all of these are properties of that particular control which kind of talks about remember how we were talking about classes essentially all that a control is is a class with properties all right so the one that we're really concerned about the most here is the one called value okay so the value property of the txt output control box here the output control I'm going to assign it which requires the equal sign the value that we stored in memory called STR text all right so again just to reiterate we're creating a variable in memory we're assigning that variable the text hello world and then we are assigning to the value property of the txt output textbox the value that we stored in the STR string STR text variable all right so that's essentially going to hopefully put the hello world string in our txt output so save that here let's go ahead and go back to our form here and under either the design tab or the Home tab you'll notice this button up here in the upper left-hand corner and if you click on the drop down arrow you'll get the different views that we can go into the one that we want is the form view you'll notice that we have the form view here and if I click on go there's our hello world string that we stored in a memory and then we assigned that value to our textbox all right so that's some of the neat stuff that you can do in VBA we're obviously going to go through a lot more than that but just kind of give you guys a little taste of what it's like to create a variable give it a value and then assign that to some sort of control it on your form and display it to the user that's pretty cool stuff to know all right I hope you've learned something and as always go ahead and subscribe tell your friends tell your neighbors tell your dog whoever let them know about this video series and hopefully they can learn some stuff too
Info
Channel: Programming Made EZ
Views: 187,030
Rating: 4.8850574 out of 5
Keywords: Microsoft, Access, 2013, Programming, VBA, Visual Basic, Applications, tutorial, lesson, guide, database, SQL, beginner, advanced, software, microsoft access, table, query, form, code, coding, development, Visual Basic For Applications, Computer Programming (Professional Field)
Id: z09sLQ_mNT4
Channel Id: undefined
Length: 10min 24sec (624 seconds)
Published: Fri Jan 10 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.