Mengolah Database Dengan SQL Dan Crystal Report Dalam Visual Basic 6.0 PDF
Mengolah Database Dengan SQL Dan Crystal Report Dalam Visual Basic 6.0 PDF
MERELASIKAN DATA
4.1
data barang dan data pemasok jika ternyata kode barang dan kode
pemasoknya belum terdaftar pada kedua tabel tersebut dan jumlah
barang langsung di-update, kemudian data pembelian ini akan
disimpan pada tabel beli.
Perhatikan dengan baik rancangan tabel beli yang pernah dibuat. Di
dalamnya terdapat field kode barang dan kode pemasok dengan
field-field tersebut merupakan field kunci pada tabel barang dan tabel
pemasok. Dengan teknik seperti ini maka tabel beli mampu
mengambil data yang diperlukan dari tabel barang dan tabel
pemasok tersebut. Untuk mencoba program pembelian, rancanglah
sebuah form dengan bentuk seperti gambar di bawah ini:
132
Nama
Obyek
Properti
Seting
Text1
Name
TxtNoFaktur
Text2
Name
TxtKodeBrg
Text3
Name
TxtHarga
Text4
Name
TxtKodePms
Text5
Name
TxtTelp
Text6
Name
TxtJumBeli
Text7
Name
TxtTglFaktur
Text8
Name
TxtNamaBrg
Text9
Name
TxtStokBrg
Text10
Name
TxtNamaPms
Text11
Name
TxtAlamat
Text12
Name
TxtRelasi
Text13
Name
TxtTotal
Text14
Name
JmlData
Command1
Name
Cmdinput
Caption
&Input
Name
Cmdtutup
Caption
&Tutup
Name
Data1
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Brg
RecordSource
Barang
RecordsetType
0 Table
Name
Data2
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Pms
RecordSource
Pemasok
RecordsetType
0 Table
Name
Data3
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Beli
RecordSource
Beli
RecordsetType
0 Table
Command2
Data1
Data2
Data3
133
Listing Program:
Fungsi untuk mencari jumlah data
Function JumlahData()
mjumlah = Data3.Recordset.RecordCount
JmlData = mjumlah
End Function
Private Sub Form_activate()
txtnofaktur.MaxLength = 6
txtkodebrg.MaxLength = 6
txtharga.MaxLength = 8
txtkodepms.MaxLength = 6
txttelpon.MaxLength = 8
txtjumbeli.MaxLength = 4
txttglfaktur.MaxLength = 8
txtnamabrg.MaxLength = 30
txtstokbrg.MaxLength = 4
txtnamapms.MaxLength = 30
txtalamat.MaxLength = 30
txtrelasi.MaxLength = 15
txttotal.MaxLength = 8
tidaksiapisi
cmdinput.SetFocus
JumlahData
End Sub
Private Sub cmdinput_click()
If cmdinput.Caption = "&Input" Then
cmdinput.Caption = "&Simpan"
cmdtutup.Caption = "&Batal"
siapisi
txtnofaktur.SetFocus
Else
If txtnofaktur = Empty Then
pesan = MsgBox("Nomor Faktur Tidak Boleh Kosong", & _
0, "Informasi")
txtnofaktur.SetFocus
Else
With Data3.Recordset
.AddNew
!NoFaktur = txtnofaktur
!TglFaktur = txttglfaktur
!kodebrg = txtkodebrg
!kodepms = txtkodepms
!jmlbeli = txtjumbeli
.Update
End With
With Data1.Recordset
.Index = "barangdex"
.Seek "=", txtkodebrg
If .NoMatch Then
134
.AddNew
!kodebrg = txtkodebrg
!namabrg = txtnamabrg
!harga = txtharga
!Jumlah = txtjumbeli
.Update
Else
.Edit
!Jumlah = !Jumlah + txtjumbeli
.Update
End If
End With
With Data2.Recordset
.Index = "Pemasokdex"
.Seek "=", txtkodepms
If .NoMatch Then
.AddNew
!kodepms = txtkodepms
!namapms = txtnamapms
!AlamatPms = txtalamat
!TelponPms = txttelpon
!RelasiPms = txtrelasi
.Update
End If
End With
Data1.Refresh
Data2.Refresh
Data3.Refresh
kosongkan
tidaksiapisi
semula
End If
End If
JumlahData
End Sub
Private Sub txtnofaktur_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txttglfaktur = Date
txtkodebrg.SetFocus
End If
End Sub
Private Sub txttglfaktur_keypress(KeyAscii As Integer)
If KeyAscii = 13 Then
txtkodebrg.SetFocus
End If
End Sub
Private Sub txtkodebrg_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
135
If KeyAscii = 13 Then
With Data1.Recordset
.Index = "barangdex"
.Seek "=", txtkodebrg
If Not .NoMatch Then
txtnamabrg.Enabled = False
txtharga.Enabled = False
txtstokbrg.Enabled = False
txtnamabrg = Data1.Recordset!namabrg
txtharga = Data1.Recordset!harga
txtstokbrg = Data1.Recordset!Jumlah
txtkodepms.SetFocus
Else
x = MsgBox("Kode Barang Tidak Ada, Data Barang & _
Langsung Dientri...!", vbYesNo, "Konfirmasi")
If x = vbYes Then
txtnamabrg.Enabled = True
txtharga.Enabled = True
txtstokbrg.Enabled = True
txtnamabrg = ""
txtharga = ""
txtstokbrg = ""
txtnamabrg.SetFocus
Else
txtkodebrg.SetFocus
End If
End If
End With
End If
End Sub
Private Sub txtkodepms_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
With Data2.Recordset
.Index = "pemasokdex"
.Seek "=", txtkodepms
If Not .NoMatch Then
txtnamapms.Enabled = False
txttelpon.Enabled = False
txtalamat.Enabled = False
txtrelasi.Enabled = False
txtnamapms = Data2.Recordset!namapms
txttelpon = Data2.Recordset!TelponPms
txtalamat = Data2.Recordset!AlamatPms
txtrelasi = Data2.Recordset!RelasiPms
txtjumbeli.Enabled = True
txtjumbeli.SetFocus
Else
x = MsgBox("Kode Pemasok Tidak Ada, Apakah & _
akan Langsung Diinput...?", vbYesNo, "Konfirmasi")
If x = vbYes Then
siapisi
136
txtnamapms.SetFocus
Else
txtkodepms.SetFocus
End If
End If
End With
End If
End Sub
Private Sub txtnamabrg_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtharga.SetFocus
End If
End Sub
Private Sub txtharga_keypress(KeyAscii As Integer)
If KeyAscii = 13 Then
With Data1.Recordset
.Index = "barangdex"
.Seek "=", txtkodebrg
If .NoMatch Then
txtkodepms.SetFocus
End If
End With
End If
End Sub
Private Sub txtnamapms_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txttelpon.SetFocus
End If
End Sub
Private Sub txttelpon_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtalamat.SetFocus
End If
End Sub
Private Sub txtalamat_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtrelasi.SetFocus
End If
End Sub
Private Sub txtrelasi_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtjumbeli.SetFocus
137
End If
End Sub
Private Sub cmdtutup_Click()
Select Case cmdtutup.Caption
Case "&Tutup"
End
Case "&Batal"
kosongkan
tidaksiapisi
semula
End Select
End Sub
Private Sub kosongkan()
Dim Ctl As Control
For Each Ctl In Me
If TypeName(Ctl) = "TextBox" Then
Ctl.Text = ""
End If
Next
End Sub
Private Sub tampilkan()
tidaksiapisi
With Data3.Recordset
txtnofaktur = !NoFaktur
txtkodebrg = !kodebrg
txtharga = !harga
txttelpon = !TelponPms
txtrealsi = !RelasiPms
End With
End Sub
Private Sub siapisi()
Dim x As Control
For Each x In Me
If TypeName(x) = "TextBox" Then
x.Enabled = True
End If
Next
End Sub
Private Sub tidaksiapisi()
Dim x As Control
For Each x In Me
If TypeName(x) = "TextBox" Then
x.Enabled = True
End If
Next
End Sub
Private Sub semula()
138
cmdinput.Caption
cmdtutup.Caption
cmdinput.Enabled
cmdtutup.Enabled
End Sub
=
=
=
=
"&Input"
"&Tutup"
True
True
Penjelasan Program:
With Data1.Recordset
.Index = "barangdex"
.Seek "=", txtkodebrg
If .NoMatch Then
.AddNew
!kodebrg = txtkodebrg
!namabrg = txtnamabrg
!harga = txtharga
!Jumlah = txtjumbeli
.Update
Else
.Edit
!Jumlah = !Jumlah + txtjumbeli
.Update
End If
End With
Potongan program ini berarti pada saat cmdinput diklik dan data
barang tidak ditemukan maka entrilah data baru tersebut, tapi jika
data barang ditemukan maka editlah jumlahnya dengan cara
menambahkan jumlah pembelian ke tabel barang (field jumlah).
Private Sub kosongkan()
Dim Ctl As Control
For Each Ctl In Me
If TypeName(Ctl) = "TextBox" Then
Ctl.Text = ""
End If
Next
End Sub
4.2
140
Nama
Obyek
Properti
Seting
DBCombo1
Name
DBCombo1
Rowsource
Data1
Listfield
Kodebrg
Name
DBCombo2
Rowsource
Data2
Listfield
Kodeplg
Name
Data1
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Brg
RecordSource
Barang
DBCombo2
Data1
141
Data2
Data3
RecordsetType
0 Table
Name
Data2
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Plg
RecordSource
Pelanggan
RecordsetType
0 Table
Name
Data3
DatabaseName
C:\Belajar VB\Master.mdb
Caption
Jual
RecordSource
Jual
RecordsetType
0 Table
Listing Program:
Function JumlahData()
mjumlah = Data3.Recordset.RecordCount
JmldataJual = mjumlah
End Function
Private Sub Form_activate()
txtnofaktur.MaxLength = 6
txtharga.MaxLength = 8
txttelpon.MaxLength = 8
txtjumjual.MaxLength = 4
txttglfaktur.MaxLength = 8
txtnamabrg.MaxLength = 30
txtstokbrg.MaxLength = 4
txtnamaplg.MaxLength = 30
txtalamat.MaxLength = 30
txtrelasi.MaxLength = 15
txttotal.MaxLength = 8
tidaksiapisi
cmdinput.SetFocus
JumlahData
End Sub
Private Sub cmdinput_click()
JumlahData
If cmdinput.Caption = "&Input" Then
cmdinput.Caption = "&Simpan"
cmdtutup.Caption = "&Batal"
siapisi
142
txtnofaktur.SetFocus
Else
If txtnofaktur = Empty Then
pesan = MsgBox("Nomor Faktur Tidak Boleh Kosong", & _
0, "Informasi")
txtnofaktur.SetFocus
Else
With Data3.Recordset
.AddNew
!Nofaktur = txtnofaktur
!tglfaktur = txttglfaktur
!kodebrg = DBCombo1
!KodePlg = DBCombo2
!JmlJual = txtjumjual
.Update
Data3.Refresh
End With
With Data1.Recordset
.Index = "barangdex"
.Seek "=", DBCombo1
If .NoMatch Then
.AddNew
!kodebrg = DBCombo1
!Namabrg = txtnamabrg
!harga = txtharga
!Jumlah = txtstokbrg
.Update
Else
.Edit
!Jumlah = !Jumlah - txtjumjual
.Update
End If
End With
With Data2.Recordset
.Index = "Pelanggandex"
.Seek "=", DBCombo2
If .NoMatch Then
.AddNew
!KodePlg = DBCombo2
!Namaplg = txtnamaplg
!AlamatPlg = txtalamat
!TelponPlg = txttelpon
!RelasiPlg = txtrelasi
.Update
End If
End With
Data1.Refresh
Data2.Refresh
Data3.Refresh
kosongkan
tidaksiapisi
semula
End If
143
End If
JumlahData
End Sub
Private Sub txtnofaktur_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txttglfaktur = Format(Date, "DD/MM/YY")
DBCombo1.SetFocus
End If
End Sub
Private Sub txttglfaktur_keypress(KeyAscii As Integer)
If KeyAscii = 13 Then
DBCombo1.SetFocus
End If
End Sub
Private Sub DBCombo1_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
Private Sub dbcombo1_change()
If Len(DBCombo1.Text) < 6 Then
Exit Sub
End If
With Data1.Recordset
.Index = "barangdex"
.Seek "=", DBCombo1
If Not .NoMatch Then
txtnamabrg.Enabled = False
txtharga.Enabled = False
txtstokbrg.Enabled = False
txtnamabrg = Data1.Recordset!Namabrg
txtharga = (Val(Data1.Recordset!harga) + & _
(Val(Data1.Recordset!harga)) * 0.1)
txtstokbrg = Data1.Recordset!Jumlah
DBCombo2.SetFocus
Else
x = MsgBox("Kode Barang Tidak Ada, Data Barang & _
Langsung dientri...!", vbYesNo, "Konfirmasi")
If x = vbYes Then
txtnamabrg.SetFocus
Else
DBCombo1.SetFocus
End If
End If
End With
End Sub
Private Sub DBCombo2_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
End Sub
144
145
If KeyAscii = 13 Then
txttelpon.SetFocus
End If
End Sub
Private Sub txtstokbrg_keypress(KeyAscii As Integer)
If KeyAscii = 13 Then
DBCombo2.SetFocus
End If
End Sub
Private Sub txttelpon_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtalamat.SetFocus
End If
End Sub
Private Sub txtalamat_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtrelasi.SetFocus
End If
End Sub
Private Sub txtrelasi_keypress(KeyAscii As Integer)
KeyAscii = Asc(UCase(Chr(KeyAscii)))
If KeyAscii = 13 Then
txtjumjual.SetFocus
End If
End Sub
Private Sub cmdtutup_Click()
Select Case cmdtutup.Caption
Case "&Tutup"
End
Case "&Batal"
kosongkan
tidaksiapisi
semula
End Select
End Sub
Private Sub kosongkan()
Dim Ctl As Control
For Each Ctl In Me
If TypeName(Ctl) = "TextBox" Then
Ctl.Text = ""
End If
If TypeName(Ctl) = "DBCombo" Then
Ctl.Text = ""
End If
Next
146
End Sub
Private Sub tampilkan()
tidaksiapisi
With Data3.Recordset
txtnofaktur = !Nofaktur
DBCombo1 = !kodebrg
txtharga = !harga
txttelpon = !TelponPlg
txtrealsi = !RelasiPlg
End With
End Sub
Private Sub siapisi()
Dim x As Control
For Each x In Me
If TypeName(x) = "TextBox" Then
x.Enabled = True
End If
If TypeName(x) = "DBCombo" Then
x.Enabled = True
End If
Next
End Sub
Private Sub tidaksiapisi()
Dim x As Control
For Each x In Me
If TypeName(x) = "TextBox" Then
x.Enabled = False
End If
If TypeName(x) = "DBCombo" Then
x.Enabled = False
End If
Next
End Sub
Private Sub semula()
cmdinput.Caption = "&Input"
cmdtutup.Caption = "&Tutup"
cmdinput.Enabled = True
cmdtutup.Enabled = True
End Sub
Private Sub txtjumJual_Change()
If Val(txtjumjual) > Val(txtstokbrg) Then
x = MsgBox("Persediaan Barang Kurang, Lihat Stok & _
Barang...!", 0, "Informasi")
txtjumjual.SetFocus
Else
txttotal.Enabled = False
txttotal = Val(txtjumjual) * Val(txtharga)
End If
147
End Sub
Private Sub txtjumJual_keypress(KeyAscii As Integer)
If KeyAscii = 13 Then
cmdinput.SetFocus
End If
End Sub
4.3
148
Nama Obyek
Properti
Seting
Command1
Name
Command1
Caption
&Cetak Ke Layar
Data1
Data2
Data3
Name
Data1
Caption
Brg
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
0 Table
RecordSource
Barang
Name
Data2
Caption
Pms
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
0 Table
RecordSource
Pemasok
Name
Data3
Caption
Beli
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
1 Dynaset
RecordSource
Beli
Listing Program:
Private Sub Command1_Click()
Tampil.Show
definisikan variabel hitungan
Dim MSubtotal, MTotal As Long
Dim MNo, MHal, MBaris As Integer
Dim MJumlah As Double
definisikan garis
Dim MGrs As String
Tampil.Show
Pilih bentuk huruf
Tampil.Font = "Courier New"
With Data3.Recordset
Jika data pembelian kosong, munculkan pesan
If Data3.Recordset.RecordCount = 0 Then
x = MsgBox("Data Pembelian Belum Ada", 0, "Informasi")
Exit Sub
End If
bawa ke record pertama
.MoveFirst
mtotalharga = 0
149
mtotalbeli = 0
mtotalbayar = 0
MNo = 0
MHal = 0
Cetak Judul laporan
Do While Not .EOF
MHal = MHal + 1
Tampil.Print
Tampil.Print
Tampil.FontBold = True
Tampil.FontSize = 10
Tampil.Print Tab(10); "Data Pembelian PT. ABC"
Tampil.Print Tab(10); "Tanggal Cetak: "; Format(Date,& _
"DD-MMM-YY")
Tampil.FontBold = False
Tampil.FontSize = 8
Tampil.Print
Tampil.Print Tab(90); "Hal:"; Format(MHal, "###")
MGrs = String$(110, "-")
Tampil.Print MGrs
Tampil.Print Tab(3); "No.";
Tampil.Print Tab(8); "No Fkt";
Tampil.Print Tab(18); "TGL Fkt";
Tampil.Print Tab(30); "Nama Barang";
Tampil.Print Tab(57); "Nama Pemasok";
Tampil.Print Tab(82); "Harga";
Tampil.Print Tab(92); "Jumlah";
Tampil.Print Tab(105); "Total"
Tampil.Print MGrs
msubtotalbeli = 0
msubtotalbayar = 0
MBaris = 0
cetak isi tabel
Do While Not .EOF And MBaris <= 30
MNo = MNo + 1
Data1.Recordset.Index = "Barangdex"
Data1.Recordset.Seek "=", Data3.Recordset!kodebrg
Data2.Recordset.Index = "Pemasokdex"
Data2.Recordset.Seek "=", Data3.Recordset!kodepms
mharga = Data1.Recordset!harga
mbeli = Data3.Recordset!jmlbeli
Mbayar = mharga * mbeli
Tampil.Print Tab(2); RKanan(MNo, "###");
Tampil.Print Tab(8); Data3.Recordset!NoFaktur;
Tampil.Print Tab(18);Format(Data3. & _
Recordset!TglFaktur, "DD-MMM-YY");
Tampil.Print Tab(30); Data1.Recordset!namabrg;
Tampil.Print Tab(57); Data2.Recordset!namapms;
Tampil.Print Tab(78); RKanan(mharga, "###,###,###");
Tampil.Print Tab(92); RKanan(mbeli, "#,###");
Tampil.Print Tab(100); RKanan(Mbayar, "###,###,###")
msubtotalbeli = msubtotalbeli + mbeli
msubtotalbayar = msubtotalbayar + Mbayar
150
MBaris = MBaris + 1
.MoveNext
Loop
mtotalbeli = mtotalbeli + msubtotalbeli
mtotalbayar = mtotalbayar + msubtotalbayar
Tampil.Print MGrs
Tampil.Print Tab(5); "Sub Total:";
Tampil.Print Tab(92); RKanan(msubtotalbeli, "#,###");
Tampil.Print Tab(100); RKanan(msubtotalbayar, & _
"###,###,###") & ",-"
Tampil.Print Tab(5); "Total:";
Tampil.Print Tab(92); RKanan(mtotalbeli, "#,###");
Tampil.Print Tab(100); RKanan(mtotalbayar, "###,###,###")
& ",-"
Tampil.Print MGrs
Loop
End With
End Sub
Fungsi Meratakan Angka (Numeric)
Private Function RKanan(NData, CFormat) As String
RKanan = Format(NData, CFormat)
RKanan = Space(Len(CFormat) - Len(RKanan)) + RKanan
End Function
Hasil:
4.4
151
keperluan
Nama Obyek
Properti
Seting
Command1
Name
Command1
Caption
&Cetak Ke Layar
Name
Data1
Caption
Brg
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
0 Table
RecordSource
Barang
Name
Data2
Caption
Plg
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
0 Table
RecordSource
Pelanggan
Name
Data3
Caption
Jual
DatabaseName
C:\Belajar VB\Master.mdb
RecordsetType
1 Dynaset
RecordSource
Jual
Data1
Data2
Data3
152
Hasil:
Listing Program:
Private Sub Command1_Click()
Tampil.Show
Dim MSubtotal, MTotal As Long
Dim MNo, MHal, MBaris As Integer
Dim MJumlah As Double
Dim MGrs As String
Tampil.Show
Tampil.Font = "Courier New"
With Data3.Recordset
If Data3.Recordset.RecordCount = 0 Then
x = MsgBox("Data Penjualan Belum Ada", 0, "Informasi")
Exit Sub
End If
.MoveFirst
mtotalharga = 0
mtotalJual = 0
mtotalbayar = 0
MNo = 0
MHal = 0
Do While Not .EOF
MHal = MHal + 1
Tampil.Print
Tampil.Print
Tampil.FontBold = True
Tampil.FontSize = 10
Tampil.Print Tab(10); "Data Penjualan PT. ABC"
Tampil.Print Tab(10); "Tanggal Cetak: "; & _
Format(Date, "DD-MMM-YY")
Tampil.FontBold = False
Tampil.FontSize = 8
Tampil.Print
Tampil.Print Tab(90); "Hal:"; Format(MHal, "###")
153
154
155