vb-beginner

Never stop to learn...

  • Increase font size
  • Default font size
  • Decrease font size
Home Beginner Create login form

Create login form

Print PDF
If we create a program office or other program, sometimes we want to create a Login Form for Users. By using the Login Form, use an application to be limited and that you can use any application that has access only. So that the data have to be safe. Here are the steps to create a simple Login Form:


  • First, create a database and give the name of the database with the name login.mdb (author create a database using the Visual Data Manager in the Visual Basic 6.0)
  • Then create a table and table name with the name info_login.
  • On the table Info_login, add fields username and password fields. Data type for each of the Text, and for the field size is 50. Field username is the primary key.
  • Give the value of each field:
    • Field username : Admin
    • Field password : Admin

After the database is ready, create a login form as below:

To Text2, sign on * PasswordChar properties.

After that, add the component library, in a way:

 

  • Click the Project menu
  • And then References
  • Then, marked on the Microsoft ActiveX Data Objects 2.7 Library.
  • Then click Ok.

Once you add the component library, enter the code under this way:

  • Click View menu
  • Then click Code
Code for the Login Form:

Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset

Dim try As Long

Private Sub Command1_Click()
On Error GoTo Error

try = try + 1

Set cn = New ADODB.Connection
Set rs = New ADODB.Recordset

With cn
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\login.mdb" & ";Persist Security Info=False"
.Open
End With

If try <= 3 Then
With rs
.Open "SELECT * FROM info_login WHERE username ='" & Text1.Text & "' AND password='" & Text2.Text & "';", cn, adOpenKeyset, adLockOptimistic
If .RecordCount <= 0 Then
MsgBox "Invalid username or password!"
Else
MsgBox "Login success"
End If
End With
Else
MsgBox "Please contact your administrator if you lost information login!"
Unload Me
End If

Error:
If Err.Number = 0 Then Exit Sub
MsgBox Err.Description, vbOKOnly, "Error " & Err.Number
Err.Clear
End Sub

Private Sub Command2_Click()
Unload Me
End Sub

Private Sub Form_Load()
try = 0
End Sub

 

If your database is on drive C, you can change the connection. ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & App.Path & "\ login.mdb" & "Persist Security Info = False ". For example, your database is located on the C: \ login.mdb, then. ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source =" & "C: \ login.mdb" & "Persist Security Info = False "

Function of As Long Dim try is to sign given the opportunity. Sign that the opportunity is given the opportunity 3 (you can change with the opportunity to change the value on the value I try <= 3 Then)
Last Updated on Tuesday, 27 January 2009 06:59