Menyimpan Data Yang Menggunakan Radio Button Dan Checkbox

Sebagai langkah awal tentang bagaimana membuat pengolahan database yang di dalamnya menggunakan Radio Button dan CheckBox, maka buatlah form dengan bentuk seperti gambar berikut ini.

Coding :

Imports System.Data.OleDb

 

Public Class Form1

'mendefinisikan gender han hobi yang akan menjadi pilihan

Dim gender As String

Dim hobi As String

 

Sub Kosongkan()

TextBox1.Clear()

TextBox2.Clear()

TextBox3.Clear()

TextBox4.Clear()

RadioButton1.Checked = False

RadioButton2.Checked = False

ComboBox1.Text = ""

CheckBox1.Checked = False

CheckBox2.Checked = False

TextBox1.Focus()

End Sub

 

Sub DataBaru()

TextBox2.Clear()

TextBox3.Clear()

TextBox4.Clear()

RadioButton1.Checked = False

RadioButton2.Checked = False

ComboBox1.Text = ""

CheckBox1.Checked = False

CheckBox2.Checked = False

TextBox2.Focus()

End Sub

 

Sub KETEMU()

On Error Resume Next

CheckBox1.Checked = False

CheckBox2.Checked = False

TextBox2.Text = DR.Item("Nama")

TextBox3.Text = DR.Item("Alamat")

ComboBox1.Text = DR.Item("sTATUS")

'jika gender = Pria maka radiobutton1 di pilih (centang)

If DR.Item("gender") = "Pria" Then

RadioButton1.Checked = True

Else

RadioButton2.Checked = True

End If

 

'jika hobi = Olah Raga maka checkbox1 dipilih (centang)

If DR.Item("hobi") = "Olah Raga" Then

CheckBox1.Checked = True

ElseIf DR.Item("hobi") = "Seni" Then

CheckBox2.Checked = True

ElseIf DR.Item("hobi") = "Olah Raga - Seni" Then

CheckBox1.Checked = True

CheckBox2.Checked = True

End If

End Sub

 

Sub Tampilgrid()

DA = New OleDbDataAdapter("select * from Pegawai", CONN)

DS = New DataSet

DA.Fill(DS)

DGV.DataSource = DS.Tables(0)

DGV.ReadOnly = True

End Sub

 

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

Call Koneksi()

Call Tampilgrid()

End Sub

 

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

TextBox1.MaxLength = 5

If e.KeyChar = Chr(13) Then

CMD = New OleDbCommand("select * from Pegawai where NIP='" & TextBox1.Text & "'", CONN)

DR = CMD.ExecuteReader

DR.Read()

If DR.HasRows Then

Call KETEMU()

TextBox2.Focus()

Else

Call DataBaru()

End If

End If

End Sub

 

Private Sub TextBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox2.KeyPress

TextBox2.MaxLength = 30

If e.KeyChar = Chr(13) Then

TextBox3.Focus()

End If

End Sub

 

Private Sub TextBox3_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox3.KeyPress

TextBox3.MaxLength = 30

If e.KeyChar = Chr(13) Then

TextBox4.Focus()

End If

End Sub

 

 

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

If TextBox1.Text = "" Or TextBox2.Text = "" Or TextBox3.Text = "" Or ComboBox1.Text = "" Then

MsgBox("data belum lengkap")

Exit Sub

Else

'jika radiobutton1 dipilih maka ambil properti text-nya

If RadioButton1.Checked = True Then

gender = RadioButton1.Text

ElseIf RadioButton2.Checked = True Then

gender = RadioButton2.Text

End If

 

'jika checkbox1 dipilih maka ambil properti text-nya

If CheckBox1.Checked = True Then

hobi = CheckBox1.Text

End If

 

If CheckBox2.Checked = True Then

hobi = CheckBox2.Text

End If

 

'jika checkbox1 dan 2 dipilih maka ambil dan gabungkan properti text-nya

If CheckBox1.Checked = True And CheckBox2.Checked = True Then

hobi = CheckBox1.Text & " - " & CheckBox2.Text

End If

 

CMD = New OleDbCommand("select * from Pegawai where NIP='" & TextBox1.Text & "'", CONN)

DR = CMD.ExecuteReader

DR.Read()

If Not DR.HasRows Then

Dim simpan As String = "insert into Pegawai values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "','" & gender & "','" & ComboBox1.Text & "','" & hobi & "')"

CMD = New OleDbCommand(simpan, CONN)

CMD.ExecuteNonQuery()

Else

Dim edit As String = "update Pegawai set Nama='" & TextBox2.Text & "',Alamat='" & TextBox3.Text & "',gender='" & gender & "',status='" & ComboBox1.Text & "',hobi='" & hobi & "' where NIP='" & TextBox1.Text & "'"

CMD = New OleDbCommand(edit, CONN)

CMD.ExecuteNonQuery()

End If

Call Kosongkan()

Call Tampilgrid()

End If

End Sub

 

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

If TextBox1.Text = "" Then

MsgBox("NIP harus diisi dulu")

TextBox1.Focus()

Exit Sub

Else

If MessageBox.Show("hapus data ini...?", "", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then

Dim hapus As String = "delete from Pegawai where NIP='" & TextBox1.Text & "'"

CMD = New OleDbCommand(hapus, CONN)

CMD.ExecuteNonQuery()

Call Kosongkan()

Call Tampilgrid()

Else

Call Kosongkan()

End If

End If

End Sub

 

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

Call Kosongkan()

End Sub

 

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

Me.Close()

End Sub

 

Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged

DA = New OleDbDataAdapter("select * from Pegawai where Nama like '%" & TextBox4.Text & "%'", CONN)

DS = New DataSet

DA.Fill(DS)

DGV.DataSource = DS.Tables(0)

End Sub

 

Private Sub DGV_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGV.CellMouseClick

On Error Resume Next

TextBox1.Text = DGV.Rows(e.RowIndex).Cells(0).Value

CMD = New OleDbCommand("select * from Pegawai where NIP='" & TextBox1.Text & "'", CONN)

DR = CMD.ExecuteReader

DR.Read()

If DR.HasRows Then

Call KETEMU()

End If

End Sub

 

End Class