Dear agan-agan sekalian para pecinta VB terutama bagi yang baru saja beralih dari VB 6 ke VB.Net seperti saya ini.
nah kali ini kita akan coding dengan database SQL Server 2000, naik level dari tutorial sebelumnya yang masih menggunakan database Ms.Access.
silahkan membuat form seperti di atas dengan properti ToolStrip, DatagridView, Textbox dan Label. untuk penamaan propertinya silahkan mengikuti coding dibawah atau menamakannya dengan nama lain, intinya paham dan aplikasi bisa running.
======================================================
Private Sub DATA_DIVISI()
Call KONEKSI()
DS = New DataSet
TABEL = "SELECT * FROM TABEL_DIVISI"
GRID = New OleDb.OleDbDataAdapter(TABEL, DATABASE)
GRID.Fill(DS, "TABEL_DIVISI")
Dim GRIDVIEW As New DataView(DS.Tables("TABEL_DIVISI"))
DGDIVISI.DataSource = GRIDVIEW
DGDIVISI.Columns(0).Width = 50
DGDIVISI.Columns(1).Width = 190
End Sub
======================================================
'Koding AutoNumber ini agak naik LEVEL lah..
Private Sub NOMOR_DIVISI()
Call KONEKSI()
TAMPIL.Connection = DATABASE
TAMPIL.CommandType = CommandType.Text
TAMPIL.CommandText = "SELECT * FROM TABEL_DIVISI"
TAMPILKAN = TAMPIL.ExecuteReader
If TAMPILKAN.HasRows = True Then
While TAMPILKAN.Read()
'NOMORDIVISI = Nama FIELD DATABASE
TxtNOMOR.Text = TAMPILKAN("NOMORDIVISI")
End While
TxtNOMOR.Text = TxtNOMOR.Text + 1
If Len(TxtNOMOR.Text) = 1 Then
TxtNOMOR.Text = "00" & TxtNOMOR.Text & ""
ElseIf Len(TxtNOMOR.Text) = 2 Then
TxtNOMOR.Text = "0" & TxtNOMOR.Text & ""
ElseIf Len(TxtNOMOR.Text) = 3 Then
TxtNOMOR.Text = "" & TxtNOMOR.Text & ""
Else
TxtNOMOR.Text = TxtNOMOR.Text
End If
TxtDIVISI.Focus()
Else
TxtNOMOR.Text = "001"
End If
End Sub
=====================================================
Private Sub KUNCI()
TxtNOMOR.Enabled = False
TxtDIVISI.Enabled = False
End Sub
=====================================================
Private Sub BUKA()
TxtDIVISI.Enabled = True
End Sub
=====================================================
Private Sub KOSONG()
TxtNOMOR.Text = ""
TxtDIVISI.Text = ""
TxtDIVISI.Focus()
BTNINPUT.Enabled = True
BTNUBAH.Enabled = False
BTNHAPUS.Enabled = False
TOOLINPUT.Text = "INPUT"
TOOLUBAH.Text = "UBAH"
TOOLHAPUS.Text = "HAPUS"
TOOLEXIT.Text = "EXIT"
Call DATA_DIVISI()
End Sub
======================================================
Private Sub FORM_DIVISI_02_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
Call KOSONG()
Call KUNCI()
TxtNOMOR.Enabled = False
TxtDIVISI.CharacterCasing = CharacterCasing.Upper
TxtDIVISI.Focus()
End Sub
=====================================================
Private Sub BTNINPUT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNINPUT.Click
If TOOLINPUT.Text = "INPUT" Then
Call DATA_DIVISI()
Call NOMOR_DIVISI()
Call BUKA()
TOOLINPUT.Text = "SIMPAN"
TOOLEXIT.Text = "BATAL"
TxtDIVISI.Focus()
ElseIf TOOLINPUT.Text = "SIMPAN" Then
If TxtDIVISI.Text = "" Then
MsgBox("PASTIKAN ANDA MENGISI DATA !!", MsgBoxStyle.Information, "WIRAT")
Call KOSONG()
Call KUNCI()
Else
Try
Call KONEKSI()
DMLSQL.Connection = DATABASE
DMLSQL.CommandType = CommandType.Text
DMLSQL.CommandText = "INSERT INTO TABEL_DIVISI VALUES('" & TxtNOMOR.Text & "','" & TxtDIVISI.Text & "')"
DMLSQL.ExecuteNonQuery()
MsgBox("DATA TERSIMPAN !!", MsgBoxStyle.Information, "WIRAT")
Call KOSONG()
Call KUNCI()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End If
End Sub
=====================================================
Private Sub BTNEXIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNEXIT.Click
If TOOLEXIT.Text = "BATAL" Then
Call KOSONG()
Call KUNCI()
Else
Me.Close()
End If
End Sub
=====================================================
Private Sub DGDIVISI_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGDIVISI.DoubleClick
Try
TxtNOMOR.Text = DGDIVISI.SelectedCells(0).Value
TxtDIVISI.Text = DGDIVISI.SelectedCells(1).Value
BTNINPUT.Enabled = False
BTNUBAH.Enabled = True
BTNHAPUS.Enabled = True
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
======================================================
Private Sub BTNUBAH_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNUBAH.Click
If TOOLUBAH.Text = "UBAH" Then
TOOLUBAH.Text = "OK"
TOOLEXIT.Text = "BATAL"
Call BUKA()
TxtDIVISI.Focus()
ElseIf TOOLUBAH.Text = "OK" Then
If TxtDIVISI.Text = "" Then
MsgBox("PASTIKAN DATA UNTUK DIUBAH", MsgBoxStyle.Exclamation, "WIRAT")
Else
'Dim TANYA As String = MsgBox("YAKIN AKAN MERUBAH DATA " & TxtNOMOR.Text & " ??", MsgBoxStyle.YesNo, "WIRAT")
'If TANYA = vbYes Then
'If MsgBox("YAKIN AKAN MERUBAH DATA " & TxtNOMOR.Text & " ??", MsgBoxStyle.YesNo, "WIRAT") = MsgBoxResult.Yes Then
Try
Call KONEKSI()
DMLSQL.Connection = DATABASE
DMLSQL.CommandType = CommandType.Text
DMLSQL.CommandText = "UPDATE TABEL_DIVISI SET DIVISI='" & TxtDIVISI.Text & "'" & _
"WHERE NOMORDIVISI ='" & TxtNOMOR.Text & "'"
DMLSQL.ExecuteNonQuery()
Call KOSONG()
MsgBox("DATA TELAH DI UBAH !!", MsgBoxStyle.Information, "WIRAT")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'Else
'Exit Sub
'End If
End If
End If
End Sub
======================================================
Private Sub BTNHAPUS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTNHAPUS.Click
'If MsgBox("YAKIN HAPUS DATA " & TxtNOMOR.Text & " ?? ", MsgBoxStyle.OkCancel, "WIRAT") = MsgBoxResult.Ok Then
Try
Call KONEKSI()
DMLSQL.Connection = DATABASE
DMLSQL.CommandType = CommandType.Text
DMLSQL.CommandText = "DELETE FROM TABEL_DIVISI WHERE NOMORDIVISI='" & TxtNOMOR.Text & "'"
DMLSQL.ExecuteNonQuery()
Call KOSONG()
MsgBox("DATA TELAH DI HAPUS !!", MsgBoxStyle.Information, "WIRAT")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
'Else
'Exit Sub
'End If
End Sub
Comments
Post a Comment