Membuat Database Barang menggunakan Visual Basic 6.0
Sebelum membuat form diatas kita harus membuat database pada Microsoft Acces dengan nama Data dan tabelnya dengan nama Barang dengan field-field dibawah ini :
Field Name | Data Type | Size |
Kode | Text | 5 |
Nama_Barang | Text | 50 |
Persediaan | Number | Long Integer |
Harga_Beli | Currency | |
Harga_Jual | Currency | |
Setelah membuat database kita mulai membuat form dengan komponen-komponen
Komponen | Caption | Name | Text |
Label1 | Kode Barang | | |
Label2 | Nama Barang | | |
Label3 | Persediaan | | |
Label4 | Harga Beli | | |
Label5 | Harga Jual | | |
TextBox1 | | TKode | Kosongkan |
TextBox2 | | TNama | Kosongkan |
TextBox3 | | TPersediaan | Kosongkan |
TextBox4 | | TBeli | Kosongkan |
TextBox5 | | TJual | Kosongkan |
CommandButton1 | &Tambah | TomTambah | |
CommandButton | &Edit | TomEdit | |
CommandButton | &Simpan | TomSimpan | |
CommandButton | &Hapus | TomBatal | |
CommandButton | &First | TomFirst | |
CommandButton | &Prev | TomPrev | |
CommandButton | &Next | TomNext | |
CommandButton | &Last | TomLast | |
CommandButton | Pencarian | Command1 | |
Adodc1 | | | |
DataGrid1 | | | |
Dan listing programnya dibawah ini :
Sub NavigasiOn()
Me.TomLast.Enabled = True
Me.TomNext.Enabled = True
Me.TomFirst.Enabled = True
Me.TomPrevious.Enabled = True
End Sub
Sub NavigasiOff()
Me.TomLast.Enabled = False
Me.TomNext.Enabled = False
Me.TomFirst.Enabled = False
Me.TomPrevious.Enabled = False
End Sub
Sub Kosong()
Me.TBeli = ""
Me.TJual = ""
Me.TKode = ""
Me.TPersediaan = ""
Me.TNama = ""
End Sub
Sub KotakKunci()
Me.TBeli.Locked = True
Me.TJual.Locked = True
Me.TKode.Locked = True
Me.TNama.Locked = True
Me.TPersediaan.Locked = True
End Sub
Sub KotakBuka()
Me.TBeli.Locked = False
Me.TJual.Locked = False
Me.TKode.Locked = False
Me.TNama.Locked = False
Me.TPersediaan.Locked = False
End Sub
Sub Isi()
On Error GoTo err
With Me.Adodc1.Recordset
Me.TBeli = !Harga_Beli
Me.TJual = !Harga_Jual
Me.TKode = !Kode
Me.TNama = !Nama_Barang
Me.TPersediaan = !Persediaan
End With
Exit Sub
err:
MsgBox "Error " & err.Number & " : " & err.Description
End Sub
Private Sub Command1_Click()
Dim cari As String
cari = InputBox("Masukkan kode yang akan dicari !", "Pencarian Barang")
With Me.Adodc1.Recordset
If Not .RecordCount = 0 Then
.MoveFirst
.Find "Kode ='" & cari & "'", , adSearchForward, 1
If Not .EOF Then
Isi
Else
MsgBox "Kode barang tidak ada", vbOKOnly, "Peringatan!"
.MoveFirst
Isi
End If
End If
End With
End Sub
Private Sub Form_Load()
With Me.Adodc1
.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\Data.mdb;Persist Security" 'u/ mengoneksikan ado ke database jadisatu, app.path itu u/ mencari database jadisatu, satu dengan program ini
.CommandType = adCmdTable ' masukannya berdasarkan tabel
.RecordSource = "Barang" 'mengoneksikan ke tabel siswa ortu
.Refresh 'meyegarkan ado biar data diatas bisa dijalankan
End With
With Me.Adodc1.Recordset
If Not .BOF Then
TomBatal_Click
Else
TomTambah_Click
End If
End With
KotakKunci
End Sub
Private Sub Form_Unload(Cancel As Integer)
Me.Adodc1.Recordset.CancelBatch
End Sub
Private Sub TBeli_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = vbKeyBack) Then
Beep
KeyAscii = 0
MsgBox "Yang dimasukkan harus angka !", vbInformation, "HarusAngka !"
End If
End Sub
Private Sub TJual_KeyPress(KeyAscii As Integer)
If Not (KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Or KeyAscii = vbKeyBack) Then
Beep
KeyAscii = 0
MsgBox "Yang dimasukkan harus angka !", vbInformation, "HarusAngka !"
End If
End Sub
Private Sub TKode_KeyPress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
Private Sub TomBatal_Click()
NavigasiOn
Me.TomBatal.Enabled = False
Me.TomEdit.Enabled = True
Me.TomSimpan.Enabled = False
Me.TomTambah.Enabled = True
Me.TomHapus.Enabled = True
With Me.Adodc1.Recordset
If Not .BOF Then ' bof (begin of file) jika tidak kosong maka program di bawah ini di jalankan
.CancelBatch 'membatalkan fungsi addnew
.MoveFirst
Isi 'untuk memanggil prosedur isi
End If
End With
End Sub
Private Sub TomEdit_Click()
NavigasiOn
Me.TomBatal.Enabled = True
Me.TomEdit.Enabled = False
Me.TomTambah.Enabled = False
Me.TomSimpan.Enabled = True
Me.TomHapus.Enabled = False
End Sub
Private Sub TomFirst_Click()
With Me.Adodc1.Recordset
If Not .BOF Then
.MoveFirst
siiipppppppppppp
BalasHapusthank's gan
BalasHapuscoding nya gk lengkap
BalasHapus