38. VBA - Passing Arguments to Parameters (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 Access 2013 my name is Steve Bishop and today we're going to be continuing our series on VBA or Visual Basic for applications since our last video was on subroutines and functions I thought this would be a good opportunity to talk about parameters now parameters are a way to pass values to a subroutine or function you can use one of two different methods of passing these parameters you can use the by ref keyword which is the default or you can use by Val by R f stands for by reference by Val stands for by value now when you use the by ref keyword a new variable is assigned the same memory as the original variable so in each one of your subroutines and your functions when you are going to have a parameter you're going to create a new variable and then that new variable is going to by reference be assigned to the memory from the original variable when you use the by Val keyword you're going to make a copy of the value and put that value into the new variable so it's a little different here when you use by Val you're making a copy of it but when you're used by ref your new variable is just simply pointing to the same spot in memory now the way this works is that if you used by ref you've got this memory location that's been allocated for a value and your original variable is going to be assigned to that memory location then when you make your call to your subroutine or your function you're going to create a new variable in that subroutine or function and it's going to simply point by reference to the same memory location as your original variable when you do the bivalve however you've got your memory location that's been created you've got your original variable as before that's pointing to that memory location but instead when you use the by Val keyword you're going to create a new memory location and your new variable in your function is going to be pointing to that new memory now the thing you need to understand here is that there's a couple of things going on first of all when you do the buy Val over here on the right this is kind of nice because you will be able to make a copy of the value that was originally in here and put it into a new memory location but when you use the buy ref since you're going your new variable is pointing to the same memory location if you make a change of the value in the buy ref reference in the buy ref variable you're going to also be changing the original variable value as well so when you change the value of either of these two variables they will both be affecting each other since they're pointing to the same spot in memory but when you use buy Val you don't have that same problem you're going to be making a copy of that value and putting it into a brand new memory location when you use the buy Val keyword and just remember by ref is the default buy a vowel is something that you have to specify so let's go ahead and take a look and see how this works in our database I've got a form which has textbox value one textbox value two and four different buttons that each perform different calculations this is the same code that we were working with in our subroutines and functions except I've changed it up a little bit I've got my operator here as a string that's been dimmed outside of our subroutines and I'm assigning the operator within each one of these subroutines okay notice before I had that math subroutine but I've gotten rid of it and now I just simply have the calculation function and each one of my subroutines now that's when you click on each one of the different buttons is going to call that calculation function and display it into a message box whatever it returns back I've also added this debug dot print function after our message box has been run and the debug print essentially takes and puts a string of data and puts it into our immediate window down here so theoretically the debug print is going to take whatever the value of operator is and stick it into our immediate window and we're going to see this is going to become pretty handy later on this is a very handy technique for displaying things to your developers because debug print will never show to a user it only shows to this immediate window and that's very handy so when you want the developer to see some bit of information when they're running through the code but you don't want something to display to the user all right so just to show how this works I've got four and two I'm going to use the plus button which gives me a value of six and when I look at the immediate window we can see the operator string had a value of plus symbol right and that's why you have the debug print there is it's showing us in the immediate window the operator value was set to plus okay now what I'd like to do is I'd like to instead of using the operator variable up that's been created up here instead of using it here in our function to run our select case against what I'm going to do instead is I'm going to pass into my calculation function a parameter and that parameter is going to be the operator and the way you specify that you want to pass a parameter into a function is first you need to create a variable inside of the function that the that the parameter is going to be assigned to so in my parentheses that come after my function name inside of here I'm going to I'm going to start out by saying by Val and then I need to give it the the name of the Val of the variable that I need that I want to create so it's opie is the name of the variable I want to create and since I'm creating a variable I need to also give it a type a datatype so as string so essentially I'm going to create this opie variable as a string type and I'm using by Val keyword to specify that I don't want to point Opie to the value that it's being referenced to I don't want to reference the memory spot I want to actually get a copy of the value and then put that copy into the Opie variable now I need to need to also go to my select case I need to change this from operator to Opie so that this select case statement will actually use this variable all right now what I need to do now that I've got this Opie variable created I need to actually pass the parameter to my calculation function and what you do that is you simply at the end of your calculation function call I'm going to add parentheses here and you'll even see it says by Val Opie as string in bold so this is even giving me a little intellisense that says ok inside of these parentheses that I'm about to put up I need to put in a parameter that will be called Opie it's going to be passed in by value and it needs to be of a type of string and luckily for me I've got operator which matches as a string value and now what's going to happen is my calculation function we're going to call it but we're going to pass in the value of operator to the calculation function and when it gets passed into the calculation function the first parameter is going to be assigned to the Opie variable and the Opie variable then can be used in my function here to run my select case against so you see how I'm just passing this whatever was in the operator variable I'm just passing the value to the Opie variable and then I can use the Opie variable here in my function all right um all right so I think that's all set let me go ahead and make a couple of copy and pastes here to do all right and I think we're good to go there so let's go ahead and you know I'm going to go ahead and clear out my immediate window let's go ahead and run four plus two is equal to six and if I look down here in my immediate window there's that plus sign again so it functioned exactly the same way as when we use the operator string down here in my select case it's the same thing it's just now I'm passing it a parameter of opie now let's talk let's see what happens here when we change this from by Val to by ref okay I'm going to leave everything else exactly the same I'm just going to change by rap by Vout by ref and again I'm going to clear out my immediate window go ahead and save it run it again four plus two equals six so that's good that's still functioning properly and again immediate window shows a plus sign so on the on the surface it looks like nothing really has changed nothing is different but if I do this if I leave it as by ref and I say let's change opt equal something else okay so now what I'm going to do is within the course of my function I'm changing the value of the Opie variable that after it's done doing the calculation I'm going to change it to say something else all right and since we're using the by ref keyword what should happen is that since I'm changing the value of Opie to say something else since it's a reference to the same spot in memory as the operator variable that means when I run the debug print and I'm showing up whatever the value is for operator since I'm changing the value to the same spot in memory for both of these variables that means the debug print should show the value of operator as being the same being as the value of Opie and it's now been changed to say something else so let's see how that works four plus two equals six and if I look down here my immediate window says something else all right so I hope you can understand that this is the by ref and bivalve difference this is the difference between those two things and then just one last thing here if I take out the by ref statement there I take that out remember by ref is the default okay because it's it's going to try to use up the least amount of memory accesses trying to use the least amount of memory as possible and by ref means you don't make a duplicate value in memory so if I leave off the by Val or by ref it's going to default to the by ref and now when I run this again and I clear this out run it again get a value of 6 and my immediate window again says something else all right so make sure that you understand the difference here between by Val and by ref and here's how you create a parameter you need to add the parameter what the name of the variable is going to be that you're going to use inside of your function you need to give it a value type and you know why don't I go ahead to show you this too I got a little bit of time you can actually add multiple parameters so I'm going to do this I'm going to say oh P is string so operator is string and then let's do value 1 as double and value - as double so now I can pass in three different variables I can pass in three different parameters to make three different variables and now I can change this from txt value to just use the parameters use these variables that I'm creating and passing it for as variables here all right so let's do this okay there's a little bit of cleanup work there so now I need to make sure that I am passing in not only my first parameter but also my second and third parameters and both of these parameters need to be of a type of double alright so the way I can handle that then is my operator is still going to be passed in there but I need to now pass in me dot txt value one and me dot txt value two and now I can just copy and paste this whole thing again and you can see I'm passing in value one our parameter on parameter two is going to be the me dot txt value one text box and then parameter three is going to be the value from txt value two okay so this is all going to work the same way but now I can pass in multiple parameters to my function I think this should be working I hope I didn't finger anything here still equals six and we still get something else as our immediate window all right so that's how you can pass in multiple parameters or parameters to your functions or your subroutines you could also do this as a sub it just won't return back a value all right so I hope that all makes sense to you I hope you understand how that works if you have any questions feel free to send me a message on my inbox on YouTube you can also ask me if there's a video you want me to make just feel free to send me a message about it and I'll see what I can do thank you so much
Info
Channel: Programming Made EZ
Views: 68,155
Rating: 4.9895015 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, parameters
Id: 6g09iXa93W8
Channel Id: undefined
Length: 15min 23sec (923 seconds)
Published: Sun Feb 16 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.