Membuat Form Master Stok












Sekarang kita membuat form master stok, masih menggunakan database yang sama.
Kita tambahkan tabel stok, simpan dengan nama tbStok

Berikut Struktur datanya,

Table Name : tbStok



No. Field Name Type Data Length Description Allow Nulls
1 KodeBarang text 10 0
2 NamaBarang text 50 0
3 Satuan text 10 0
4 Jumlah number double 0
Hampir sama dengan form master pelanggan 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 FrmStok ===> tambahkan SSTAB objek agar terlihat lebih manis, atur properti dan font sesuai dengan keinginan.

Objek Properties Value
Label1 ===> Caption Kode Barang
Label2 ===> Caption Nama Barang
Label3 ===> Caption Satuan
Label4 ===> Caption Jumlah


TextBox => Name Text1
TextBox => Name Text2
TextBox => Name Text3
TextBox => Name MaskedEditBox1

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
MaskEdBox1.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("Apakah Anda Yakin ingin menghapus Record ??? ", vbYesNo + vbCritical, "Konfirmasi")
If Konfirmasi = vbYes Then

'Menghapus record pada tabel
Msql = "Delete From tbStok" & _
" Where KodeBarang='" & Text1.Text & "'"

Cnn.Execute (Msql)

Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
MaskEdBox1.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 tbStok(KodeBarang, NamaBarang, Satuan, Jumlah)" & _
" Values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "'," & Val(MaskEdBox1.Text) & ")"

'Mengeksekusi perintah SQL
Cnn.Execute (Msql)

Else

'Mengubah record pada tabel
Msql = "Update tbStok set " & _
" NamaBarang ='" & Text2.Text & "'," & _
" Satuan='" & Text3.Text & "'," & _
" Jumlah =" & Val(MaskEdBox1.Text) & "," & _
" Where KodeBarang='" & Text1.Text & "'"

'Mengeksekusi printah SQL
Cnn.Execute (Msql)
End If

Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
MaskEdBox1.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
MaskEdBox1.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
MaskEdBox1.Enabled = True
Text2.SetFocus

End If

CmdUbah.Enabled = False
CmdHapus.Enabled = False
CmdTambah.Enabled = False

CmdSimpan.Enabled = True
End Sub

Private Sub Form_Activate()
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

Me.Top = (Main.ScaleHeight - Me.ScaleHeight) / 2
Me.Left = (Main.ScaleWidth - Me.ScaleWidth) / 2

End Sub

Private Sub MaskEdBox1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
MaskEdBox2.SetFocus
Else
Dim StrAngka As String
StrAngka = "0123456789"

If KeyAscii > 26 Then
If InStr(StrAngka, Chr(KeyAscii)) = 0 Then
KeyAscii = 0
End If
End If
End If
End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then
SendKeys vbTab
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 tbStok " & _
" Where KodeBarang='" & 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("NamaBarang")
Text3.Text = rs.Fields("Satuan")
MaskEdBox1.Text = rs.Fields("Jumlah")

Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
MaskEdBox1.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
MaskEdBox1.SetFocus
Else
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End If

End Sub

Sub Kosong()

'mengosongkan textbox dan maskededit

Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
MaskEdBox1.Text = ""

End Sub

Read Users' Comments (0)

0 Response to "Membuat Form Master Stok"