I found this code:


If Me.txtUserName.Text = "user" Then 'Replace the string "user" with whatever you want.

If Me.txtPassword.Text = "password" Then 'Replace the string "password" with what ever you want.

Form1.Show() 'This call represents the main entry to your application. It can be an MDI form or even a startup function

Me.Close() 'Close the login form after successful login, otherwise keep requesting for login info until user hits Cancel.


MsgBox("Could not verify your identity. Invalid username or password.")



End If

End If


But it just work for one user, username = user and password = password.

I want an easy code like this that has several users in the code.. lets say i want 5 users:

user1 ; password1

user2 ; password2

user3 ; password3

And so on..

And i want that when user1 login, the form sends user1 to FormUSER1.

If user2 login, the form send user2 to FormUSER2 and so on...

I hope you can help me!


Re: Visual Basic General basic login form with several users

-=[ Bjorn ]=-


it's probably not one of the best solutions but it will certainly work if you would use a Hashtable to store your users and their passwords.

A hashtable takes a key, in this case the username, and a value, in this case a password.

So adding users is like (pseudocode)

Code Snippet

Hashtable a = new Hashtable()

a.add("user1", "password1")

a.add("user2", "password2")

note that you can't have a user with different passwords as the username is a key value.

Then you can easily check if a user exists in the hashtable and then compare it value with the given password from the textfield (pseudocode)

Code Snippet

if a.keyExists(me.txtUsername.Text) Then

if a.getValue(me.txtUsername.Text).toString = me.txtPassword.Text then

' successfully logged in.

This al is written in pseudo code, but have a look at hashtable documentation and i think you will find your way in it.

Re: Visual Basic General basic login form with several users


Thanks Bjorn.
That does look very good and simple.
But i have three small questions:

1. Where can i find a HashTable

2. How can i make a database with one table for usernames and another one for passwords
(2) This database thingy is realy hard for me, yesterday i tried to make a table but i didn't succed, it just turned into a mess.
So i would like a link to a full guide of how to make a database and how to make tables and columns and that...
(I would be very gratefull)

3. If someone can answer this, please skip question #2.
If i make a table and make that name (on the top) to UserName and i make a column inside named user1 then i can use a code that looks for the table UserName and inside it, looks for the users that are there
(Same thing for password )
(I already have an code for validating from an database but if someone would like to post, please, be free to do it, maybe your code is better)

//Greetings, Zeelia

Re: Visual Basic General basic login form with several users

-=[ Bjorn ]=-

Hi Zeelia,

depending on the number of users you want to store and how dynamic this list is, you don't exactly need a database to store this information.

If you have a small number of users which doesn't change much you could use the HashTable collection object.

Here is an example using the Hashtable collection object

User Class

Code Snippet

Public Class User

Private strUsername As String

Private strPassword As String

Sub New(ByVal username As String, ByVal password As String)

Me.strUsername = username

Me.strPassword = password

End Sub

ReadOnly Property Username() As String


Return Me.strUsername

End Get

End Property

ReadOnly Property Password() As String


Return Me.strPassword

End Get

End Property

End Class

Main application

Code Snippet

Imports System.Collections.Hashtable

Module Module1

Private users As Hashtable = New Hashtable

Sub Main()

Dim user1 As New User("username1", "pass")

Dim user2 As New User("username2", "pass231")

Dim user3 As New User("username3", "pass")

users.Add(user1.Username, user1)

users.Add(user2.Username, user1)

users.Add(user3.Username, user1)

Console.WriteLine("user test 1: " & checkUser("username1", "asdfjhk"))

Console.WriteLine("user test 2: " & checkUser("username1", "pass"))

Console.WriteLine("user test 3: " & checkUser("username2", "pass231"))


End Sub

Function checkUser(ByVal username As String, ByVal password As String) As Boolean

If users.ContainsKey(username) Then

Dim user As User = CType(users(username), User)

If user.Password Is password Then

Return True

End If

End If

Return False

End Function

End Module

Hope this helps. If you want to continue with a database you should first read more about it. It's also quite 'easy' to setup something like that (depending on the database you are using). There should be enough examples on the internet.

good luck!