39. VBA - Access Modifiers (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 more specifically today's video is going to be on access modifiers now we've used access modifiers in the past but I've just kind of skipped over there meaning I have just told you to go ahead and type it out but don't worry about it and we'll talk about it later well now is the time to go ahead and talk about them access modifiers limits the access to elements such as methods and variables so element you haven't seen me use the word elements just yet but as elements are essentially a word that Microsoft uses in order to talk about anything and everything that is in your VBA code such as functions subroutines properties variables anything you write in your VBA code is essentially an element so access modifiers limit the access to those elements the first one that we're going to talk about the first access modifier that we're going to talk about is public and you may recall me using this public term in the past and we would use these to declare something like a public function so public function function name and then we'd say as string or as some sort of value type so public is basically setting the access to that function and public makes it so that elements can be accessed from code anywhere in the same project and also from other projects that reference the project one of the neat things about access is that you can actually write your code in one database and then add that database as a reference in another database and you can use your code that you wrote in that new database so you don't have to go back and rewrite everything you could just make a reference to another database this is really nice this is kind of convenient when you don't really want to retype everything or you don't want to copy and paste everything over I don't really see too many times that another application references another database it's just so rare but it is a possibility the other access modifier that you've seen we use is the private access modifier and private basically makes it so that elements can be accessed only from within the same module class or structure so unlike the public access modifier private is very very limiting on who can access what what other parts of your code can access that function subroutine or variable the other access modifier that you might see occasionally is global now global has not been the prick it's it's not deprecated too much you still might see it occasionally especially if you're working on an older database one that was written you know say ten five years ago something like that global keyword is still used occasionally but it's essentially the same as public except you can only use it on variables and you can't use it within object modules or classes you can only use it under very very strict circumstances when you're creating a module that's all by itself not in any way associated with a form or report or anything like that and you can't use it within a class you have to declare a very separate specific module that you're putting your functions in then you can declare a global variable okay and again you can only use it on a variable you can't declare it on your subroutines or functions then there's the friend access modifier and I debated whether or not to even mention friend because it is so rarely used that it's almost not even worth discussing however I thought there might be the off chance that you might see it so you can at least understand what it's talking about what a friend access modifier is for a friend access modifier basically makes it so that elements can be accessed only from code within the same project and the code can only it basically the friend can modifier can only be used in object modules and classes so you can't use friend in that separate module that I was talking about when I was talking about the global access modifier friend is used in basically the exact opposite circumstances you can only use it when you're talking about object modules and classes and friend also makes it so that those those functions or subroutines they can't be accessed from another project okay so it's very very similar to public except you can't use it from another ref project all right so those are some of the access modifiers you might run across I'll tell you right now though that I have never really had to develop any application that used anything other than public and private there may be circumstances that you might use global occasionally friend you might use if you're really concerned about other people making a you know a database that's working off of code that you've written before but honestly public and private are the only two that I really use and that you'll probably ever need to use so let's go ahead and take a look at some of these in use in how we can use them alright so first I'm going to talk about this public access modifier that I've declared in front of this function called public test you'll notice that we're declaring our function as public and this means that not only in this code that we've written here is this public test function available but I can access it from other modules so say for example here in my mod twos in my mod - I'm sorry mod test - module or even from within a forum here so I can go into this and type in something like you know maude test and we can see there's our public test function you'll notice here that what I've done is I've actually started by typing out mod tests and then I use the dot in order to look at the different to get the intellisense to show me what my different functions and variables were available to me from the mod tests so this is really neat this is a neat functionality of access again in the mod test module I've got a public function called public test declared and I'm going to another location which in this case was the forum forum and I can actually use the mod test class named our module name to start my drill down in to find my public test all right so that's pretty cool that's that's definitely really really nice I'm going to go ahead and backtrack this here because I have this set there for a reason and you can see I'm going to go ahead type mod test dot public test so I'm drilling down to the mod test module and I'm going to go ahead and call that public test function which is declared as a public function and the public test function is going to return back a value of public access modifier all right so when I run this I should get public let's say print out public access modifier exactly how we anticipated all right so again this debug print basically calls this function and prints out to our immediate window whatever the value was that was returned from this function okay so I can drill down call that function that's all great now let's talk about the private modifier so I've got this function called private test and it's going to return back probably access modifier to whoever the caller was of this function so private now is the access modifier I've set to the private test function and so I'm going to go back in here and I'm going to try to drill down to it so I'm going to do mod test and I hit my period and you notice the private test function is not available I can't see it anywhere so okay well let's just do soon maybe I just it's is something wrong with access so I'm going to go private test is what I'm going to do anyway oops private test okay and you'll notice that oh well see no there we go it even though the intellisense didn't find it it changed it for me so I'm sure that's going to work now well guess what no it doesn't ok method or data member not found but we know that private test is a function that is here it's right there we've declared it but because we use the private keyword as the access modifier for this function it is not available from anywhere outside of this of this module I'm going to stop my code right there and you'll notice that I have this other public function that I'm declaring called call to private test and from here I can in fact call the private test function so now what I'm doing is I'm going to go ahead and call my public function called call to private test and I'm going to set its value to whatever the value returned from private test is but because I'm I'm declaring this public function from within the same module here within this screen window which is the same module that houses this private function called private test this private test call is available to me I can call it from within this module I just can't call it from anywhere else this is it this is the only place I can call that private test function so I'm going to go ahead and set a breakpoint here and we'll follow along here I'm going to go ahead and drill down to call to private test and now I'm going to run my printout subroutine here so debug print mod test call to private test and see what we get here so our we're going to jump to our public function called called a private test and it is going to return a value as a string so we're setting the VAT the variable called private test equal to whatever the value returned from private test is which jumps us up to the private function so now we are gaining access to that private function called private test it's setting the value of private test to private access modifier okay I'm going to go ahead and return back all the way back to our debug print request our print out subroutine and notice that down here on our immediate window private access modifier has been printed out on our immediate window and again that was what we saw was labeled within our private function called private test there's our private access modifier private access modifier all right so the only again the only way we can get to this private test function is if we call it from somewhere within this code and that's why we had to create this public function called called a private test all right so that's public and private but I'm going to show you a neat little thing here remember how in a previous video when we were talking about going through a select case and we declared this operator string in order to determine which function we were going to run on two different values and if you remember we use the dim keyword here well one of the things that you may not know is that dim and private are interchangeable in this particular case when you're declaring a variable you can use private or you can even use public oops okay oops if I could not fat-finger it there okay there we go so we can use private or public in order to declare a variable okay now um private and dim are basically the same thing when we're declaring the variable but notice though that I cannot change this one for example I've got this subroutine called whatever down here I cannot dim a subroutine I'm going to get an error here I can only dim a variable but I can use the private keyword to declare a variable that I want private to this particular module remember the private keyword makes it only to the local so dim or private are interchangeable in this particular circumstance and the common practice just so that you know this is that we use the dim keyword when we are declaring a variable from within a function or subroutine you'll see it the dim keyword used here but you will see the private keyword used when it is declaring a variable outside of your functions and your subroutines okay so that's just the common practice you can do a dam if you'd like like we had before when we were demonstrating the the preview in the previous video but normally you will see the keyword private used okay all right so now this is a good example of the global access modifier so I've got the STR test string that I'm declaring here as a global variable and is if type string now remember the global variable is very similar to public in that I can access it from anywhere within my database so I'm going to go ahead and go here I'm going to type in mod test and you'll see there's my STR test variable and I can go ahead and set its value so mod test STR test equals whatever and now when we print out the STR test when I run my printout function or subroutine here we get the return value of whatever because that's what I did that's what I set the value of the STR STR test variable all right I could also use the public key word to declare my STR test and you'll see that it works essentially the same way I'll even show you if I dot if I dot down there's the STR test variable I can set it to vary set it to whatever delete that out and sure enough we get the same results so public and global are very very similar and that's why I said you hardly ever see global anymore because public is so much more common and does essentially the same thing okay go ahead and turn that back for posterity measure there all right now functions are I'm sorry the friend access modifier is a bit trickier I went ahead and went into my form here and I declared a friend function called friend method test all right and it is going to return a string that is a friend function modifier okay so again this is in my form underscore frm form I'm going to go to my mod test here and let me delete this out of here and instead of calling mod test as a matter of fact you know what before I do that I'm going to go ahead and show you that if I try to copy this if I go ahead and friend function I'm going to copy this I'm going to put this in my mod test here you'll notice that it's red I get an error here sorry about that let me let me delete that out here sorry that's not necessary and from it's an artifact but you'll see here syntax error okay it's because the friend access modifier cannot be used in a module that is separate from a form or from a class I can use the friend access modifier if I'm in a class okay get rid of it here so we don't error out when we do our debug here you'll notice that we compile just fine with the friend access modifier in class we also have it declared here in our form okay and when we debug you know we compiled it we we didn't run any problems so that's great so what I'm going to do is up I'm in the wrong paw or on place here there we go so what I'm going to do is I'm going to go ahead and call that friend function that again I'm just to show you here and the friend function called friend method test we're going to return a value a friend function modifier so we need to drill down to that function form frm form dot friend method test okay so even with the method being on our form on the module on our form I can still go ahead and drill down to it if I if I call the form object and then use the period in order to drill down to our function that we declared in there so now when I do print out sure enough friend function modifier so friend is very similar again to the public keyword it just makes it so that we can only declare it we can only declare it within a class module or within our class objects here for access we cannot declare it in our modules all right I hope that makes sense to you and again also the friend access modifier makes it so that other databases cannot view those functions so this function here on our forum called frm forum this friend method test function would not be available from another database all right I hope that all makes sense to you if you have any questions about this video please feel free to put a comment below or you can send me an email and I can address your question a little bit more directly and if you have any other video idea ideas or suggestions that you'd like to see please feel free to send me an email on those as well
Info
Channel: Programming Made EZ
Views: 46,534
Rating: 4.9851851 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, modifiers, public, private, global, friend
Id: zGC1-5M-hOg
Channel Id: undefined
Length: 20min 22sec (1222 seconds)
Published: Sat Feb 22 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.