Posted by : jody baharizki Friday, January 13, 2012

selamat datang di tutorial saya ang ke sekian kalinya, kita akan membuat data teman dengan VB.net
seperti biasa buka aplikasinya FILE - NEW PROJECT - Visual Basic - Windows Form Application - ok
kemudian setelah terbuka tampilanya akan terlihat kosong kemudian buatlah desain seperti berikut :


untuk nama - rumah menggunakan label yang iconya A.
kemudian untuk tombol2nya menggunakan button (di modifikasi sesuai kreatifitas).
untuk image menggunakan picture box
untuk text yang akan kita isi menggunakan textbox


setelah jadi desainya kita masuk ke script

dobel klik pada form1, kemudian ketik sript di bawah ini


Imports System.Data.OleDb
Public Class Form1

    Dim R, maxR As Integer
    Dim ds As New DataSet
    Dim da As OleDb.OleDbDataAdapter

    Private Sub navigasi()
        On Error Resume Next
        Label1.Text = "RECORD KE : " & R + 1
        txtnama.Text = ds.Tables("database1").Rows(R).Item(0)
        txtgender.Text = ds.Tables("database1").Rows(R).Item(1)
        txtTTL.Text = ds.Tables("database1").Rows(R).Item(2)
        txtagama.Text = ds.Tables("database1").Rows(R).Item(3)
        txtrumah.Text = ds.Tables("database1").Rows(R).Item(4)
        PictureBox1.Image = Image.FromFile("D:\tugas\vbnet\data teman\data teman\gambar\" & "images0" & R + 1 & ".jpg")

    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As New OleDb.OleDbConnection
        Dim dbprovider, dbsource, sql As String
        dbprovider = "provider=microsoft.jet.OLEDB.4.0;"
        dbsource = "data source = " & _
        "D:\tugas\vbnet\data teman\data teman\database1.mdb"
        con.ConnectionString = dbprovider & dbsource
        con.Open()
        sql = "select * from Table1"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "database1")
        maxR = ds.Tables("database1").Rows.Count
        navigasi()
    End Sub



    Private Sub btnfirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnfirst.Click
        If R <> 0 Then
            R = 0
            navigasi()

        End If
    End Sub

    Private Sub btnprevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnprevious.Click
        If R > 0 Then
            R = R - 1
            navigasi()
        Else
            MsgBox("first record")
        End If

    End Sub

    Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
        If R <> maxR - 1 Then
            R = R + 1
            navigasi()
        Else
            MsgBox("last record")
        End If

    End Sub

    Private Sub btnlast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnlast.Click
        If R <> maxR - 1 Then
            R = maxR - 1
            navigasi()
     
        End If

    End Sub

    Private Sub btnupdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnupdate.Click
        ds.Tables("database1").Rows(R).Item(0) = txtnama.Text
        ds.Tables("database1").Rows(R).Item(1) = txtgender.Text
        ds.Tables("database1").Rows(R).Item(2) = txtTTL.Text
        ds.Tables("database1").Rows(R).Item(3) = txtagama.Text
        ds.Tables("database1").Rows(R).Item(4) = txtrumah.Text
        da.Update(ds, "database1")
        MsgBox("data updated")

    End Sub

    Sub DelRec()
        Dim cd As New OleDb.OleDbCommandBuilder(da)
        ds.Tables("database1").Rows(R).Delete()
        maxR = maxR - 1
        R = 0
        navigasi()
        da.Update(ds, "database1")
    End Sub


    Private Sub btndelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndelete.Click
        If MessageBox.Show("yakin untuk menghapus ?", "konfirmasi", MessageBoxButtons.OKCancel, _
        Nothing, MessageBoxDefaultButton.Button1) = DialogResult.OK Then
        Else
            MessageBox.Show("batal menghapus")
            Exit Sub
        End If

    End Sub

    Private Sub btnaddnew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnaddnew.Click
        btncommit.Enabled = True
        Panel1.Enabled = False

        txtnama.Clear() : txtgender.Clear() : txtTTL.Clear()
        txtagama.Clear() : txtrumah.Clear() : txtnama.Clear()
        Label1.Text = "record ke : " & maxR + 1

    End Sub

    Private Sub btnCommit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btncommit.Click
        If R <> -1 Then
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("database1").NewRow()
            dsNewRow.Item("Nama") = txtnama.Text
            dsNewRow.Item("Gender") = TxtGender.Text
            dsNewRow.Item("TTL") = TxtTTL.Text
            dsNewRow.Item("Agama") = txtagama.Text
            dsNewRow.Item("Rumah") = txtrumah.Text

            ds.Tables("database1").Rows.Add(dsNewRow)
            da.Update(ds, "database1")
            maxR = ds.Tables("database1").Rows.Count
            R = MaxR
            MsgBox("New Record Added To The Database")
            Panel1.Enabled = True
            btnCommit.Enabled = False
        End If
    End Sub

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

    Private Sub TxtNama_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtnama.KeyPress
        If e.KeyChar = Chr(13) Then txtgender.Focus()
    End Sub

    Private Sub TxtGender_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtgender.KeyPress
        If e.KeyChar = Chr(13) Then TxtTTL.Focus()
    End Sub


    Private Sub TxtTTL_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTTL.KeyPress
        If e.KeyChar = Chr(13) Then txtagama.Focus()
    End Sub


    Private Sub TxtAgama_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtagama.KeyPress
        If e.KeyChar = Chr(13) Then TxtRumah.Focus()
    End Sub


    Private Sub TxtRumah_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtrumah.KeyPress
        If e.KeyChar = Chr(13) Then btnCommit.Focus()
    End Sub
End Class


kalau sudah kita membuat database melalui microsoft acces
dengan nama databasenya Table1
dan fieldnya Nama,Gender,TTL,Agama,Rumah
kemudian simpan 1 folder dengan project anda
*pada script yang menyangkut direktori seperti "D:\gambar\" (diatas berwarna biru) sesuaikan pada direktori anda masing-masing

dan hasilnya...

hasil




*perhatian*
untuk nama pada textbox disesuaikan pada script
contoh txtnama,txtagama,txtrumah. dan lainya
intinya jangan hanya copast kita juga belajar bagaimana menganalisa script ini
semoga sukses

{ 1 comments... read them below or add one }

Psang Iklan?

Psang Iklan?
Klik gambar diatas

Like Us!

Youtube

\
\

Labels

Powered by Blogger.

Total Pageviews

- Copyright © 2016 Jody Baharizki -@jodybaharizki- Powered by Blogger -