belajar membuat aplikasi dari visual basic dengan ms acces
TRANSCRIPT
Tutorial Vb & Acces dasar ## Cek Ly
Pemrograman Visual Basic
dengan Ms.Acces
Buatlah Database dari Ms. Acces dengan Nama “DBDasar1”, Buatlah Table sesuai kolom dibawah ini :
No Field Name Data Type Field size 1 KodeBrg Txt 6 2 NamaBrg Txt 30 3 HargaBeli Number Long Integer 4 HargaJual Number Long Integer 5 JumlahBrg Number Integer
Simpan Table Dengan Nama Barang.
Selanjutnya buka Visual Basic buatlah Form Seperti Gambar Dibawah ini :
Setting Pengaturanya :
Tutorial
Dasar
Modul dapat digunakan, dimodifikasi dan disebarkan secara bebas untuk tujuan bukan komersial (nonprofit),
dengan syarat tidak menghapus atau merubah atribut penulis dan pernyataan copyright yang disertakan dalam
setiap dokumen. Tidak diperbolehkan melakukan penulisan ulang, kecuali mendapatkan ijin terlebih dahulu dari
Penulis
Tutorial Vb & Acces dasar ## Cek Ly
No Objek Properti Nilai 1 Label1 Caption
Border Style Kode 1 fixed-single
2 Label2 Caption Border Style
Nama 1 fixed-single
3 Label3` Caption Border Style
Harga Beli 1 fixed-single
4 Label4 Caption Border Style
Harga Jual 1 fixed-single
5 Label5 Caption Border Style
Jumlah 1 fixed-single
6 Combo1 - - 7 Command1 Name
Caption Cmdinput &Input
8 Command2 Name Caption
Cmdedit &Edit
9 Command3 Name Caption
Cmdhapus &Hapus
10 Command4 Name Caption
Cmdtutup &Tutup
Selanjutnya buatlah Module dan Ketikkan sintaks Berikut :
Selanjutnya Ketikkan code berikut :
Private Sub form_activate()
Call BukaDB
RSBarang.Open "select * from barang", Conn
Public Conn As New ADODB.Connection
Public RSBarang As ADODB.Recordset
Public Sub BukaDB()
Set Conn = New ADODB.Connection
Set RSBarang = New ADODB.Recordset
Conn.Open "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\DBDasar1.mdb"
End Sub
Tutorial Vb & Acces dasar ## Cek Ly
Combo1.Clear
Do While Not RSBarang.EOF
Combo1.AddItem RSBarang!Kodebrg & Space(5) & RSBarang!namabrg
RSBarang.MoveNext
Loop
End Sub
Sub Form_Load()
Text1.MaxLength = 6
Text2.MaxLength = 30
Text3.MaxLength = 8
Text4.MaxLength = 8
Text5.MaxLength = 4
Kondisiawal
End Sub
Private Sub AutoNomor()
Call BukaDB
RSBarang.Open ("select * from Barang Where KodeBrg In(Select Max(KodeBrg)From Barang)Order By KodeBrg Desc"), Conn
RSBarang.Requery
Dim Urutan As String * 6
Dim Hitung As Long
With RSBarang
If .EOF Then
Urutan = "BRG" + "001"
Text1 = Urutan
Else
Hitung = Right(!Kodebrg, 3) + 1
Tutorial Vb & Acces dasar ## Cek Ly
Urutan = "BRG" + Right("000" & Hitung, 3)
End If
Text1 = Urutan
End With
End Sub
Private Sub KosongkanText()
Combo1 = ""
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
End Sub
Private Sub SiapIsi()
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
End Sub
Private Sub TidakSiapIsi()
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Enabled = False
Tutorial Vb & Acces dasar ## Cek Ly
End Sub
Private Sub Kondisiawal()
KosongkanText
TidakSiapIsi
Cmdinput.Caption = "&Input"
Cmdedit.Caption = "&Edit"
Cmdhapus.Caption = "&Hapus"
Cmdtutup.Caption = "&Tutup"
Cmdinput.Enabled = True
Cmdedit.Enabled = True
Cmdhapus.Enabled = True
End Sub
Private Sub TampilkanData()
With RSBarang
If Not RSBarang.EOF Then
Text2 = RSBarang!namabrg
Text3 = RSBarang!HargaBeli
Text4 = RSBarang!HargaJual
Text5 = RSBarang!JumlahBrg
End If
End With
End Sub
Private Sub CmdInput_click()
If Cmdinput.Caption = "&Input" Then
Cmdinput.Caption = "&Simpan"
Cmdedit.Enabled = False
Tutorial Vb & Acces dasar ## Cek Ly
Cmdhapus.Enabled = False
Cmdtutup.Caption = "&Batal"
SiapIsi
KosongkanText
Call AutoNomor
Text1.Enabled = False
Text2.SetFocus
Else
If Text1 = "" Or Text2 = "" Or Text3 = "" Or Text4 = "" Or Text5 = "" Then
MsgBox "Data Belum Lengkap...!"
Else
Dim SQLTambah As String
SQLTambah = "Insert Into Barang (KodeBrg,NamaBrg,HargaBeli,HargaJual,JumlahBrg) values ('" & Text1 & "','" & Text2 & "','" & Text3 & "','" & Text4 & "','" & Text5 & "')"
Conn.Execute SQLTambah
Call form_activate
Call Kondisiawal
End If
End If
End Sub
Private Sub CmdEdit_Click()
If Cmdedit.Caption = "&Edit" Then
Cmdinput.Enabled = False
Cmdedit.Caption = "&Simpan"
Cmdhapus.Enabled = False
Cmdtutup.Caption = "&Batal"
SiapIsi
Text1.Enabled = False
Tutorial Vb & Acces dasar ## Cek Ly
Combo1.SetFocus
Else
If Text2 = "" Or Text3 = "" Or Text4 = "" Or Text5 = "" Then
MsgBox "Masih Ada Data Yang Kosong"
Else
Dim SQLEdit As String
SQLEdit = "Update Barang Set NamaBrg= '" & Text2 & "', HargaBeli='" & Text3 & "', HargaJual='" & Text4 & "',JumlahBrg='" & Text5 & "' where KodeBrg='" & Text1 & "'"
Conn.Execute SQLEdit
Call form_activate
Call Kondisiawal
End If
End If
End Sub
Private Sub CmdHapus_Click()
If Cmdhapus.Caption = "&Hapus" Then
Cmdinput.Enabled = False
Cmdedit.Enabled = False
Cmdtutup.Caption = "&Batal"
KosongkanText
SiapIsi
Text1.Enabled = False
Combo1.SetFocus
End If
End Sub
Private Sub CmdTutup_Click()
Select Case Cmdtutup.Caption
Tutorial Vb & Acces dasar ## Cek Ly
Case "&Tutup"
Unload Me
Case "&Batal"
TidakSiapIsi
Kondisiawal
End Select
End Sub
Function CariData()
Call BukaDB
RSBarang.Open "Select * From Barang where KodeBrg='" & Text1 & "'", Conn
End Function
Private Sub Text1_Keypress(Keyascii As Integer)
Keyascii = Asc(UCase(Chr(Keyascii)))
If Keyascii = 13 Then
If Len(Text1) < 6 Then
MsgBox "Kode Harus 6 Digit"
Text1.SetFocus
Exit Sub
Else
Text2.SetFocus
End If
If Cmdinput.Caption = "&Simpan" Then
Call CariData
If Not RSBarang.EOF Then
TampilkanData
MsgBox "Kode Barang Sudah Ada"
Tutorial Vb & Acces dasar ## Cek Ly
KosongkanText
Text1.SetFocus
Else
Text2.SetFocus
End If
End If
If Cmdedit.Caption = "&Simpan" Then
Call CariData
If Not RSBarang.EOF Then
TampilkanData
Text1.Enabled = False
Text2.SetFocus
Else
MsgBox "Kode Barang Tidak Ada"
Text1 = ""
Text1.SetFocus
End If
End If
If Cmdhapus.Enabled = True Then
Call CariData
If Not RSBarang.EOF Then
TampilkanData
pesan = MsgBox("Yakin akan dihapus", vbYesNo)
If pesan = vbYes Then
Dim SQLHapus As String
SQLHapus = "Delete From Barang where kodebrg= '" & Text1 & "'"
Conn.Execute SQLHapus
Tutorial Vb & Acces dasar ## Cek Ly
Call Kondisiawal
Call form_activate
Else
Kondisiawal
Cmdhapus.SetFocus
End If
Else
MsgBox "Data Tidak ditemukan"
Text1.SetFocus
End If
End If
End If
End Sub
Private Sub Text2_Keypress(Keyascii As Integer)
Keyascii = Asc(UCase(Chr(Keyascii)))
If Keyascii = 13 Then Text3.SetFocus
End Sub
Private Sub Text3_Keypress(Keyascii As Integer)
If Keyascii = 13 Then Text4.SetFocus
If Not (Keyascii >= Asc("0") And Keyascii <= Asc("9") Or Keyascii = vbKeyBack) Then Keyascii = 0
End Sub
Private Sub Text4_Keypress(Keyascii As Integer)
If Keyascii = 13 Then
If Val(Text4) <= Val(Text3) Then
MsgBox "harga jual jangan lebih kecil dari harga beli"
Text4 = ""
Tutorial Vb & Acces dasar ## Cek Ly
Text4.SetFocus
Exit Sub
Else
Text5.SetFocus
End If
End If
If Not (Keyascii >= Asc("0") And Keyascii <= Asc("9") Or Keyascii = vbKeyBack) Then Keyascii = 0
End Sub
Private Sub Text5_Keypress(Keyascii As Integer)
If Keyascii = 13 Then
If Cmdinput.Enabled = True Then
Cmdinput.SetFocus
ElseIf Cmdedit.Enabled = True Then
Cmdedit.SetFocus
End If
End If
If Not (Keyascii >= Asc("0") And Keyascii <= Asc("9") Or Keyascii = vbKeyBack) Then Keyascii = 0
End Sub
Private Sub Combo1_Click()
Call BukaDB
RSBarang.Open "select * from Barang where Kodebrg='" & Left(Combo1, 6) & "'", Conn
If Not RSBarang.EOF Then
With RSBarang
If Not RSBarang.EOF Then
Text1 = RSBarang!Kodebrg
Text2 = RSBarang!namabrg
Text3 = RSBarang!HargaBeli
Tutorial Vb & Acces dasar ## Cek Ly
Text4 = RSBarang!HargaJual
Text5 = RSBarang!JumlahBrg
End If
End With
End If
End Sub
Private Sub Combo1_KeyPress(Keyascii As Integer)
If Keyascii = 13 Then
If Cmdedit.Caption = "&Simpan" Then
Call CariData
If Not RSBarang.EOF Then
TampilkanData
Text1.Enabled = False
Text2.SetFocus
Else
MsgBox "Kode Barang Barang Tidak Ada"
Text1.SetFocus
Exit Sub
End If
ElseIf Cmdhapus.Caption = "&Hapus" Then
Call CariData
If Not RSBarang.EOF Then
TampilkanData
pesan = MsgBox("Yakin akan dihapus", vbYesNo)
If pesan = vbYes Then
Dim hapus As String
hapus = "Delete From Barang where Kodebrg= '" & Text1 & "'"
Tutorial Vb & Acces dasar ## Cek Ly
Conn.Execute hapus
Kondisiawal
form_activate
Else
Kondisiawal
End If
Else
MsgBox "Data Tidak ditemukan"
Text1.SetFocus
End If
End If
End If
End Sub
****** Selamat Bekerja ******