Advertisement
stevennathaniel

MS Access : Source Code Lengkap Input, Edit, Hapus & Preview

Feb 22nd, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 6.05 KB | None | 0 0
  1. Imports System.Data.OleDb
  2.  
  3. Imports System.Data
  4.  
  5. Imports Access = Microsoft.Office.Interop.Access
  6.  
  7. Imports Microsoft.Office
  8.  
  9.  
  10. Public Class Form2
  11.  
  12.     Dim idbarang As String
  13.  
  14.     Dim namabarang As String
  15.  
  16.     Dim hargabarang As String
  17.  
  18.     Dim namaprodusen As String
  19.  
  20.     Dim oledbKoneksi As OleDbConnection = New OleDbConnection
  21.  
  22.     Dim oledbPerintah As OleDbCommand = New OleDbCommand
  23.  
  24.     Dim lokasiFile As String = "D:\fileAccess\"
  25.  
  26.     Dim namaFile As String = "Database1.accdb"
  27.  
  28.     Dim stringKoneksi As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & "Data Source=" & lokasiFile & namaFile & ";" & "Persist Security Info=False;"
  29.  
  30.     Dim adapter As New OleDbDataAdapter
  31.  
  32.  
  33.     Sub tampilIsiGrid()
  34.  
  35.         oledbKoneksi.ConnectionString = stringKoneksi
  36.  
  37.         oledbKoneksi.Open()
  38.  
  39.         oledbPerintah.Connection = oledbKoneksi
  40.  
  41.         oledbPerintah.CommandText = "Select * From databarang"
  42.  
  43.         adapter.SelectCommand = oledbPerintah
  44.  
  45.         Dim ds As DataSet
  46.  
  47.         ds = New DataSet
  48.  
  49.         adapter.Fill(ds)
  50.  
  51.         DataGridView1.DataSource = ds.Tables(0)
  52.  
  53.         oledbKoneksi.Close()
  54.  
  55.  
  56.     End Sub
  57.  
  58.  
  59.     Sub autoID()
  60.  
  61.         oledbKoneksi.ConnectionString = stringKoneksi
  62.  
  63.         oledbKoneksi.Open()
  64.  
  65.         oledbPerintah.Connection = oledbKoneksi
  66.  
  67.         oledbPerintah.CommandText = "SELECT * FROM databarang ORDER BY idbarang desc"
  68.  
  69.         Dim baca As OleDbDataReader = oledbPerintah.ExecuteReader
  70.  
  71.         baca.Read()
  72.  
  73.         If Not baca.HasRows Then
  74.  
  75.             TextBox1.Text = "DB" + "0001"
  76.  
  77.         Else
  78.  
  79.             TextBox1.Text = Val(Microsoft.VisualBasic.Mid(baca.Item("idbarang").ToString, 4, 3)) + 1
  80.  
  81.             If Len(TextBox1.Text) = 1 Then
  82.  
  83.                 TextBox1.Text = "DB000" & TextBox1.Text & ""
  84.  
  85.             ElseIf Len(TextBox1.Text) = 2 Then
  86.  
  87.                 TextBox1.Text = "DB00" & TextBox1.Text & ""
  88.  
  89.             ElseIf Len(TextBox1.Text) = 3 Then
  90.  
  91.                 TextBox1.Text = "DB0" & TextBox1.Text & ""
  92.  
  93.  
  94.  
  95.             End If
  96.  
  97.         End If
  98.  
  99.         oledbKoneksi.Close()
  100.  
  101.     End Sub
  102.  
  103.  
  104.     Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
  105.  
  106.         'Data Baru
  107.  
  108.         autoID()
  109.  
  110.         TextBox2.Text = ""
  111.  
  112.         TextBox3.Text = ""
  113.  
  114.         TextBox4.Text = ""
  115.  
  116.  
  117.  
  118.     End Sub
  119.  
  120.     Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
  121.  
  122.         'Simpan Data
  123.  
  124.         idbarang = TextBox1.Text
  125.  
  126.         namabarang = TextBox2.Text
  127.  
  128.         hargabarang = TextBox3.Text
  129.  
  130.         namaprodusen = TextBox4.Text
  131.  
  132.         oledbKoneksi.ConnectionString = stringKoneksi
  133.  
  134.         oledbKoneksi.Open()
  135.  
  136.         oledbPerintah.Connection = oledbKoneksi
  137.  
  138.         oledbPerintah.CommandText = "INSERT INTO databarang(idbarang,namabarang,hargabarang,namaprodusen) values" & "('" & idbarang & "', '" & namabarang & "', '" & hargabarang & "', '" & namaprodusen & "')"
  139.  
  140.         oledbPerintah.ExecuteNonQuery()
  141.  
  142.         oledbKoneksi.Close()
  143.  
  144.         TextBox2.Text = ""
  145.  
  146.         TextBox3.Text = ""
  147.  
  148.         TextBox4.Text = ""
  149.  
  150.         autoID()
  151.  
  152.         DataGridView1.Refresh()
  153.  
  154.         tampilIsiGrid()
  155.  
  156.         oledbKoneksi.Close()
  157.  
  158.  
  159.     End Sub
  160.  
  161.     Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load
  162.  
  163.         'Perintah pertama yg dijalankan ketika program di load
  164.  
  165.         autoID()
  166.  
  167.         tampilIsiGrid()
  168.  
  169.  
  170.     End Sub
  171.  
  172.     Private Sub DataGridView1_CellClick(sender As Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
  173.  
  174.         'Tampil data dari datagridview ke textbox
  175.  
  176.         Dim row As DataGridViewRow = DataGridView1.CurrentRow
  177.  
  178.         Me.TextBox1.Text = row.Cells(0).Value.ToString
  179.  
  180.         Me.TextBox2.Text = row.Cells(1).Value.ToString
  181.  
  182.         Me.TextBox3.Text = row.Cells(2).Value.ToString
  183.  
  184.         Me.TextBox4.Text = row.Cells(3).Value.ToString
  185.  
  186.     End Sub
  187.  
  188.     Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
  189.  
  190.         'Update Data
  191.  
  192.         idbarang = TextBox1.Text
  193.  
  194.         namabarang = TextBox2.Text
  195.  
  196.         hargabarang = TextBox3.Text
  197.  
  198.         namaprodusen = TextBox4.Text
  199.  
  200.         oledbKoneksi.ConnectionString = stringKoneksi
  201.  
  202.         oledbKoneksi.Open()
  203.  
  204.         oledbPerintah.Connection = oledbKoneksi
  205.  
  206.         oledbPerintah.CommandText = "UPDATE databarang SET namabarang = '" & namabarang & "', hargabarang = '" & hargabarang & "', namaprodusen = '" & namaprodusen & "' WHERE idbarang = '" & idbarang & "'"
  207.  
  208.         oledbPerintah.ExecuteNonQuery()
  209.  
  210.         oledbKoneksi.Close()
  211.  
  212.         TextBox2.Text = ""
  213.  
  214.         TextBox3.Text = ""
  215.  
  216.         TextBox4.Text = ""
  217.  
  218.         autoID()
  219.  
  220.         DataGridView1.Refresh()
  221.  
  222.         tampilIsiGrid()
  223.  
  224.         oledbKoneksi.Close()
  225.  
  226.  
  227.     End Sub
  228.  
  229.     Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click
  230.         'Hapus data
  231.  
  232.         idbarang = TextBox1.Text
  233.  
  234.         namabarang = TextBox2.Text
  235.  
  236.         hargabarang = TextBox3.Text
  237.  
  238.         namaprodusen = TextBox4.Text
  239.  
  240.         oledbKoneksi.ConnectionString = stringKoneksi
  241.  
  242.         oledbKoneksi.Open()
  243.  
  244.         oledbPerintah.Connection = oledbKoneksi
  245.  
  246.         oledbPerintah.CommandText = "DELETE * FROM databarang WHERE idbarang = '" & idbarang & "'"
  247.  
  248.         oledbPerintah.ExecuteNonQuery()
  249.  
  250.         oledbKoneksi.Close()
  251.  
  252.         TextBox2.Text = ""
  253.  
  254.         TextBox3.Text = ""
  255.  
  256.         TextBox4.Text = ""
  257.  
  258.         autoID()
  259.  
  260.         DataGridView1.Refresh()
  261.  
  262.         tampilIsiGrid()
  263.  
  264.         oledbKoneksi.Close()
  265.  
  266.     End Sub
  267.  
  268.     Private Sub Button5_Click(sender As System.Object, e As System.EventArgs) Handles Button5.Click
  269.  
  270.         'Menampilkan Report MS Access
  271.  
  272.         Dim MSA As New Access.Application
  273.  
  274.         MSA.Visible = True
  275.  
  276.         MSA.OpenCurrentDatabase("D:\fileAccess\Database1.accdb", False)
  277.  
  278.         MSA.Application.DoCmd.OpenReport("reportDataBarang", Access.AcView.acViewPreview)
  279.  
  280.     End Sub
  281. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement