Minggu, 04 November 2012

Dasar Contoh Syntax Visual Basic

Dasar Visual Basic


1. sytax Module untuk mengubungkan Visual basic dengan data Base

Public Kns As New ADODB.Connection
Public Rs As New ADODB.Recordset

Pada baris selanjutnya >

Public Sub BukaDb()
Set Kns = New ADODB.Connection
Set Rs = New ADODB.Recordset
Kns.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & App.Path & "\dbstock.Accdb"
End Sub

2. Syntax beberapa komponen Pendukung pada From

a. Private Aktivate

Private Sub Form_Activate()
Call Kosong
Call BukaDb
Adodc1.ConnectionString = Kns
Adodc1.RecordSource = "[Tabel Barang]"
Adodc1.Refresh

Set DataGrid1.DataSource = Adodc1
DataGrid1.Refresh
Call Kosong
End Sub

b. Private Kosong

Sub Kosong()
Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""

End Sub

c. Private Sub Key ASCII

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Text2.SetFocus
End Sub

Private Sub Text2_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Text3.SetFocus
End Sub



Private Sub Text3_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then Text4.SetFocus
End Sub



Private Sub Text4_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
    Text5 = Text3 * Text4
    CmdSimpan.SetFocus
End If
End Sub

d. Private Function

Function Cari()
Call BukaDb
Rs.Open "SELECT * from [Tabel Barang] where [Kode Barang]= '" & Text1 & "'", Kns
End Function

e. Private Cari

Private Sub Cmdcari_Click()
Call Cari
If Not Rs.EOF Then
Text1 = Rs![Kode Barang]
Text2 = Rs![Nama Barang]
Text3 = Rs![Harga Satuan]
Text4 = Rs![Jumlah Stock]
Text5 = Rs![Jumlah Harga]
Else
MsgBox ("data tersebut tidak ditemukan..")
End If

End Sub

f. Private Simpan

Private Sub CmdSimpan_Click()
Dim Sn As String
Sn = "insert into [Tabel Barang] Values ('" & Text1 & "','" & Text2 & "','" & Text3 & "','" & Text4 & "','" & Text5 & "')"
Kns.Execute Sn
Call Form_Activate
End Sub

g. Private Hapus

Private Sub CmdHapus_Click()
psn = MsgBox("apakah benar data ini akan dihapus...?.", vbYesNo + vbQuestion, "Perhatian")
If psn = vbYes Then
        Dim H As String
        H = "Delete from [Tabel Barang] where [Kode Barang]='" & Text1 & "'"
        Kns.Execute H
        Call Form_Activate    
Else
    MsgBox ("Data Tidak Ditemukan..")
    Exit Sub
End If
End Sub

h. Private Update

Private Sub CmdUpdate_Click()
Dim ud As String
ud = "update [Tabel Barang] set [Nama Barang] ='" & Text2 & "',[Harga Satuan] ='" & Text3 & "', [Jumlah Stock] ='" & Text4 & "', [Jumlah Harga] ='" & Text5 & "' where [Kode Barang] ='" & Text1 & "'"
Kns.Execute ud
Call Form_Activate

End Sub

i. Private Close

Private Sub CmdTutup_Click()
End

End Sub

0 komentar:

Posting Komentar

Jika anda merasa tulisan maupun artikel ini sangat berguna bagi anda Please folow blog ini dan tinggalkan jejak anda di sini dengan berkomentar. Terimakasih telah berkunjung.

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Best Buy Coupons