Imports System.Data.Odbc
Public Class frmUbahPassword

    Private Sub frmUbahPassword_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
        If Asc(e.KeyChar) = Keys.Enter Then
            SendKeys.Send("{tab}")
        End If
    End Sub

    Private Sub frmUbahPassword_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnSimpan.Enabled = False
        txtPassBaru.Enabled = False
        txtpassLama.UseSystemPasswordChar = True
        txtPassBaru.UseSystemPasswordChar = True
    End Sub

    Private Sub btnSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSimpan.Click
        If txtPassBaru.Text = "" Then Exit Sub
        If MessageBox.Show("Yakin akan mengganti Password..?", "Konfirmasi", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
            cmd = New OdbcCommand("Select * from tbuser where user_id='" & cUserId & "'", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                Dim sqledit As String = "Update tbuser set " & _
                "password=md5('" & txtPassBaru.Text & "') where user_id='" & cUserId & "'"
                cmd = New OdbcCommand(sqledit, Conn)
                cmd.ExecuteNonQuery()
                MsgBox("Password berhasil di Update", MsgBoxStyle.Information, "Perhatian...!")
            End If
        End If
        btnSimpan.Enabled = False

    End Sub

    Private Sub btnTutup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTutup.Click
        'Conn.Close()
        Me.Visible = False
        frmLogin.Show()
    End Sub

    Private Sub txtpassLama_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtpassLama.KeyPress
        If txtpassLama.Text = "" Then Exit Sub
        If e.KeyChar = Chr(13) Then
            cmd = New OdbcCommand("Select * from tbuser where user_Id='" & cUserId & "' and password=md5('" & txtpassLama.Text & "')", Conn)
            rd = cmd.ExecuteReader
            rd.Read()
            If rd.HasRows Then
                txtPassBaru.Enabled = True
            Else
                MsgBox("Maaf..., Password Salah", MsgBoxStyle.Critical, "Perhatian...!")
                txtpassLama.Text = ""
                txtpassLama.Focus()
                btnSimpan.Enabled = False
            End If
        End If
    End Sub

    Private Sub txtPassBaru_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPassBaru.TextChanged
        btnSimpan.Enabled = True
    End Sub

    Private Sub txtPassBaru_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtPassBaru.Validated

        If txtpassLama.Text = txtPassBaru.Text Then
            MsgBox("Maaf..., Ganti dengan Password Baru", MsgBoxStyle.Critical, "Perhatian...!")
            txtPassBaru.Text = ""
            txtPassBaru.Focus()
            btnSimpan.Enabled = False
        Else
            btnSimpan.Enabled = True
        End If
    End Sub
End Class