if you do rt as a subject at school and you will know that you need to do a practical assessment task or a patch as a project at the end of the year to show off all your skills that you've learned during the year and so this video is going to give you some tips and some ideas on on particularly how to do a login screen so let's let's look at it now most pat projects whatever the topic is there is an opportunity for you to do some sort of login where the user has to log in depending on the username and password so let's do something like that and see how we would do that in delphi but before we look at delphi let's look at the database that we're going to be using that's going to store the details of our users so i've just created a simple database there's a table called tbl users and obviously you can have more fields in it if you want but i've kept it very simple we've got a username which is our primary key because we don't want anyone to have the same username as someone else so my username is my primary key we have a place to store the password that the user has requested and their first name surname and a nice little thing is a user type if you've got different types of users maybe an admin and a general user or something like that i use a little variable or a field here to record what type of user they are because maybe on delphi side when you log on you will go look at that particular field and then that will allow you to open up certain parts of the program and close other parts depending on whether they're an admin user or a general user so that's the those are the fields that i've created obviously you will change the field size and make it more efficient but if you just look at some of the data that i've put in i've already populated data in i've made a main admin with a qwerty password very terrible passwords here some of them um john's got a terrible opposite there as well but so these are the passwords and there's the different types of users and then the um so the admin or general and their names and surnames and so that's their username i'm going to use the admin and qrt possibly remember john.smith's password for john okay so those those are the the fields in my database so let's go look at the delphi program part now so as i go through this video just take note these are just suggestions there lots of ways of doing these things there's lots of alternative ways i'm just going to give you some suggestions that can help you so let's pretend this is my my patch program and what i've done that whatever we can have in our patch program will be over here on the main form and i've already done the connection to our database and there's the ado table called ado users and this is on dm pat example so we're going to use this data module to access the ado table the connection's already been done for us so if i scroll up here if you go to the top of the program we're going to go right to the top you'll see that i've added that data module to the top okay so there we go so what i'm going to do is if you've got a pepper maybe you want the login form or the login part of the screen to be a separate form then you would add a new form what i've done in this case is i've got my main program here and i want to hide it from the user until they log in correctly and so what i've got here if you notice on the side here but this panel which i can move over here so i've got this panel that i've designed already so this is the i want this panel to appear like this and when you log in successfully then this panel will then disappear all the visibility will be false and then you can then see the program behind but until then you've got to log on and then i've got a little but label there if you want to you can create that when you click on that it'll open up another form where you can register an account and that will add a value to that table that we just showed you and that's a nice way of using an insert part of your code for the the delphi data pad project so let's look at this form here now what i've done is obviously if you when you're making a program you don't want to see this every single time you want to see your main program so i've put the i literally move it down here and then when the form gets created i just make that panel's left and top property zero so that when i run it even though it was right at the bottom you'll see boom it appears in front and so when i log on i want to then hide it if the login is successful so little things like that so i'm going to close that so that's ideally what i want to do so for now let's just look at our form or panel yeah i put these components on the panel i've got edt login usernames so just some tips here as well when you name your edit controls particularly like these components you'll notice i didn't call it edt username i call it edt login username and the reason for that is because there's a possibility that i'm gonna have lots of um username editor editor controls maybe i've got one where they register if it's on the same form it becomes a problem because then you go which edt username is this is the one for them to log on or is this the one for them to register depending on how your pro program is going to be designed um so i like to give it a name that also tells me which part which screen or which aspect we are using this is the login username maybe the register username will be where they type in what username they want to suggest and stuff like that so so be careful when you just have edt username maybe have the screen name maybe l off or login and this obviously they could enter in a password as a suggestion when they register so yeah i've called edt login password so that i know this is the login password so little tips like that another little tip before we get into the actual code is like you will spend so much time trying to test at your program and you'll especially once you get this part working and you're working on other aspects to have to type in the username and password all the time is quite a mission so what i do is i pre-populate the edit box with a valid username and password so i'm going to put that in now qwerty was i think the password admin are pre-populated so when i'm running it i don't need to spend time testing it um every single time i can just it's there i can log in it'll work straight away and that way as well when the program is completed then i will remove whatever's in those edit controls but just when you're doing your testing and programming it takes a lot of time to having to type it in every single time you want to test your program so let's get into the code we're going to click on the login button and we want to test that that is legit that that is those two credentials are in our database table so we're going to click on the login i've already extracted those values from the edit controls into variables and so what we're going to do is we're going to do a search through our database so we are obviously using the dm module i think it's the pat example if i remember correctly this data module is dmpat example so we are going to with this patch example what are we going to do i'm typing this side of this up dm pat example every single time so we are going to do a search for that username and password so i'm going to have a variable to say we found it to be found of type boolean and we are going to assume that we haven't found the user so b found is equal to a false and we are now going to go through our ado table ado users we're going to start at the very top not next sorry the top is first and then i'm going to go while we are not at ado users dot end of file do what are we going to do we're going to begin and then inside here at the bottom we can say ado users dot next okay so that's our algorithm to go through that database so that's always going to run so that's great now okay so what are we going to do we want to check so just pretend we are at the first record so i want to check if the username so how do we get the username from ado users well we've got to go find out what what do we call those fields in our database so it's username and it's password those are the two field names that we want to refer to so if ado uses username so that's how we extract the value from the table at the particular first record if that is the same as my s username then we know we've got a match but we don't want to just to match that that must match the username and the password if that's true and if the value at ado user's password is the same as my password variable which we extracted from the user and remember we put brackets around it so if those two parts are correct then we know we've had a match so begin end we can set b found to true because we found a match and there we go so we can keep going like that okay so it's it's legit okay so we also probably want to stop searching when we found a match so what i'm going to do while we are not at the end of the file and while be found equals false so when we get to the end of the file or when b file becomes true then we can stop searching so if we find it then it'll stop okay and then so then we can go else we can actually use the else next year i think so else next so if it's a match b found is true we don't go to the next one we'll be at the right spot that type of thing so we can go on like that so then once the loop is finished oh got so many ends here i'm just gonna keep track of them by saying end of wow and this is the now that's the end of the width that's the end of the width and this is the end of the wall and wow okay so let's have a look so let's so this should stop and say that everything's okay so if we get to the end here after the loop we can say if be found is still equal to a false after we've done all of that then we can show message um username and or password because one of them or both of them is incorrect is not correct okay so that's what we can do and if it is this is only going to display something if the username is incorrect so if it does nothing we know that it found it so let's just test it to see if it works okay so we know that that works when i click and log on username password is not correct so obviously there's a problem there so let's have a look at what that problem is let's go look at the database oh quickly that should be qwerty not like that so let's test it again that feels uh misspelt qwerty in the database so let's run it i like to have it quickly because it's easy to type let's log in boom so nothing happened so therefore the login was successful so if it doesn't log in then we know that a message will appear and then it will continue so what do we want to do if the login is successful so those are the types of things that we want to do well the one thing we definitely want to do is we want to that panel must disappear so that visible panel must be equal to false and we can do all this over here if we want or you can do it over here if it's true then you can do some code over here as well if you want to make the panel false so we say the username password is correct i also want to record who this user is it's only a good idea i've got two global variables here user and user type which i'm going to store the the username in so s user is equal to whatever this is and that's normally a good idea because later on if you want to get details or do a search you know who the user is you're dealing with that's logged in and then the s user type is going to equal to the ado user but it's going to be whatever is in the user type option so that we can know if it's an admin person also not to use a type and that's normally a good idea so that you can check this variable to determine if you must unlock certain aspects of the the program because it's an admin user or sign so that's how you can keep track of who the user is and what their type is and later on in the code you can say no you can't do this because you're an admin user or whatever stuff like that another little thing you'll notice over here on the program i've got if i'll just move that down here you can see that the welcome here's lbl welcome i'm gonna add a little funky thing here i'm gonna say okay that lbl welcome dot caption is going to equal to welcome plus the user's name what was the field called the field is called first name and then this surname so first name then i'm going to add a space and then i'm going to add the surname so we have a nice little welcome symbol sign for them okay so there we go so if the user we go through all the records if we find a record where the username and the password is a match then we say v found is true we can say we record who the user is and they type so we can use that for later have a nice little walk message and then that that panel just make it disappear and if it's not true then we go the next one next one next one we keep going if we get to the end and there's no user then we just say hey the username pause is not correct and it'll stay on that screen so let's run it and let's see if it works so we know admin and qwerty we're going to log on well your pet program here welcome main boss which is actually who the person is that is the main boss and if i log if i do it again and i type in i think it's john.smith and i type in i think his password was password but i'm going to put some random nope username possibly not correct that's great and then i'm saying okay well if i say the word password i know that that's the correct one i think hey welcome john smith now that user is now logged on okay so there we go that's a nice little user login that you can use for your program most pets can have some sort of aspect where you log on and so those are my tips for your login screen i hope they're useful for more videos on delphi and and other related content like cat and rt and that's please go to our youtube channel subscribe follow us on facebook and twitter leave your comments we'd love to hear from you and remember don't do it the long way to the mr long way