Selasa, 24 Januari 2012

DATA TEMAN

hai ketemu lagi dengan saya :D

disini saya mau bagi bagi tentang vb.net lo , ini salah satu tugas saya semester 2 .
semoga bermanfaat bagi anda .

pertama buat desain seperti ini ya .. 
*RECORD KE : (label1)
*Nama, Gender, TTL, Agama, Rumah (textbox)
*<<, <, >, >>, Update, Delete ini berada di panel
*button X yang dikasik warna merah
*Addnew, Commit (button)
*yang kotak itu pakek picture box1

 

kedua itu buat databasenya ..
setelah buat databasenya save dengan nama (Database1) dengan format access 2007 


ketiga save gambar dengan format JPEG dengan nama image1 dan seterusnya sebanyak yang kamu mau .


keempat mulai mengcoding ..

Imports System.Data.OleDb

Public Class Form1
    Dim R, MaxR As Integer
    Dim ds As New DataSet
    Dim da As 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 ("E:\Data Moklet\Kelas 2\VB\Semester 2\DATA TEMAN\" &   "image" & R + 1 & ".jpg")
    End Sub

    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("E:\Data Moklet\Kelas 2\VB\Semester 2\DATA TEMAN\" & "image" & 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 OleDbConnection
        Dim dbProvider, dbSource, SQL As String
        dbProvider = "Provider=Microsoft.ACE.OLEDB.12.0;"
        dbSource = "Data Source = E:\Data Moklet\Kelas 2\VB\Semester 2\DATA TEMAN\Database1.accdb"
        con.ConnectionString = dbProvider & dbSource
        con.Open()
        SQL = "SELECT * from Table1"
        da = New OleDbDataAdapter(SQL, con)
        da.Fill(ds, "Database1")
        MaxR = ds.Tables("Database1").Rows.Count
        NAVIGASI()
    End Sub

    Private Sub btnFirst_Click_1(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_1(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_1(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_1(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_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
        Dim cv As New OleDbCommandBuilder(da)
        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 Update")
    End Sub

    Sub DelRec()
        Dim cb 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_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDelete.Click
        If MessageBox.Show("Mau Delete??", "Konfirmasi", MessageBoxButtons.OKCancel, _
    Nothing, MessageBoxDefaultButton.Button1) = DialogResult.OK Then
            DelRec()
        Else
            MessageBox.Show("Gak Jadi Delete :)")
            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.Focus()
        Label1.Text = "RECORD KE : " & MaxR + 1
    End Sub

    Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
        If R <> -1 Then
            Dim cb As New 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 System.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 udah coding skr setting connection antara database sama vb.net
langkahnya pilih project kemudian pilih paling bawah sendiri kalau aku nama file nya DATA TEMAN jadi munculnya DATA TEMAN Properties kemudian liat gambar dibawah ini .


 

jika tidak ada error nya maka akan tampil seperti ini outputnya



kalau ada yang ditanyakan mending coment aja :D
makasih :)

Tidak ada komentar:

Posting Komentar