Membuat Form Master Pelanggan
Layout diatas adalah bentuk form untuk kita meregistrasikan pelanggan.
Database yang sudah ada kita tambahkan tabelnya dengan tabel pelanggan --> kita berikan nama tbPelanggan. Struktur tabel yang diperlukan sebagai berikut
field name tipe data length keterangan
kode text 10 --> diset sebagai primary key
nama text 50
alamat text 50
telp text 15
kota text 15 --> apabila diperlukan dibuat tabel tersendiri
Hampir sama dengan form master barang, komponen yang kita butuhkan sebagai berikut :
Tambahkan form baru dengan cara klik menu project ==> Add Form dan dari dialog yang muncul pilih Tab New ===> pilih Form
Berikan Nama FrmPelanggan ===> tambahkan SSTAB objek agar terlihat lebih manis, atur properti dan font sesuai dengan keinginan.
Objek Properties Value
Label1 ===> Caption Kode Pelanggan
Label2 ===> Caption Nama
Label3 ===> Caption Alamat
Label4 ===> Caption Telepon
Label5 ===> Caption Kota
TextBox => Name Text1
TextBox => Name Text2
TextBox => Name Text3
TextBox => Name Text4
TextBox => Name Text5
Tambahkan Command Button
Objek Properties Value
CommandButton Name CmdTambah
Caption Tambah
CommandButton Name CmdSimpan
Caption Simpan
CommandButton Name CmdHapus
Caption Hapus
CommandButton Name CmdUbah
Caption Ubah
CommandButton Name CmdBatal
Caption Batal
CommandButton Name CmdSelesai
Caption Selesai
Atur Form seperti gambar layout diatas.
Kemudian Berikut Scriptnya :
Dim Cnn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Private Sub CmdBatal_Click()
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
Call Kosong
CmdUbah.Enabled = False
CmdHapus.Enabled = False
CmdBatal.Enabled = False
CmdSimpan.Enabled = False
Text1.SetFocus
End Sub
Private Sub CmdHapus_Click()
Dim Msql As String
Dim Konfirmasi As String
If Text1.Text <> "" And Text1.Enabled = False Then
Cnn.BeginTrans
Konfirmasi = MsgBox("Mau Hapus Record ??? ", vbYesNo + vbCritical, "Konfirmasi")
If Konfirmasi = vbYes Then
'Menghapus record pada tabel
Msql = "Delete From tbPelanggan" & _
" Where Kode='" & Text1.Text & "'"
Cnn.Execute (Msql)
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
CmdTambah.Enabled = False
CmdSimpan.Enabled = False
CmdHapus.Enabled = False
CmdUbah.Enabled = False
CmdBatal.Enabled = False
Call Kosong
Cnn.CommitTrans
End If
End If
End Sub
Private Sub CmdSelesai_Click()
Cnn.Close
Set Cnn = Nothing
Unload Me
Call Menu_Bisa
End Sub
Private Sub CmdSimpan_Click()
Dim Msql As String
If Text1.Text <> "" Then
Cnn.BeginTrans
If Text1.Enabled = True Then
'menambah record pada tabel
Msql = "Insert into tbPelanggan(Kode, Nama, Alamat, Telp, Kota)" & _
" Values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "','" & Text4.Text & "','" & Text5.Text & "')"
'Mengeksekusi perintah SQL
Cnn.Execute (Msql)
Else
'Mengubah record pada tabel
Msql = "Update tbPelanggan set " & _
" Nama='" & Text2.Text & "'," & _
" Alamat='" & Text3.Text & "'," & _
" Telp='" & Text4.Text & "'," & _
" Kota='" & Text5.Text & "'" & _
" Where Kode='" & Text1.Text & "'"
'Mengeksekusi printah SQL
Cnn.Execute (Msql)
End If
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Enabled = False
CmdSimpan.Enabled = False
CmdBatal.Enabled = False
CmdTambah.Enabled = True
CmdHapus.Enabled = True
CmdUbah.Enabled = True
Cnn.CommitTrans
End If
End Sub
Private Sub CmdTambah_Click()
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
CmdTambah.Enabled = False
CmdSimpan.Enabled = False
CmdHapus.Enabled = False
CmdUbah.Enabled = False
CmdBatal.Enabled = False
Call Kosong
Text1.SetFocus
End Sub
Private Sub CmdUbah_Click()
Dim Msql As String
If Text1.Text <> "" And Text1.Enabled = False Then
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
Text2.SetFocus
End If
CmdUbah.Enabled = False
CmdHapus.Enabled = False
CmdTambah.Enabled = False
CmdSimpan.Enabled = True
End Sub
Private Sub Form_Activate()
Me.Left = (Main.ScaleHeight - Me.ScaleHeight) / 2
Me.Top = (Main.ScaleWidth - Me.ScaleWidth) / 2
Text1.SetFocus
End Sub
Private Sub Form_Load()
'Membuat Sebuah Koneksi
Set Cnn = New ADODB.Connection
'Membuka Koneksi dengan ODBC Driver Aplikasi
Cnn.Open "inventori"
CmdTambah.Enabled = False
CmdSimpan.Enabled = False
CmdUbah.Enabled = False
CmdHapus.Enabled = False
CmdBatal.Enabled = False
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text2.SetFocus
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If
End Sub
Private Sub Text1_LostFocus()
Dim Msql As String
If Text1.Text <> "" Then
Cnn.BeginTrans
'Mencari Kode pada Tabel
Msql = "Select * From tbPelanggan " & _
" Where Kode='" & Text1.Text & "'"
Set rs = Cnn.Execute(Msql)
'Jika Kode sudah ada tampilkan
If Not rs.EOF Then
'Menampilkan data dalam tabel ke form
Text2.Text = rs.Fields("Nama")
Text3.Text = rs.Fields("Alamat")
Text4.Text = rs.Fields("Telp")
Text5.Text = rs.Fields("Kota")
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Enabled = False
CmdTambah.Enabled = True
CmdHapus.Enabled = True
CmdUbah.Enabled = True
Else
CmdSimpan.Enabled = True
CmdBatal.Enabled = True
End If
rs.Close
Cnn.CommitTrans
End If
End Sub
Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text3.SetFocus
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If
End Sub
Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text4.SetFocus
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If
End Sub
Private Sub Text4_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
Text5.SetFocus
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If
End Sub
Private Sub Text5_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys vbTab
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If
End Sub
Sub Kosong()
'mengosongkan textbox dan maskededit
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
End Sub
0 Response to "Membuat Form Master Pelanggan"
Posting Komentar