VB.NET: Proses Login dengan Database Ms Access - Part 2


Artikel ini lanjutan dari part 1.

Dan ini dia source code langkap nya:
Imports System.Data.OleDb

Public Class LoginForm1
    Dim con As New OleDbConnection   
    Dim iFail As Integer

    Sub Open_Koneksi()
        con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _
                               ";Data Source=latihan.accdb;" & _
                               "Persist Security Info=False;"
        con.Open()
    End Sub

    Function CheckLogin(ByVal UserName As String, _
            
ByVal Password As String) As Integer

       
Dim cmd As New OleDbCommand
       
Dim objValue As Object

        If Not con.State = ConnectionState.Open Then Open_Koneksi()

        Try
            cmd.Connection = con
            cmd.CommandText = "SELECT COUNT(username) AS getin " & _
                              "FROM tbluser WHERE username = " & _
                              "'" & UserName & "' AND " & _
                              "password = '" & Password & "'"

            objValue = cmd.ExecuteScalar()
            con.Close()
            If objValue Is Nothing Then
                Return 0
            Else
                Return
objValue.ToString
            End If
        Catch
myerror As OleDbException
            MessageBox.Show("Error: " & myerror.Message)
        Finally
            con.Dispose()
        End Try
        Return
0
    End Function

    Private Sub
LoginForm1_Load(sender As Object, _
                e As EventArgs) Handles Me.Load
        iFail = 0
    End Sub

    Private Sub
OK_Click(ByVal sender As System.Object, _
               
ByVal e As System.EventArgs) Handles OK.Click
       
Dim strUserName As String = UsernameTextBox.Text
       
Dim strPassword As String = PasswordTextBox.Text

        If strUserName = String.Empty Then _
           MsgBox("Username belum diisi!") : Exit Sub
        If strPassword = String.Empty Then _
           MsgBox("Password belum diisi!") : Exit Sub

        Try
            If CheckLogin(strUserName, strPassword) > 0 Then
                MsgBox("Welcome " & strUserName & "!")
                'tulis code lainnya setelah login disini
                Me.Close()
            Else
                iFail = iFail + 1
                If iFail >= 3 Then
                    MsgBox("Gagal login 3 kali." & vbCrLf & _
                           "Aplikasi akan ditutup.")
                    End
                End If

                MsgBox("Username/password Salah." & vbCrLf & _
                       "Silahkan cek lagi!")
            End If
        Catch ex As Exception
            MsgBox("Error Login: " & ex.Message)
        End Try
    End Sub


    Private Sub Cancel_Click(ByVal sender As System.Object, _
                ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

End Class


Lalu-lalu mari kita coba run login form nya:
Saat kita klik OK sebelum mengisi Username akan muncul pesan:
Saat kita klik OK sebelum mengisi Password akan muncul pesan:
 Jika kita salah mengiputkan username atau password maka muncul pesan:
Jika pengisian username dan password benar maka login akan sukses.
Jika kita melakukan kesalahan login sebanyak 3 kali maka aplikasi akan ditutup.


Click here if you like this article.


Post a Comment

4 Comments

Unknown said…
Gan erornya di 'Public Sub Open_Koneksi()' has multiple definitions with identical signatures.

itu gimana gan?
rani said…
kayaknya Open_Koneksi() nya ada lebih dari 1, cukup 1 aja dibuat reusable
Unknown said…
NullreferenceExeption not set an instance of a object

Please help me..
rani said…
@zero kifaco:
any detail why? in which part?
have you read/follor the first part?
http://rani-irsan.blogspot.co.id/2015/06/vbnet-proses-login-dengan-database-ms.html