Membuat Form Master Barang
Berikut Layout Form Master Data Barang.
Komponen yang kita butuhkan antara lain
SSTAB, label, textbox, button, dan frame
Penambahan komponen ini dapat dilakukan dengan cara klik--> menu project dan --> pilih component
Pilih dari daftar object yang akan kita gunakan antara lain, sebagai berikut :
1. Crystal Report Control --> berguna untuk membuat report atau laporan.
2. Microsoft ADO Data Control 6.0 (SP4) (oledb).
3. Microsoft DataGrid Control 6.0 (SP5) (oledb)
4. Microsoft Masked Edit Control 6.0 (SP3)
5. Microsoft Tabbed Dialog Control 6.0 (SP5)
6. Microsoft Windows Common Controls-2 6.0 (SP4)
Penulis menggunakan o/s windows xp sp2 dan crystal report 8 --> dan Tentunya microsoft visual studio 2000.
Berikut struktur table barang --> simpan dengan nama tbBarang
Detail Table Inventory Design | ||||
Table Name : tbBarang | ||||
No. | Field Name | Type Data | Length | Description |
1 | Kode | text | 10 | |
2 | Nama | text | 40 | |
3 | Satuan | text | 10 | |
4 | HargaBeli | number | double | |
5 | HargaJual | number | double | |
Atur form dengan properties sebagai berikut :
Label tidak dibahas (karena biasanya sudah tau untuk penamaan komponen, jadi kita lanjut ke input (isian). Siapkan 3 textbox dan 2 Masked Edit Box
Seperti layout diatas.
berikut script pada form barang, ingat kita menggunakan ADO teknik bukan DAO
jadi jangan lupa tambahkan reference anda dengan cara klik menu --> project --> reference
dan pilih Microsoft ActiveX Data Object 2.0 library.
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
MaskEdBox2.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 tbBarang" & _
" Where Kode='" & Text1.Text & "'"
Cnn.Execute (Msql)
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
MaskEdBox1.Enabled = True
MaskEdBox2.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 tbBarang(Kode, Nama, Satuan, HargaBeli, HargaJual)" & _
" Values('" & Text1.Text & "','" & Text2.Text & "','" & Text3.Text & "'," & Val(MaskEdBox1.Text) & "," & MaskEdBox2.Text & ")"
'Mengeksekusi perintah SQL
Cnn.Execute (Msql)
Else
'Mengubah record pada tabel
Msql = "Update tbBarang set " & _
" Nama='" & Text2.Text & "'," & _
" Satuan='" & Text3.Text & "'," & _
" HargaBeli=" & Val(MaskEdBox1.Text) & "," & _
" HargaJual=" & Val(MaskEdBox2.Text) & "" & _
" Where Kode='" & Text1.Text & "'"
'Mengeksekusi printah SQL
Cnn.Execute (Msql)
End If
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
MaskEdBox1.Enabled = False
MaskEdBox2.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
MaskEdBox2.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
MaskEdBox2.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 MaskEdBox2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
SendKeys vbTab
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 tbBarang " & _
" 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("Satuan")
MaskEdBox1.Text = rs.Fields("HargaBeli")
MaskEdBox2.Text = rs.Fields("HargaJual")
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
MaskEdBox1.Enabled = False
MaskEdBox2.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 = ""
MaskEdBox2.Text = ""
End Sub
18 Desember 2013 pukul 22.38
Kok saya gk ada crystal report control nya ?