SHARE
TWEET

MS Access : Source Code Lengkap Input & Edit Data

stevennathaniel Feb 21st, 2016 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Imports System.Data.OleDb
  2.  
  3. Imports System.Data
  4.  
  5. Public Class Form1
  6.  
  7.     Dim idpelanggan As String
  8.  
  9.     Dim namapelanggan As String
  10.  
  11.     Dim alamatpelanggan As String
  12.  
  13.     Dim oledbKoneksi As OleDbConnection = New OleDbConnection
  14.  
  15.     Dim oledbPerintah As OleDbCommand = New OleDbCommand
  16.  
  17.     Dim lokasiFile As String = "D:\fileAccess\"
  18.  
  19.     Dim namaFile As String = "Database1.accdb"
  20.  
  21.     Dim stringKoneksi As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & lokasiFile & namaFile & ";" & "Persist Security Info=False;"
  22.  
  23.     Dim adapter As New OleDbDataAdapter
  24.  
  25.  
  26.     Sub tampilIsiGrid()
  27.  
  28.         oledbKoneksi.ConnectionString = stringKoneksi
  29.  
  30.         oledbKoneksi.Open()
  31.  
  32.         oledbPerintah.Connection = oledbKoneksi
  33.  
  34.         oledbPerintah.CommandText = "Select * From datapelanggan"
  35.  
  36.         adapter.SelectCommand = oledbPerintah
  37.  
  38.         Dim ds As DataSet
  39.  
  40.         ds = New DataSet
  41.  
  42.         adapter.Fill(ds)
  43.  
  44.         DataGridView1.DataSource = ds.Tables(0)
  45.  
  46.         oledbKoneksi.Close()
  47.  
  48.  
  49.     End Sub
  50.  
  51.  
  52.     Sub autoID()
  53.  
  54.         oledbKoneksi.ConnectionString = stringKoneksi
  55.  
  56.         oledbKoneksi.Open()
  57.  
  58.         oledbPerintah.Connection = oledbKoneksi
  59.  
  60.         oledbPerintah.CommandText = "SELECT * FROM datapelanggan ORDER BY idpelanggan desc"
  61.  
  62.         Dim baca As OleDbDataReader = oledbPerintah.ExecuteReader
  63.  
  64.         baca.Read()
  65.  
  66.         If Not baca.HasRows Then
  67.  
  68.             TextBox1.Text = "DP" + "0001"
  69.  
  70.         Else
  71.  
  72.             TextBox1.Text = Val(Microsoft.VisualBasic.Mid(baca.Item("idpelanggan").ToString, 4, 3)) + 1
  73.  
  74.             If Len(TextBox1.Text) = 1 Then
  75.  
  76.                 TextBox1.Text = "DP000" & TextBox1.Text & ""
  77.  
  78.             ElseIf Len(TextBox1.Text) = 2 Then
  79.  
  80.                 TextBox1.Text = "DP00" & TextBox1.Text & ""
  81.  
  82.             ElseIf Len(TextBox1.Text) = 3 Then
  83.  
  84.                 TextBox1.Text = "DP0" & TextBox1.Text & ""
  85.  
  86.  
  87.             End If
  88.         End If
  89.  
  90.         oledbKoneksi.Close()
  91.  
  92.  
  93.     End Sub
  94.  
  95.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  96.  
  97.         'Simpan Data
  98.  
  99.         idpelanggan = TextBox1.Text
  100.  
  101.         namapelanggan = TextBox2.Text
  102.  
  103.         alamatpelanggan = TextBox3.Text
  104.  
  105.         oledbKoneksi.ConnectionString = stringKoneksi
  106.  
  107.         oledbKoneksi.Open()
  108.  
  109.         oledbPerintah.Connection = oledbKoneksi
  110.  
  111.         oledbPerintah.CommandText = "INSERT INTO datapelanggan(idpelanggan,namapelanggan,alamatpelanggan) values" & "('" & idpelanggan & "', '" & namapelanggan & "', '" & alamatpelanggan & "')"
  112.  
  113.         oledbPerintah.ExecuteNonQuery()
  114.  
  115.         oledbKoneksi.Close()
  116.  
  117.         TextBox2.Text = ""
  118.  
  119.         TextBox3.Text = ""
  120.  
  121.         autoID()
  122.  
  123.         DataGridView1.Refresh()
  124.  
  125.         tampilIsiGrid()
  126.  
  127.  
  128.  
  129.     End Sub
  130.  
  131.     Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  132.  
  133.         autoID()
  134.  
  135.  
  136.         tampilIsiGrid()
  137.  
  138.  
  139.     End Sub
  140.  
  141.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  142.  
  143.         'Data Baru
  144.  
  145.         autoID()
  146.  
  147.         TextBox2.Text = ""
  148.  
  149.         TextBox3.Text = ""
  150.  
  151.  
  152.  
  153.     End Sub
  154.  
  155.     Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  156.  
  157.         'Tampil data dari datagridview ke textbox
  158.  
  159.         Dim row As DataGridViewRow = DataGridView1.CurrentRow
  160.  
  161.         Me.TextBox1.Text = row.Cells(0).Value.ToString
  162.  
  163.         Me.TextBox2.Text = row.Cells(1).Value.ToString
  164.  
  165.         Me.TextBox3.Text = row.Cells(2).Value.ToString
  166.  
  167.  
  168.     End Sub
  169.  
  170.     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
  171.  
  172.         'tombol untuk mengupdate data
  173.  
  174.         idpelanggan = TextBox1.Text
  175.  
  176.         namapelanggan = TextBox2.Text
  177.  
  178.         alamatpelanggan = TextBox3.Text
  179.  
  180.         oledbKoneksi.ConnectionString = stringKoneksi
  181.  
  182.         oledbKoneksi.Open()
  183.  
  184.         oledbPerintah.Connection = oledbKoneksi
  185.  
  186.         oledbPerintah.CommandText = "UPDATE datapelanggan SET namapelanggan = '" & namapelanggan & "', alamatpelanggan = '" & alamatpelanggan & "' WHERE idpelanggan = '" & idpelanggan & "'"
  187.  
  188.         oledbPerintah.ExecuteNonQuery()
  189.  
  190.         oledbKoneksi.Close()
  191.  
  192.         TextBox2.Text = ""
  193.  
  194.         TextBox3.Text = ""
  195.  
  196.         autoID()
  197.  
  198.         DataGridView1.Refresh()
  199.  
  200.         tampilIsiGrid()
  201.  
  202.     End Sub
  203.  
  204.     Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
  205.  
  206.         'Hapus Data
  207.  
  208.         idpelanggan = TextBox1.Text
  209.  
  210.         namapelanggan = TextBox2.Text
  211.  
  212.         alamatpelanggan = TextBox3.Text
  213.  
  214.         oledbKoneksi.ConnectionString = stringKoneksi
  215.  
  216.         oledbKoneksi.Open()
  217.  
  218.         oledbPerintah.Connection = oledbKoneksi
  219.  
  220.         oledbPerintah.CommandText = "DELETE * FROM datapelanggan WHERE idpelanggan = '" & idpelanggan & "'"
  221.  
  222.         oledbPerintah.ExecuteNonQuery()
  223.  
  224.         oledbKoneksi.Close()
  225.  
  226.         TextBox2.Text = ""
  227.  
  228.         TextBox3.Text = ""
  229.  
  230.         autoID()
  231.  
  232.         DataGridView1.Refresh()
  233.  
  234.         tampilIsiGrid()
  235.  
  236.  
  237.     End Sub
  238. End Class
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top