Sabtu, 21 Mei 2011

Contoh Form Login


Public Class LoginForm1

' TODO: Insert code to perform custom authentication using the provided username and password
' (See http://go.microsoft.com/fwlink/?LinkId=35339).
' The custom principal can then be attached to the current thread's principal as follows:
' My.User.CurrentPrincipal = CustomPrincipal
' where CustomPrincipal is the IPrincipal implementation used to perform authentication.
' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
' such as the username, display name, etc.

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
If UsernameTextBox.Text = "adit" And PasswordTextBox.Text = "keren" Then
MDIParent1.Show()
Me.Visible = False
Else
MsgBox("Login gagal,silahkan ulangi lagi")
End If

End Sub

Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
Me.Close()
End Sub


End Class

Contoh Program yang Ane Buat


Public Class DATA_CUSTOMER

Sub Kosong()
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox1.Focus()
End Sub
Sub netral()
Bttambah.Text = "&Input"
BtUbah.Text = "&Edit"
Bthapus.Text = "&Delete"
btkeluar.Text = "&Exit"
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 data_customer where kd_cust='" & 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 = "&Input" Then
TextBox1.Text = ""
Bttambah.Text = "Save"
BtUbah.Enabled = False
Bthapus.Enabled = False
btkeluar.Text = "&Cancel"
'TextBox1.Enabled = False
TextBox1.Focus()
Else
If TextBox1.Text = "" Then Exit Sub
If TextBox2.Text = "" Then Exit Sub
If TextBox2.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 data_customer (kd_cust,Nama_cust,Alamat,Contact)values( '" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & TextBox4.Text & "')"
Tampil.ExecuteNonQuery()

Call Kosong()

Else
MsgBox("Data sudah ada bro, 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 = "&Exit" Then
Me.Close()
Else
Call Kosong()
Call netral()
End If
End Sub
Private Sub BtUbah_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtUbah.Click
Call Btnubahkelompok()
End Sub
Sub Btnubahkelompok()
If BtUbah.Text = "&Edit" Then
TextBox1.Text = ""
BtUbah.Text = "Save"
Bttambah.Enabled = False 'enabled= aktif/tidak aktif
Bthapus.Enabled = False
btkeluar.Text = "&Cancel"
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
If TextBox3.Text = "" Then Exit Sub
If TextBox4.Text = "" Then Exit Sub
Try
Call mykoneksi() 'untuk mengecek data kdjur ada apa tidak

Tampil.Connection = DataAdit
tampil2.Connection = DataAdit
tampil3.Connection = DataAdit
Tampil.CommandType = CommandType.Text
tampil2.CommandType = CommandType.Text
tampil3.CommandType = CommandType.Text
Tampil.CommandText = "Update data_customer Set Nama_cust='" & Trim(TextBox2.Text) & "'where kd_cust ='" & Trim(TextBox1.Text) & "'"
tampil2.CommandText = "Update data_customer Set Alamat='" & Trim(TextBox3.Text) & "'where kd_cust ='" & Trim(TextBox1.Text) & "'"
tampil3.CommandText = "Update data_customer Set Contact='" & Trim(TextBox4.Text) & "'where kd_cust ='" & Trim(TextBox1.Text) & "'"
Tampil.ExecuteNonQuery()
tampil2.ExecuteNonQuery()
tampil3.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 = "&Delete" Then
TextBox1.Text = ""
Bthapus.Text = "Ok"
Bttambah.Enabled = False
BtUbah.Enabled = False
btkeluar.Text = "&Cancel"
TextBox1.Focus()

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

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

Tampil.CommandText = "Delete from data_customer where kd_cust='" & 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 = "Save" 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 data_customer where kd_cust='" & Trim(TextBox1.Text) & "'"
Tampilkan = Tampil.ExecuteReader

If Tampilkan.HasRows = True Then
While Tampilkan.Read()
If IsDBNull(Tampilkan("kd_cust")) Then
TextBox1.Focus()
Else
TextBox2.Text = Tampilkan("Nama_cust")
TextBox2.Focus()
TextBox3.Text = Tampilkan("Alamat")
TextBox3.Focus()
TextBox4.Text = Tampilkan("Contact")
TextBox4.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 Yang ane buat

Imports System.Data
Imports System.Data.Sql

Module Module1
Public DataAdit As New OleDb.OleDbConnection
Public Tampil As New OleDb.OleDbCommand
Public tampil2 As New OleDb.OleDbCommand
Public tampil3 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=customer;Data Source=."

DataAdit.Open()

MsgBox("Koneksi Berhasil")
Catch ex As Exception
MsgBox("Koneksi ERROR")

End Try


End Sub

End Module

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