How To Create Login Form With MySQL In Visual Studio
Preview
Hello friends, in this post I will discuss about how to create a login form in Visual Studio 2010. Almost all applications have a Login Form, because Login Form serves as a detector of user permissions that will enter into the system. Most applications that are worldwide use the login process first when users want to enter their account when they have registered like Facebook, Twitter, Instagram and so on.
Okay, first open Visual Studio and create a new project, then you need to set up a database and table to connect to your project. Read the previous article on how to connect MySQL database with Visual Studio.
MORE ARTICLE
How To Create Connection VB.Net MySQL
And here I have a table named users and there are three fields in it. user, pass and status. Status here I classify into three of them Leader, Cashier and Warehouse Employee who have access rights respectively.
Next, open Form1 (Login Form) then add some required components like the picture below
When done, add one more Form as the Main Menu that will appear if the login process succeed.
NOTES :
Components in Form2:
Components in Form2:
- MenuStrip
- StatusStrip
Perform configuration on StatusStrip, we only need two panels that will display user name and user status, as shown below
When done, reopen Form1 and enter the following ODBC library
Imports System.Data.Odbc
Public Class Form1
End Class
Then, double-click Form1 and enter the following code in Form1_Load
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call koneksinya()
cmd = New OdbcCommand("select * from users", conn)
dr = cmd.ExecuteReader
ComboBox1.Items.Add("Choose")
ComboBox1.SelectedIndex = 0
While dr.Read()
ComboBox1.Items.Add(dr.Item("status"))
End While
End Sub
NOTES :
The above code serves to enter data that is inside the Status field into ComboBox
Then double-click the Login Button and enter the following codeThe above code serves to enter data that is inside the Status field into ComboBox
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If TextBox1.Text = "" Or TextBox2.Text = "" Or ComboBox1.SelectedIndex = 0 Then
MessageBox.Show("Complete All Fields")
Else
Call koneksinya()
cmd = New OdbcCommand("select * from users where user = '" & TextBox1.Text & "' and pass = '" & TextBox2.Text & "' and status = '" & ComboBox1.Text & "'", conn)
dr = cmd.ExecuteReader
dr.Read()
If Not dr.HasRows Then
MessageBox.Show("Login failed Check your username, password and status")
Else
Form2.Show()
Form2.panel1.Text = "USER NAME : " + dr.Item("user")
Form2.panel2.Text = "USER STATUS : " + dr.Item("status")
If ComboBox1.Text = "LEADER" Then
Form2.BARANGToolStripMenuItem.Enabled = True
Form2.SUPPLIERToolStripMenuItem.Enabled = True
Form2.USERToolStripMenuItem.Enabled = True
Form2.GANTIPASSWORDToolStripMenuItem.Enabled = True
Form2.PENJUALANToolStripMenuItem.Enabled = False
Form2.PEMBELIANToolStripMenuItem.Enabled = False
Form2.LAPORANHARIANToolStripMenuItem.Enabled = True
Form2.LAPORANBULANANToolStripMenuItem.Enabled = True
ElseIf ComboBox1.Text = "CASHIER" Then
Form2.BARANGToolStripMenuItem.Enabled = False
Form2.SUPPLIERToolStripMenuItem.Enabled = False
Form2.USERToolStripMenuItem.Enabled = False
Form2.GANTIPASSWORDToolStripMenuItem.Enabled = True
Form2.PENJUALANToolStripMenuItem.Enabled = True
Form2.PEMBELIANToolStripMenuItem.Enabled = False
Form2.LAPORANHARIANToolStripMenuItem.Enabled = False
Form2.LAPORANBULANANToolStripMenuItem.Enabled = False
Else
Form2.BARANGToolStripMenuItem.Enabled = False
Form2.SUPPLIERToolStripMenuItem.Enabled = False
Form2.USERToolStripMenuItem.Enabled = False
Form2.GANTIPASSWORDToolStripMenuItem.Enabled = True
Form2.PENJUALANToolStripMenuItem.Enabled = False
Form2.PEMBELIANToolStripMenuItem.Enabled = True
Form2.LAPORANHARIANToolStripMenuItem.Enabled = False
Form2.LAPORANBULANANToolStripMenuItem.Enabled = False
End If
TextBox1.Clear()
TextBox2.Clear()
ComboBox1.SelectedIndex = 0
End If
End If
End Sub
NOTES :
- The program will detect if the username, password and status are empty, a warning message will appear to fill in all fields.
- If all the fields are filled, then the program will match the username, password and status entered together with the contents of the database.
- If there is not match, it will display a warning message that the login failed, if appropriate then directly display the Main Menu (Form2).
- At the same time when the Main Menu appears, Panel username and Panel status will be filled in accordance with the previous Login Form.
- Then the program will identify if the status Panel is written as LEADER, then only the TRANSACTION menu that can not be accessed, if as CASHIER, then that can be accessed is Sub Menu SALES and UTILIY, besides, it can only access Sub Menu PURCHASE and UTILITY.
Here you are free, specify user permissions according to the menu you entered
And finally, double-click the Exit Button and enter the following code to close the Login Form
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Close()
End Sub
When finished, the program is ready to run.