Jump to content
xisto Community
Sign in to follow this  
kitty

Simple Login In Visual Basic 6 user interaction example trough login programm

Recommended Posts

First of all, I am NOT a programmer, this is something my friend taught me. It describes basic interaction with the user, while showing basic functionality of this simple programm. So, without further ado, we're off to the tutorial:

 

First of all, start your visual basic, when prompted for new project, select Standard Exe. Next, we need to open code window, so we can start typing the program. This can be done in two ways, one is double clicking on the form, or selecting Code from View menu.

 

If you double clicked on the form, you will see following text:

Private Sub Form_Load()End Sub
If you don't see it, you will need to type it by hand. You will only need to type the first line (Private Sub Form_Load()), the last line, visual basic will add automaticaly (End Sub).

 

Next, we need to declare variables we will use in our program. Here's the next piece of code:

Dim MyName As String ' This variable contains users full nameDim MyUName As String ' This variable contains users login name (username)Dim MyPass As String ' This variable contains users passwordDim Response As Integer ' This variable contains users answers from message boxesDim Saved_Name As String ' This variable contains saved username from fileDim Saved_Pass As String 'This variable contains saved password from file
This part is simple, we only prepare variables for use (I know there is more technical explanation for this, but I can't remember :P).

 

Now, we need to read username and password from the file. Now, for this example, we will use simple text file. You can create it using notepad (I don't think you need a tutorial on that :(). In that file, write the username you want, and in next line, write the password. Now, add the following code:

Open "pass.txt" For Input As #1 'we open file with data we need  Line Input #1, Saved_Name ' We read username from file  Line Input #1, Saved_Pass 'We read password from fileClose #1 'we close the file

Now that we have done this, we can proceed with basic interaction with the user:

Login_Name:MyName = InputBox("Please, enter Your name.", "Introduce yourself", "")If MyName = "" Then  Response = MsgBox("To continue with the program, You need to introduce Yourself." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error")  If Response = vbYes Then GoTo Login_Name  If Response = vbNo Then    MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"    End  End IfEnd If
First, we ask a user, to enter his full name, so we can know how to address him/her. If the user doesn't enter his/hers name, we will tell him he/she needs to enter it, or to quit the program. I believe the code is very self explainatory.

 

Next, we will prompt for user name. This is the information that we need for successfull login.

Login_UName:MyUName = InputBox("Good day " & MojeIme & ", please, enter Your user name.", "Login", "")If MyUName = "" Then  Response = MsgBox("To continue with the program, You need to enter username." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error")  If Response = vbYes Then GoTo Login_UName  If Response = vbNo Then    MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"    End  End IfEnd If
This is basicaly the same thing we used for prompting for full name, just with different variables.

 

Next, we ask the user to enter his/hers password, to authenticate into system:

Login_Pass:MyPass = InputBox("Please, enter a password for user name " & MyUName & ".", "Login", "")If MyPass = "" Then  Response = MsgBox("To continue with the program, You need to enter Your password." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error")  If Response = vbYes Then GoTo Login_Pass  If Response = vbNo Then    MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"    End  End IfEnd If
OK, I guess you noticed this is the same thing I used in two earlier segments of programm. I was told it can be done with functions or subs, but I haven't learned it yet :P I guess I'll post another tutorial when I learn how to do that :(

 

OK, we're almost done. All we have to do is check if username and passwords match, and if that is the case, we can proeed with programm. If not, we tell the user he/she made a mistake, and exit the programm. Here is the programm for that segment:

If (Saved_Name = MyUName) And (Saved_Pass = MyPass) Then  MsgBox "Good day " & MyName & "." & vbCrLf & "Welcome to our programm, we wish You pleasant work.", vbInformation + vbOKOnly, "Welcome"Else  MsgBox "We are sorry, username and/or password are not correct", vbCritical + vbOKOnly, "Error"  EndEnd If
Here, we check if saved username is the one entered, and if the saved password is the one entered. If they match, we allow the user to access the programm, if not, we tell him/her there is an error, and exit the programm.

 

I learned this recently, and I hope I passed that knowledge to some of you..

Share this post


Link to post
Share on other sites

Hey, Nice tutorial. But it seems only one user that can login!. It's better if we can use new way, so not only one user that can use the program. Anyone has an idea?, I'm not good in Visual Basic 6 :D

Share this post


Link to post
Share on other sites

Hey, Nice tutorial. But it seems only one user that can login!. It's better if we can use new way, so not only one user that can use the program. Anyone has an idea?, I'm not good in Visual Basic 6

I've used VB6 for 9 years. I'll post the code for how I would do it, however I'm sure there is more than just one way to create a simple login system. It'll just take a couple changes to the kitty's code to implement other users.

 

We can copy the first part of kitty's code right into the form almost without changes. Here it is:

Dim MyName As String ' This variable contains users full nameDim MyUName As String ' This variable contains users login name (username)Dim MyPass As String ' This variable contains users passwordDim Response As Integer ' This variable contains users answers from message boxesDim Saved_Name() As String ' This variable contains saved username from fileDim Saved_Pass() As String 'This variable contains saved password from file

The only real difference between my code and his/her's is that Saved_Name and Saved_Pass are now arrays. Arrays can hold more than one value, each value in an array is given a number.

 

Dim TotUser As Integer 'The total number of usersDim TempName As String 'For the nameDim TempPass As String 'For the passTotUser = 1Open "pass.txt" For Input As #1 'we open file with data we needDo Until EOF(1)Line Input #1, TempName ' We read username from fileLine Input #1, TempPass ' We read password from fileTotUser = TotUser + 1 'Add one to TotUserReDim Preserve Saved_Name(1 To TotUser) 'Expand the arraySaved_Name(TotUser) = TempNameReDim Preserve Saved_Pass(1 To TotUser) 'Expand the arraySaved_Pass(TotUser) = TempPassLoopClose #1 'we close the file

The ReDim statement resizes the array (creates more elements). The Preserve in the ReDim statement means to make sure the data originally in the array stays there. Without the preserve statement, only the last username and password would be in the array, all the others would be "" or NULL.

 

The next bit of code stays the same.

 

Login_Name:MyName = InputBox("Please, enter Your name.", "Introduce yourself", "")If MyName = "" Then Response = MsgBox("To continue with the program, You need to introduce Yourself." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error") If Response = vbYes Then GoTo Login_Name If Response = vbNo Then   MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"   End End IfEnd If

So does the next bit

 

 

Login_UName:MyUName = InputBox("Good day " & MojeIme & ", please, enter Your user name.", "Login", "")If MyUName = "" Then Response = MsgBox("To continue with the program, You need to enter username." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error") If Response = vbYes Then GoTo Login_UName If Response = vbNo Then   MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"   End End IfEnd If

and the next bit

 

Login_Pass:MyPass = InputBox("Please, enter a password for user name " & MyUName & ".", "Login", "")If MyPass = "" Then Response = MsgBox("To continue with the program, You need to enter Your password." & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error") If Response = vbYes Then GoTo Login_Pass If Response = vbNo Then   MsgBox "We are sorry You do not wish to proceed." & vbCrLf & "We wish You a good day", vbInformation + vbOKOnly, "End"   End End IfEnd If

Finally something actually changes!!

 

Dim i As Integer 'For the For Next LoopFor i = 1 To TotUser 'Check every user.If (Saved_Name = MyUName(i)) And (Saved_Pass = MyPass(i)) Then MsgBox "Good day " & MyName & "." & vbCrLf & "Welcome to our program, we wish You pleasant work.", vbInformation + vbOKOnly, "Welcome"Exit sub 'You are logged in!ElseIf i = TotUser Then MsgBox "We are sorry, username and/or password are not correct", vbCritical + vbOKOnly, "Error" EndEnd IfEnd IfNext i

Place all this code together and it will work fine! Feel free to PM me with any questions you may have.

 

 


Share this post


Link to post
Share on other sites

internet cafe..

Simple Login In Visual Basic 6

 

How can I make a program with log-in log-out in my internet cafe using vb6?

Also a time code...

 

-question by camille

Share this post


Link to post
Share on other sites

How to prevent multiple user login using one account in VB

Simple Login In Visual Basic 6

 

I am writing a VB 6 program that shd run on the network. A user has to login before using the program. Now, I want to prevent multiple user login using one user account. It shouldn't allow or prevent a user to login whilst his/her account is in use. I am using MS SQL Server 2000 Database.

Please, can anyone help me with this? I would be very grateful if you could help me with this.

 

-question by King Acheampong

Share this post


Link to post
Share on other sites

Sub programs

Simple Login In Visual Basic 6

 

Replying to kitty

 

If you type Sub the name of the sub program and press enter you have a sub program. Then all you have to do is put in the code.

 

Example

 

Sub Celebrate()Label1.Visible = FalseImgDefence.Visible = FalseClsBackColor = QBColor(15)ForeColor = QBColor(12)FontSize = 72FontName = "Times New Roman"FontItalic = TrueCurrentX = 50CurrentY = 150Print "Victory To The Hearts!"Delay 5EndEnd Sub
Hope I helped.

 

-reply by Naomi

Edited by moderator (see edit history)

Share this post


Link to post
Share on other sites

Great code BUT i slightly changed it and i get the wrong password dude everytime and the pass.txt is so far only:

test
test

So should be good enough i enclosed the code i used please help thanks

Code :

Private Sub Form_Load()Dim MyName As String ' This variable contains users full nameDim MyUName As String ' This variable contains users login name (username)Dim MyPass As String ' This variable contains users passwordDim Response As Integer ' This variable contains users answers from message boxesDim Saved_Name As String ' This variable contains saved username from fileDim Saved_Pass As String 'This variable contains saved password from fileOpen "pass.txt" For Input As #1 'we open file with data we needLine Input #1, Saved_Name ' We read username from fileLine Input #1, Saved_Pass 'We read password from fileClose #1 'we close the fileLogin_Name:MyName = InputBox("Skriv dit navn.", "Dit navn", "")If MyName = "" ThenResponse = MsgBox("Skriv dit navn eller det bliver værst for dig selv [img]http://forums.xisto.com/public/style_emoticons/default/laugh.gif; & vbCrLf & "Do You wish to proceed with the programm?", vbCritical + vbYesNo, "Error")If Response = vbYes Then GoTo Login_NameIf Response = vbNo Then   MsgBox "Nå ikke!." & vbCrLf & "God dag til dig", vbInformation + vbOKOnly, "End"   Form2.Visible = TrueEnd IfEnd IfLogin_UName:MyUName = InputBox("Login " & MojeIme & ", Skriv dit brugernavn.", "Login", "")If MyUName = "" ThenResponse = MsgBox("Skriv dit brugernavn er du dum?." & vbCrLf & "Vil du fortsætte?", vbCritical + vbYesNo, "Error")If Response = vbYes Then GoTo Login_UNameIf Response = vbNo Then   MsgBox "Synd for dig." & vbCrLf & "God dag til dig", vbInformation + vbOKOnly, "End"   Form2.Visible = TrueEnd IfEnd IfLogin_Pass:MyPass = InputBox("skriv password til brugernavn " & MyUName & ".", "Login", "")If MyPass = "" ThenResponse = MsgBox("Skriv dit password." & vbCrLf & "Vil du fortsætte?", vbCritical + vbYesNo, "Error")If Response = vbYes Then GoTo Login_PassIf Response = vbNo Then   MsgBox "Synd for dig." & vbCrLf & "Hav en god dag", vbInformation + vbOKOnly, "End"   Form2.Visible = TrueEnd IfEnd IfIf (Saved_Name = MyUName) And (Saved_Pass = MyPass) ThenEndElseMsgBox "Nope dude, brugernavn og/eller password er forkert", vbCritical + vbOKOnly, "Error"Form2.Visible = TrueEnd IfEnd Suband a form1 with a button and code :Private Sub Command1_Click()Form2.Visible = TrueEnd SubPrivate Sub Form_Load()App.TaskVisible = False (btw not full proff because the .exe name is still visible in processes not application any help on this too is appreciated or any coder can just close app from there :/)End Sub

Edited by moderator (see edit history)

Share this post


Link to post
Share on other sites
how to prevent mutiple loginsSimple Login In Visual Basic 6

I am also beginner in vb6 but you can try to make an txtfile at login I an shared map and check if the file exists. And if you log out you delete it. You can use filesystemobject to do this. Just go to project,references and select windows script host object model. Here is the code to use it.Text1.Text is the textbox where the username is entered. Command 1 is to log in and command 2 to log out

(private declarations)Dim file as stringPrivate Sub Command1_Click()

Dim f as new filesystemobjectFile = ("C:sharedfolder") & text1.Text &(".Txt")If f.Fileexists (file) thenMsgbox "user already loged in"ElseF.Createtextfile("C:sharedfolder") & text1.Text &(".Txt")End ifEnd subPrivate Sub Command2_Click()

f.Deletefile(file)

end sub

I hope I helped you with this and sorry for my bad english I am dutch and 13 years old.greeting jason

Share this post


Link to post
Share on other sites

Dang that would be complicated for beginners to understand, I just use a simple one that can easily be found out using olydbg but i still use it, it's just like a

if textbox1.text = "pass" thenme.hideshow.form2

But I like your style of thinking XD

Share this post


Link to post
Share on other sites
log in programSimple Login In Visual Basic 6

How can I make a log in program in vb6 that after 3 retries the main form will be disabled???

-question by gaara

Share this post


Link to post
Share on other sites
how to make login page using database?Simple Login In Visual Basic 6

how to make login page uisng ado?

like we fill the username and password in database(exmple:ms access)

the the program will retrive the data from the database to see if the data is true?

sorry for my bad english.

-reply by takep

Share this post


Link to post
Share on other sites
module in VB 6Simple Login In Visual Basic 6

what's the uses of Module in VB 6 if we made a Log-in form?I hope you will reply ..Thnx! :)

-reply by angeline

Share this post


Link to post
Share on other sites
i need help with a thoughtSimple Login In Visual Basic 6

I'm trying to create a program that forces users to provide a name and documents the time they logon then returns to the logon screen when the screen saver pops up. But I'm having no luck. I took a couple programming classes a few years back but I'm stuck. Would you happen to know of a program that would do what I want or do I need to write it. 

-question by lostinme

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.