Langkah pertama
membuat proses export dan import data excel adalah membuat database dengan
access 2007 dan beri nama DBExport.accdb. kemudian buatlah tabel dengan nama
TBLBarang (kode_barang dan Nama_barang). Setelah itu form dengan bentuk seperti
gambar di bawah ini.
Form
export import
Coding :
Imports System.Data.OleDb
Public Class
ExportImport
'definisikan
variabel koneksi di database access 2007
Dim ConnAccess As
OleDbConnection
Dim CMDAccess As
OleDbCommand
Dim DRAccess As
OleDbDataReader
Private Sub
Button1_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
Button1.Click
Call
Koneksi() 'koneksi ke excel pertama kali sudah dibuat
di module
DA = New
OleDbDataAdapter("select * from [sheet1$]",
CONN)
DS = New
DataSet
DA.Fill(DS)
DGV.DataSource = DS.Tables(0)
DGV.Columns(1).Width = 200
DGV.ReadOnly = True
CONN.Close()
End Sub
Sub
Koneksi1()
'string
koneksi ke daatabase access 2007
ConnAccess = New
OleDbConnection("provider=Microsoft.ace.OLEDB.12.0;data
source=dbimport.accdb")
ConnAccess.Open()
End Sub
Private Sub
Button2_Click(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles
Button2.Click
Call
Koneksi1()
'cari data di
tabel barang
CMDAccess = New
OleDbCommand("select * from tblbarang",
ConnAccess)
DRAccess = CMDAccess.ExecuteReader
DRAccess.Read()
If Not DRAccess.HasRows Then
'jika datanya
tidak ada maka simpan secara berulang
For
baris As Integer
= 0 To DGV.RowCount - 2
Dim
simpan As String
= "insert into tblbarang values('"
& DGV.Rows(baris).Cells(0).Value & "','"
& DGV.Rows(baris).Cells(1).Value & "')"
CMDAccess = New OleDbCommand(simpan, ConnAccess)
CMDAccess.ExecuteNonQuery()
Next
MsgBox("proses
simpan berhasil")
Else
'jika
datanya sudah ada maka edit secara berulang
For
baris As Integer
= 0 To DGV.RowCount - 2
Dim
edit As String
= "update tblbarang set nama_barang='"
& DGV.Rows(baris).Cells(1).Value & "'
where kode_barang='" & DGV.Rows(baris).Cells(0).Value & "'"
CMDAccess = New OleDbCommand(edit, ConnAccess)
CMDAccess.ExecuteNonQuery()
Next
MsgBox("proses
edit berhasil")
End If
End Sub
End Class
1 comments so far
terima kasih banyak. sangat membantu
EmoticonEmoticon