You are on page 1of 10

Imports System.Data.

OleDb
Public Class frmLogIn
Public mPengguna As String
Public mPassword As String
Dim mCount As Integer
#Region "Konsep Struktur"
Public Function Masuk()
Try
Query = "SELECT *FROM MasterUser WHERE Nama = '" & mPengguna & "' and Pass =
'" & mPassword & "'"
daData = New OleDbDataAdapter(Query, conn)
dsData = New DataSet
daData.Fill(dsData)
Return Query
Catch ex As Exception
Return 0
End Try
End Function
Public Sub keluar()
Try
Dispose()
Catch ex As Exception
End Try
End Sub
#End Region

Private Sub cmdMasuk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles cmdMasuk.Click
Try
mPengguna = txtNamaUser.Text
mPassword = txtPassword.Text
Masuk()

If dsData.Tables(0).Rows.Count = 0 Then
If mCount < 3 Then
MsgBox("Anda tidak terdaftar sebagai pengguna", MsgBoxStyle.Critical
+ MsgBoxStyle.OkCancel, "Pesan")
txtNamaUser.Focus()
mCount = mCount + 1
Me.txtNamaUser.Text = ""
Me.txtPassword.Text = ""
Else
MsgBox("Tiga kali gagal login....", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")

End If
Else
frmMenuUtama.ToolStripStatusLabel1.Text = "User : " & txtNamaUser.Text
mKodeUser = dsData.Tables(0).Rows(0).Item(0)
mNamaUser = txtNamaUser.Text
dsData.Dispose()
dsData.Reset()
daData.Dispose()
Me.Hide()
Me.txtNamaUser.Text = ""
Me.txtPassword.Text = ""
frmMenuUtama.Show()
End If

Catch ex As Exception

End Try
End Sub

Private Sub frmLogIn_FormClosed(ByVal sender As Object, ByVal e As


System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
End Sub

Private Sub frmLogIn_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles MyBase.Load
Try
KoneksiKeAcess()
txtNamaUser.Focus()
Catch ex As Exception
MsgBox("Koneksi ke database gagal")
End Try
End Sub

Private Sub txtPassword_KeyUp(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyEventArgs) Handles txtPassword.KeyUp
If e.KeyCode = Keys.Up Then
txtNamaUser.Focus()
End If
End Sub

Private Sub txtPassword_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles txtPassword.TextChanged

End Sub

Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles cmdKeluar.Click
keluar()
End Sub
End Class

Imports System.Data.OleDb
Public Class frmCustomer
Public KodeCustomer As String
Public NamaCustomer As String
Public Alamat As String
Public Kota As String
Public NoTelpon As String

Private Sub PosisiList()


Try
With lvcustomer.Columns
.Add("Kode", 50)
.Add("Nama Customer", 150)
.Add("Alamat", 180)
.Add("Kota", 100)
.Add("No.Telepon", 90)
End With
Catch ex As Exception
End Try
End Sub
Private Sub IsiList()
Try
Query = "SELECT * FROM MasterCustomer ORDER BY NamaCustomer ASC"
daData = New OleDbDataAdapter(Query, conn)
dsData = New DataSet
daData.Fill(dsData)
lvcustomer.Items.Clear()
For a = 0 To dsData.Tables(0).Rows.Count - 1
With lvcustomer
.Items.Add(dsData.Tables(0).Rows(a).Item(0))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(1))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(2))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(3))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(4))
If (a Mod 2 = 0) Then
.Items(a).BackColor = Color.LightSteelBlue
Else
.Items(a).BackColor = Color.Lavender
End If
End With
Next
Catch ex As Exception

End Try
End Sub

#Region "Konsep Struktur"


'Untuk membersihkan atribut'
Public Sub Baru()
Try
KodeCustomer = ""
NamaCustomer = ""
Alamat = ""
Kota = ""
NoTelpon = ""
Catch ex As Exception
End Try
End Sub

'Untuk menyimpan data'


Public Function SimpanData()
Try
Query = "INSERT into MasterCustomer VALUES('" & KodeCustomer & "', '" &
NamaCustomer & "', '" & Alamat & "', '" & Kota & "', '" & NoTelpon & "')"
daData = New OleDbDataAdapter(Query, conn)
dsData = New DataSet
daData.Fill(dsData)
Return Query
Catch ex As Exception
MsgBox("Data dengan kode ini sudah ada", MsgBoxStyle.Exclamation +
MsgBoxStyle.OkOnly, "Pesan")
Return Query
End Try
End Function

'Untuk edit data'


Public Function EditData()
Try
Query = "UPDATE MasterCustomer SET NamaCustomer = '" & NamaCustomer & "',
Alamat = '" & Alamat & "', Kota = '" & Kota & "', NoTelepon = '" & NoTelpon & "' WHERE
KodeCustomer = '" & KodeCustomer & "'"
daData = New OleDbDataAdapter(Query, conn)
dsData = New DataSet
daData.Fill(dsData)
Return Query
Catch ex As Exception
Return 0
End Try
End Function

'Untuk hapus data'


Public Function HapusData()
Try
Query = "DELETE FROM MasterCustomer WHERE KodeCustomer = '" & KodeCustomer &
"'"
daData = New OleDbDataAdapter(Query, conn)
dsData = New DataSet
daData.Fill(dsData)
Return Query
Catch ex As Exception
Return 0
End Try
End Function

'Untuk menghancurkan objek'


Public Sub Keluar()
Try
Dispose()
Catch ex As Exception
End Try
End Sub
#End Region

Private Sub AmbilDataDariListView()


Try
With lvcustomer.SelectedItems
txtkode.Enabled = False
txtkode.Text = .Item(0).SubItems(0).Text
txtnama.Text = .Item(0).SubItems(1).Text
txtalamat.Text = .Item(0).SubItems(2).Text
txtkota.Text = .Item(0).SubItems(3).Text
txtnotelpon.Text = .Item(0).SubItems(4).Text
End With
Catch ex As Exception
End Try
End Sub

Private Sub BersihkanIsian()


Try
txtkode.Clear()
txtkode.Enabled = True
txtkode.Focus()
txtnama.Clear()
txtalamat.Clear()
txtkota.Clear()
txtnotelpon.Clear()
cmdSimpan.Enabled = True
cmdEdit.Enabled = False
cmdHapus.Enabled = False
Catch ex As Exception
End Try
End Sub

Private Sub frmCustomer_Activated(ByVal sender As Object, ByVal e As


System.EventArgs) Handles Me.Activated
txtkode.Focus()
End Sub

Private Sub frmCustomer_FormClosed(ByVal sender As Object, ByVal e As


System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
Me.Dispose()
End Sub
Private Sub Kunci()
txtkode.Enabled = False
txtnama.Enabled = False
txtalamat.Enabled = False
txtkota.Enabled = False
txtnotelpon.Enabled = False

End Sub
Private Sub Unkunci()
txtkode.Enabled = True
txtnama.Enabled = True
txtalamat.Enabled = True
txtkota.Enabled = True
txtnotelpon.Text = True
End Sub
Private Sub frmCustomer_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Try
Kunci()
KoneksiKeAcess()
PosisiList()
IsiList()
cmdEdit.Enabled = False
cmdHapus.Enabled = False
cmdSimpan.Enabled = False
Catch ex As Exception
End Try
End Sub

Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)


Handles cmdEdit.Click
Try
Dim A As String

If txtkode.Text = "" Then


MsgBox("Pilih data yang akan di-edit", MsgBoxStyle.Information +
MsgBoxStyle.OkOnly, "Pesan")
Else
A = MsgBox("Benar akan di-edit", MsgBoxStyle.Question +
MsgBoxStyle.OkCancel, "Informasi")
Select Case A
Case vbCancel
BersihkanIsian()
Exit Sub
Case vbOK
Try
KodeCustomer = txtkode.Text
NamaCustomer = txtnama.Text
Alamat = txtalamat.Text
Kota = txtkota.Text
NoTelpon = txtnotelpon.Text
EditData()
IsiList()
BersihkanIsian()
Catch ex As Exception
End Try
End Select
End If
Catch ex As Exception
End Try
End Sub

Private Sub txtkode_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtkode.KeyPress
If e.KeyChar = Chr(13) Then
If txtkode.Text = "" Then
MsgBox("Kode tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")
txtkode.Focus()
Else
txtnama.Focus()
End If
End If
End Sub

Private Sub txtnama_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtnama.KeyPress
If e.KeyChar = Chr(13) Then
If txtnama.Text = "" Then
MsgBox("Nama customer tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")
txtnama.Focus()
Else
txtalamat.Focus()
End If
End If
End Sub

Private Sub txtalamat_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtalamat.KeyPress
If e.KeyChar = Chr(13) Then
If txtalamat.Text = "" Then
MsgBox("Alamat tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")
txtalamat.Focus()
Else
txtkota.Focus()
End If
End If
End Sub

Private Sub txtkota_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtkota.KeyPress
If e.KeyChar = Chr(13) Then
If txtkota.Text = "" Then
MsgBox("Kota tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")
txtkota.Focus()
Else
txtnotelpon.Focus()
End If
End If
End Sub

Private Sub txtnotelpon_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles txtnotelpon.KeyPress
If e.KeyChar = Chr(13) Then
If txtnotelpon.Text = "" Then
MsgBox("No.Telepon tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Pesan")
txtnotelpon.Focus()
Else
If txtnama.Enabled = False Then
cmdEdit.Focus()
Else
cmdSimpan.Focus()
End If
End If
End If
End Sub

Private Sub lvcustomer_Click(ByVal sender As Object, ByVal e As System.EventArgs)


Handles lvcustomer.Click
Try
cmdSimpan.Enabled = False
cmdEdit.Enabled = True
cmdHapus.Enabled = True
Catch ex As Exception

End Try
End Sub

Private Sub lvcustomer_KeyPress(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyPressEventArgs) Handles lvcustomer.KeyPress
If e.KeyChar = Chr(13) Then
txtnama.Focus()
End If
End Sub

Private Sub lvcustomer_KeyUp(ByVal sender As Object, ByVal e As


System.Windows.Forms.KeyEventArgs) Handles lvcustomer.KeyUp
If e.KeyCode = Keys.Escape Then
BersihkanIsian()
End If
End Sub

Private Sub lvcustomer_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles lvcustomer.SelectedIndexChanged
Try
AmbilDataDariListView()
Catch ex As Exception

End Try
End Sub

Private Sub cmdTambah_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles cmdTambah.Click
Try
Unkunci()
Baru()
txtkode.Text = KodeCustomer
txtnama.Text = NamaCustomer
txtalamat.Text = Alamat
txtkota.Text = Kota
txtnotelpon.Text = NoTelpon
txtkode.Enabled = True
txtkode.Focus()
cmdSimpan.Enabled = True
cmdEdit.Enabled = False
cmdHapus.Enabled = False
Catch ex As Exception
End Try
End Sub

Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles cmdSimpan.Click
Try
If txtkode.Text = "" Then
MsgBox("Kode tidak boleh kosong", MsgBoxStyle.Critical +
MsgBoxStyle.OkOnly, "Information")
txtnama.Focus()
Else
'Memanggil Prosedur Simpan Data
KodeCustomer = txtkode.Text
NamaCustomer = txtnama.Text
Alamat = txtalamat.Text
Kota = txtkota.Text
NoTelpon = txtnotelpon.Text
SimpanData()
IsiList()
BersihkanIsian()
End If
Catch ex As Exception
End Try
End Sub

Private Sub cmdHapus_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles cmdHapus.Click
Try
If (txtkode.Text) = "" Then
MsgBox("Pilih data yang dihapus", MsgBoxStyle.Information +
MsgBoxStyle.OkOnly, "Hapus data")
Exit Sub
Else
Dim A As String

A = MsgBox("Benar akan dihapus..." & txtkode.Text & " : " & txtnama.Text
& " : " & txtalamat.Text & " : " & txtkota.Text & " : " & txtnotelpon.Text,
MsgBoxStyle.OkCancel, "Hapus Data")
Select Case A
Case vbCancel
BersihkanIsian()
Exit Sub
Case vbOK
'Memanggil Prosedur Hapus Data
KodeCustomer = txtkode.Text
HapusData()
IsiList()
BersihkanIsian()
End Select
End If
Catch ex As Exception
End Try
End Sub

Private Sub cmdKeluar_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles cmdKeluar.Click
Keluar()
End Sub

Private Sub txtData_TextChanged(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles txtData.TextChanged
Try
' If cboKriteria.Text = "Kode Customer" Then
' Query = "SELECT * FROM MasterCustomer WHERE KodeCustomer Like '" &
txtData.Text & "%" & "' ORDER BY KodeCustomer"
' Else
Query = "SELECT * FROM MasterCustomer WHERE NamaCustomer Like '" &
txtData.Text & "%" & "' ORDER BY NamaCustomer"
'End If

daData = New OleDbDataAdapter(Query, conn)


dsData = New DataSet
daData.Fill(dsData)
lvcustomer.Items.Clear()
For a = 0 To dsData.Tables(0).Rows.Count - 1
With lvcustomer
.Items.Add(dsData.Tables(0).Rows(a).Item(0))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(1))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(2))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(3))
.Items(a).SubItems.Add(dsData.Tables(0).Rows(a).Item(4))
If (a Mod 2 = 0) Then
.Items(a).BackColor = Color.LightSteelBlue
Else
.Items(a).BackColor = Color.Lavender
End If
End With
Next
Catch ex As Exception

End Try
End Sub

End Class

You might also like