0% found this document useful (0 votes)
8 views3 pages

Koding

Uploaded by

211210012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Koding

Uploaded by

211210012
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.

Object, ByVal e
As System.EventArgs)

End Sub
Imports System.Data.OleDb
Public Class Form1
Dim con As New OleDbConnection
Sub open_koneksi()
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0" & _
";Data Source=perkuliahan.accdb;"
con.Open()
End Sub
Sub data_load()
Dim cmd As New OleDbCommand
Dim adapt As New OleDbDataAdapter
Dim dt As New DataTable

Try
If Not con.State = ConnectionState.Open Then open_koneksi()

cmd.Connection = con
cmd.CommandText = "select * from mhs"
adapt.SelectCommand = cmd
adapt.Fill(dt)

With DataGridView1
.AllowUserToAddRows = False
.ReadOnly = True
.DataSource = dt
End With
Catch ex As Exception
MessageBox.Show("data error")
Finally
con.Close()

End Try
End Sub
Sub data_load_tabel()
Dim cmd As New OleDbCommand
Dim adapt As New OleDbDataAdapter
Dim dt As New DataTable

Try
If Not con.State = ConnectionState.Open Then open_koneksi()

cmd.Connection = con
cmd.CommandText = "select * from pddk"
adapt.SelectCommand = cmd
adapt.Fill(dt)

With DataGridView2
.AllowUserToAddRows = False
.ReadOnly = True
.DataSource = dt
End With
Catch ex As Exception
MessageBox.Show("Data Error")
Finally
con.Close()
End Try
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
data_load()
data_load_tabel()
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button1.Click
Dim MyCommand As New OleDbCommand
Dim Sql As String
Try
If Not con.State = ConnectionState.Open Then open_koneksi()
If CheckBox1.Checked Then
' If CheckBox is checked, use INSERT INTO pddk query
Sql = "INSERT INTO pddk(nim, nm_mhs) VALUES " & _
"('" & TextBox1.Text & "', '" & TextBox2.Text & "')"
Else
' If CheckBox is not checked, use INSERT INTO mhs query
Sql = "INSERT INTO mhs(nim, nm_mhs) VALUES " & _
"('" & TextBox1.Text & "', '" & TextBox2.Text & "')"
End If
MyCommand.Connection = con
MyCommand.CommandText = Sql
MyCommand.ExecuteNonQuery()

MsgBox("Data Berhasil diinput")

TextBox1.Text = String.Empty
TextBox2.Text = String.Empty
data_load()

con.Close()

Catch ex As Exception
MsgBox("Gagal")
End Try
con.Dispose()
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button2.Click
If MsgBox("Yakin Akan Menghapus data ini ?", MsgBoxStyle.OkCancel, _
"Konfirmasi") = MsgBoxResult.No Then Exit Sub
Dim MyCommand As New OleDbCommand
Dim Sql As String

Try
If Not con.State = ConnectionState.Open Then open_koneksi()
Sql = "DELETE FROM mhs WHERE nim = " & _
"'" & DataGridView1.CurrentRow.Cells(1).Value & "'"

MyCommand.Connection = con
MyCommand.CommandText = Sql
MyCommand.ExecuteNonQuery()
data_load()
con.Close()
Catch ex As Exception
MessageBox.Show("Error:")
Finally
con.Dispose()

End Try
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As


System.EventArgs) Handles Button3.Click
Dim mycommand As New OleDbCommand
Dim str As String
Try
If Not con.State = ConnectionState.Open Then open_koneksi()
str = "update mhs set nim = '" & TextBox1.Text & "', nm_mhs= '" &
TextBox2.Text & "' where nim = '" & TextBox1.Text & "'"

mycommand = New OleDbCommand(str, con)


mycommand.ExecuteNonQuery()
data_load()

MessageBox.Show("data berhasil di update")


Catch ex As Exception
MessageBox.Show("data gagal di update")
End Try
End Sub
Private Sub CheckBox1_CheckedChanged_2(ByVal sender As System.Object, ByVal e
As System.EventArgs)
If CheckBox1.Checked Then
CheckBox1.Text = "mhs"
Else
CheckBox1.Text = "pddk"
End If
End Sub

You might also like