Sabtu, 14 Mei 2011

Contoh source program vb.net




Public Class Form1

Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call adit()
Call abe()

End Sub
Sub adit()
Dim I As Integer
Dim B As Integer
ListBox1.Items.Clear()
For I = Val(TextBox1.Text) To Val(TextBox2.Text)
If I Mod 5 = 0 Then
ListBox1.Items.Add("&" & I)
B = B + I

End If
Next
TextBox3.Text = B
End Sub

Sub abe()
Dim I As Integer
Dim A As Integer
ListBox2.Items.Clear()
For I = Val(TextBox1.Text) To Val(TextBox2.Text)
If I Mod 1 = 0 And I Mod 5 > 0 Then
ListBox2.Items.Add(I & "%")
A = A + I
End If

Next
TextBox4.Text = A
End Sub




Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

Contoh koneksi untuk ke Database 3


Public Class Form2


Sub Kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox1.Focus()
End Sub
Sub netral()
Bttambah.Text = "&Tambah"
BtUbah.Text = "&Ubah"
Bthapus.Text = "&Hapus"
btkeluar.Text = "&Keluar"
Bttambah.Enabled = True
BtUbah.Enabled = True
Bthapus.Enabled = True
TextBox1.Enabled = True
End Sub

Sub cekrecord()
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from Jurusan where KdJur='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
hasilcek = True
Else
hasilcek = False
End If
End Sub
Private Sub BtnTambah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bttambah.Click
Call simpankelompok()
'memenggil metod (simpankelompok)
End Sub
Sub simpankelompok()
If Bttambah.Text = "&Tambah" Then
TextBox1.Text = ""
Bttambah.Text = "Simpan"
BtUbah.Enabled = False
Bthapus.Enabled = False
btkeluar.Text = "&Batal"
'TextBox1.Enabled = False
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Call cekrecord()
Try
If hasilcek = "False" Then 'jika data true langsung ke else
Call mykoneksi()

Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Insert Into Jurusan (KdJur,NmJur)values( '" & TextBox1.Text & "','" & TextBox2.Text & "')"
Tampil.ExecuteNonQuery()

Call Kosong()

Else
MsgBox("Data sudah ada boss, silahkan cek data yang di input", MsgBoxStyle.Critical, "Input data")
TextBox1.Focus()
End If

Catch ex As Exception
MsgBox(ex.ToString())

End Try

End If
End Sub
Private Sub BtnKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btkeluar.Click
If btkeluar.Text = "&Keluar" Then
Me.Close()
Else
Call Kosong()
Call netral()
End If
End Sub
Private Sub Btnubah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUbah.Click
Call Btnubahkelompok()
End Sub
Sub Btnubahkelompok()
If BtUbah.Text = "&Ubah" Then
TextBox1.Text = ""
BtUbah.Text = "Simpan"
Bttambah.Enabled = False 'enabled= aktif/tidak aktif
Bthapus.Enabled = False
btkeluar.Text = "&Batal"
TextBox1.Focus()

Else
If TextBox1.Text = "" Then Exit Sub 'untuk mengeck data ada/tidak lalu keluar, dan tidak akan dijalankan
If TextBox2.Text = "" Then Exit Sub
Try
Call mykoneksi() 'untuk mengecek data kdjur ada apa tidak

Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "Update Jurusan Set NmJur ='" & Trim(TextBox2.Text) & "' where KdJur ='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()


Call Kosong()
TextBox1.Focus()

Catch ex As Exception
MsgBox(ex.ToString())

End Try

End If
End Sub
Private Sub BtnHapus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bthapus.Click
Call BtnHapuskelompok()
End Sub
Sub BtnHapuskelompok()
If Bthapus.Text = "&Hapus" Then
TextBox1.Text = ""
Bthapus.Text = "Ok"
Bttambah.Enabled = False
BtUbah.Enabled = False
btkeluar.Text = "&Batal"
TextBox1.Focus()

Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
Try
Call mykoneksi()

Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text

Tampil.CommandText = "Delete from Jurusan where KdJur='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()

Call Kosong()
TextBox1.Focus()

Catch ex As Exception
MsgBox(ex.ToString())

End Try

End If
End Sub
Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = Chr(Keys.Enter) Then
If Bttambah.Text = "Simpan" Then
Call cekrecord()
If hasilcek = False Then
TextBox2.Focus()
Else
MsgBox("Kode sudah ada boss, silahkan ganti kode lain boss", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Infromasi")
TextBox1.Focus()
End If
Else
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from Jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
While Tampilkan.Read()
If IsDBNull(Tampilkan("kdjur")) Then
TextBox1.Focus()
Else
TextBox2.Text = Tampilkan("Nmjur")
TextBox2.Focus()
End If
End While
Else
MsgBox("Data tidak diketemukan", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Tambah Data")
End If
End If
End If
End Sub

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub
End Class

Contoh koneksi untuk ke Database 2


Public Class Form1

Sub tambahkelompokadit()
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select max(Nourut) as jum from Dataphoto"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
While Tampilkan.Read()
If IsDBNull(Tampilkan("jum")) Then

Else
adit = Tampilkan("jum")

End If

End While
Else
adit = 0
End If
'TextBox1.Text=adit+1

End Sub



Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Call mykoneksi()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Call jurusan()

End Sub
Sub jurusan()
Call mykoneksi()
Tampil.Connection = DataAdit
Tampil.CommandType = CommandType.Text
Tampil.CommandText = "select * from jurusan where kdjur='" & Trim(TextBox1.Text) & "'"
' '" & Trim(Textboxt.Text)& "' ==>untuk karakter
' " & Val(Textboxt.Text)& " ==>untuk numeric
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
'untuk menampilkan apakah recordnya ada atau tidak, jika true ada recordnya
While Tampilkan.Read()
If IsDBNull(Tampilkan("kdjur")) Then
TextBox2.Text = "-"
'untuk mengecek apakah di field kdjur apakah ada datanya atau tidak
Else
TextBox2.Text = Tampilkan("nmjur")
'cara untuk menampilkan data sebuah field
End If
End While
End If
End Sub

End Class

Contoh source program untuk KONEKSI ke Database

Imports System.Data
Imports System.Data.Sql

Module Module1
Public DataAdit As New OleDb.OleDbConnection
Public Tampil As New OleDb.OleDbCommand
Public Tampilkan As OleDb.OleDbDataReader
Public adapter As New OleDb.OleDbDataAdapter
Public ds As New DataSet
Public Tabel, hasilcek As String
Public record As New BindingSource
Public adit As Integer
Public DmlSql As New OleDb.OleDbCommand

Public Sub mykoneksi()
Try
DataAdit.Close()
'server
'DataAdit.ConnectionString = "Provider=SQLOLED.1;Password=;Persist Security Info=True;User ID=sa;Initial Catalog=sim_akuntansi;Data Source=200.200.10.150"

'lokal
DataAdit.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=Klinik_Adit;Data Source=."

DataAdit.Open()


Catch ex As Exception
MsgBox("Koneksi ERROR")

End Try
MsgBox("Koneksi Berhasil")

End Sub

End Module