How to CRUD Using Parameter In Visual Basic.Net and MySQL

Please Create Form Like Belows Using Visual Basic Net 2010 or Laters, I took this tutorial from My Project I was directly in making an accounting application. In this study Case I use Visual Basic Net.2010 and MySQL datatabase, You Can Use Micorosoft Acces or Microsoft SQL Server for database Or MySQL like me


In Genenal Please Use Declaration Below:

Imports MySql.Data.MySqlClient


Public Class FrmPerkiraan
    Dim objPerkiraan As New ClsPerkiraan

    'Membersihkan Entry Isian
    Public Sub BersihkanIsianText()
        Try
            txtNoper.Text = ""
            txtNamaPerkiraan.Text = ""
            txtNoper.Enabled = True
            CboGroup.Text = ""
            cboKeterangan.Text = ""
            txtNoper.Focus()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub CmdKeluar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmdKeluar.Click
        With objPerkiraan
            .keluar()
        End With
    End Sub

    Private Sub txtNoper_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If e.KeyChar = Chr(13) Then
            If (txtNoper.Text = "") Then
                MsgBox("Nomor Perkiraan tidak boleh Kosong", MsgBoxStyle.Critical, "Error")
            Else
                If Len(txtNoper.Text) < 6 Then
                    MsgBox("Panjang Nomor Perkiraan harus 6 Digit", MsgBoxStyle.Exclamation, "Error")
                    txtNoper.Focus()
                Else
                    txtNamaPerkiraan.Focus()
                End If
            End If
        End If
    End Sub

    Private Sub FrmPerkiraan_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        txtNoper.Focus()
    End Sub

    Private Sub FrmPerkiraan_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            If Koneksi() = True Then

                If FormEditStatus = True Then
                    txtNoper.ReadOnly = True
                    cmdSimpan.Enabled = True
                    cmdSimpan.Text = "Update"
                    'Mode Add Data
                Else
                    txtNoper.ReadOnly = False
                    'Kode_Perkiraan()
                    BersihkanIsianText()
                    cmdSimpan.Enabled = True
                    cmdSimpan.Text = "Simpan"
                End If
            End If
        Catch ex As Exception
            MsgBox("Koneksi data gagal", CType(MsgBoxStyle.Information + MsgBoxStyle.OkOnly, MsgBoxStyle), "Pesan")
        End Try
    End Sub

    Private Sub cmdSimpan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSimpan.Click

        Dim A As String

        Try
            If txtNoper.Text = "" Then
                MsgBox("Nomor Perkiraan tidak boleh kosong", MsgBoxStyle.Critical, "Error")
                txtNoper.Focus()
            ElseIf Trim(txtNamaPerkiraan.Text) = "" Then
                MsgBox("Nama Perkiraan tidak boleh kosong", MsgBoxStyle.Critical, "Error")
                txtNamaPerkiraan.Focus()
            ElseIf CboGroup.Text = "" Then
                MsgBox("Kelompok Perkiraan, masih kosong..", MsgBoxStyle.Information, "Konfirmasi")
                CboGroup.Focus()
            Else

                A = CStr(MsgBox("Apakah data sudah benar ....! " & vbCrLf & vbCrLf _
                               & "Kode Perkiraan --------> " & txtNoper.Text & " " & vbCrLf _
                               & "Nama Perkiraan -------> " & txtNamaPerkiraan.Text & " " & vbCrLf _
                               & "Kelompok Perkiraan --> " & CboGroup.Text & " " & vbCrLf & vbCrLf _
                               & "Silahkan Pilih [OK] untuk menyimpan data" & vbCrLf _
                               & "", CType(MsgBoxStyle.Information + MsgBoxStyle.OkCancel, MsgBoxStyle), "Konfirmasi Penyimpanan Data"))

                Select Case A
                    Case CStr(vbCancel)
                        txtNoper.Focus()
                        Exit Sub
                    Case CStr(vbOK)
                        If cmdSimpan.Text = "Simpan" Then
                            SimpanDataPerkiraan()
                            BersihkanIsianText()
                        Else
                            UpdateDataPerkiraan()
                        End If
                        FrmPerkiraanPaging.ToolStripRefresh.PerformClick()
                End Select
            End If
        Catch ex As Exception
        End Try
    End Sub

    Private Sub SimpanDataPerkiraan()
        Dim Cmd As New MySqlCommand
        Dim dtAdapter As New MySqlDataAdapter
        Dim dtTable As New DataTable

        Try

            With objPerkiraan
                .mKode = txtNoper.Text
                .mPerkiraan = txtNamaPerkiraan.Text
                .mGroup = CboGroup.Text
                .mKet = cboKeterangan.Text
                'Panggil methode simpan data

                Cmd.Connection = Conn
                Cmd.CommandText = "Select * From tblmasterakun where Noperkiraan= '" & .mKode & "'"
                dtAdapter.SelectCommand = Cmd
                dtAdapter.Fill(dtTable)

                If CBool(dtTable.Rows.Count - 1) Then
                    Cmd.CommandText = "INSERT INTO tblmasterakun VALUES('" & .mKode & "','" & .mPerkiraan & "','" & .mGroup & "','" & .mKet & "','Y') "
                    Cmd.ExecuteNonQuery()
                    MsgBox("Data sudah tersimpan", CType(MsgBoxStyle.Information + MsgBoxStyle.OkOnly, MsgBoxStyle), "Simpan Data")
                    txtNoper.Focus()
                End If

            End With
        Catch ex As Exception
        End Try
    End Sub

    Private Sub UpdateDataPerkiraan()
        Dim A As String
        Dim Cmd As New MySqlCommand
        Dim dtAdapter As New MySqlDataAdapter
        Dim dtTable As New DataTable

        A = CStr(MsgBox("Benar Nama Perkiraan akan diperbaiki menjadi - " & txtNamaPerkiraan.Text & "", CType(MsgBoxStyle.Question + MsgBoxStyle.OkCancel, MsgBoxStyle), "Konfirmasi Edit Data"))
        Select Case A
            Case CStr(vbCancel)
                txtNoper.Focus()
                Exit Sub
            Case CStr(vbOK)
                Try
                    With objPerkiraan
                        .mKode = txtNoper.Text
                        .mPerkiraan = txtNamaPerkiraan.Text
                        .mGroup = CboGroup.Text
                        .mKet = cboKeterangan.Text
                        'Panggil methode simpan data

                        Cmd.Connection = Conn
                        Cmd.CommandText = "UPDATE tblmasterakun SET NamaPerkiraan='" & .mPerkiraan & "',Kelompok='" & .mGroup & "', Keterangan='" & .mKet & "' WHERE NoPerkiraan= '" & .mKode & "'"
                        Cmd.ExecuteNonQuery()
                        MsgBox("Data sudah diperbaiki", CType(MsgBoxStyle.Information + MsgBoxStyle.OkOnly, MsgBoxStyle), "Perbaiki Data Succes")
                        txtNoper.Focus()
                    End With
                Catch ex As Exception
                    MsgBox(ex.Message, MsgBoxStyle.Information, "Perhatian")
                End Try
        End Select
    End Sub

    Private Sub cboKeterangan_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
        If (e.KeyChar = Chr(13)) Then
            If (cboKeterangan.Text) = "" Then
                MsgBox("Keterangan Perkiraan tidak boleh kosong", CType(MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, MsgBoxStyle), "Error")
            Else
                cmdSimpan.Focus()
            End If
        End If
    End Sub

    Public Sub TampilDataPerkiraan()

        Try
            Query = "Select NoPerkiraan, NamaPerkiraan, Kelompok, Keterangan From tblmasterakun  where NoPerkiraan = '" & Trim(txtNoper.Text) & "'"

            Cmd = New MySqlCommand(Query, Conn)
            RD = Cmd.ExecuteReader
            RD.Read()
            If RD.HasRows Then
                txtNoper.Text = Convert.ToString(RD.Item("NoPerkiraan"))
                txtNamaPerkiraan.Text = Convert.ToString(RD.Item("NamaPerkiraan"))
                CboGroup.Text = Convert.ToString(RD.Item("Kelompok"))
                cboKeterangan.Text = Convert.ToString(RD.Item("Keterangan"))
            Else
                txtNamaPerkiraan.Text = ""
                CboGroup.Text = ""
                cboKeterangan.Text = ""
            End If
            RD.Close()
        Catch ex As Exception
            RD.Close()
            MsgBox(ex.Message).ToString()
        End Try
    End Sub

    Private Sub CboGroup_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles CboGroup.KeyPress, cboKeterangan.KeyPress
        If (e.KeyChar = Chr(13)) Then
            If (cboKeterangan.Text) = "" Then
                MsgBox("Kelompok Perkiraan tidak boleh kosong", CType(MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, MsgBoxStyle), "Error")
                CboGroup.Focus()
            Else
                cboKeterangan.Focus()
            End If
        End If
    End Sub
End Class

0 Response to "How to CRUD Using Parameter In Visual Basic.Net and MySQL"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel