Membuat program Import Data Excel ke MySQL dengan VB 6.0
Persiapan Awal
Dalam pembuatan aplikasi "Import data Excel ke MySQL dengan VB 6.0" sederhana di sini yang pertama kali harus kita siapkan adalah File Excel yang akan kita importkan datanya ke dalam Visual Basic 6. Untuk mengimportkan data Excel ke MySQL kita dapat menyiapkan file excel menggunakan Microsoft Excel 2000 (*.xls) atau menggunakan Microsoft Excel 2007 (*.xlsx).
- Download dan install komponen VB MySQL Direct V.1 di sini
Contoh data Excel yang dibuat dengan Microsoft Excel 2007 yang akan diimportkan ke dalam database MySQL.
Setelah data excel sudah selesai Anda buat seperti contoh data di atas, berikutnya adalah membuat database dan tabel yang akan digunakan untuk menampung data dari Excel yang akan diimportkan datanya ke Database.
Membuat database dan tabel MySQL
Buat database menggunakan PHPMyadmin dengan nama "DB_IMPORT_EXPORT" dan buat buat tabelnya dengan nama "tb_mahasiswa" dengan struktur tabel seperti berikut:
Membuat Project
Buat project baru, pilih "VB Enterprise Edition Control" .
Buat desain Form seperti gambar berikut:
Aturlah desain form di atas dengan pengaturan properties sebagai berikut:
Objek
Properties
Nilai
Properties
Form
Name
Border Style
Caption
Font
Form1
1 – Fixed Single
SIMPAN, EDIT dan HAPUS Data
Tahoma, Size 9
MSFlexGrid
Name
BackColor
Cols
MSFlexGrid1
&H00C0FFC0&
2
TextBox
Name
Text
TxtNamaFile
(dikosongkan)
Command1
Name
Caption
CmdBuka
&Open File
Command2
Name
Caption
CmdImport
&Import
Command3
Name
Caption
CmdClose
&Close
CommonDialog
Name
CommonDialog1
Objek
|
Properties
|
Nilai
Properties
|
Form
|
Name
Border Style
Caption
Font
|
Form1
1 – Fixed Single
SIMPAN, EDIT dan HAPUS Data
Tahoma, Size 9
|
MSFlexGrid
|
Name
BackColor
Cols
|
MSFlexGrid1
&H00C0FFC0&
2
|
TextBox
|
Name
Text
|
TxtNamaFile
(dikosongkan)
|
Command1
|
Name
Caption
|
CmdBuka
&Open File
|
Command2
|
Name
Caption
|
CmdImport
&Import
|
Command3
|
Name
Caption
|
CmdClose
&Close
|
CommonDialog
|
Name
|
CommonDialog1
|
Buat kode program di atas seperti berikut:
Dim rsExcel As ADODB.Recordset
Dim strSql As String
Dim Baris As Long
Dim SQL As String
'Untuk Mengatur Tampilan MSFlexGrid1
Sub AktifMSFlexGrid1()
MSFlexGrid1.Cols = 5
MSFlexGrid1.RowHeightMin = 300
'-------------------------------------------------
MSFlexGrid1.Col = 0
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NO"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(0) = 500
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 1
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NIM"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(1) = 900
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 2
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NAMA MAHASISWA"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(2) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 3
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "ALAMAT"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(3) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'---------------------------------------------------
MSFlexGrid1.Col = 4
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "JURUSAN"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(4) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
End Sub
Private Sub CmdBuka_Click()
MSFlexGrid1.Clear
Call AktifMSFlexGrid1
Baris = 0
'Memilih File Excel
With CommonDialog1
.DialogTitle = "Pilih File Excelnya (.xlsx)"
.InitDir = App.Path
.Filter = "SQL Files (*.xlsx)|*.xlsx"
'jika menggunakan file excel 2007 keatas
'untuk .Filter = "SQL Files (*.xls)|*.xls" '
'Ganti dengan .Filter = "SQL Files (*.xlsx)|*.xlsx"
.ShowOpen
End With
'menampilkan nama filenya di textbox
TxtNamaFile.Text = CommonDialog1.FileName
'Membuka File Excel
If openExcelFile(CommonDialog1.FileName) Then
'selanjutnya data yg diambil ada di sheet1,
'sheet disini sama seperti tabel yang ada di database
strSql = "SELECT * FROM [Sheet1$]" ' penting !!!, jangan lupa menambahkan karakter $
Set rsExcel = New ADODB.Recordset
rsExcel.Open strSql, conXls, adOpenForwardOnly, adLockReadOnly
'tampilkan data yg ada sheet1 ke MSFlexGrid1
If Not rsExcel.EOF Then
Do While Not rsExcel.EOF
Baris = Baris + 1
MSFlexGrid1.Rows = Baris + 1
MSFlexGrid1.TextMatrix(Baris, 0) = Baris
MSFlexGrid1.TextMatrix(Baris, 1) = rsExcel(0).Value
MSFlexGrid1.TextMatrix(Baris, 2) = rsExcel(1).Value
MSFlexGrid1.TextMatrix(Baris, 3) = rsExcel(2).Value
MSFlexGrid1.TextMatrix(Baris, 4) = rsExcel(3).Value
rsExcel.MoveNext
DoEvents
Loop
End If
rsExcel.Close
Set rsExcel = Nothing
End If
End Sub
Private Sub CmdClose_Click()
Unload Me
End Sub
Private Sub CmdImport_Click()
'On Error GoTo AdaError
Dim i As Integer
Call KonekDb
For i = 1 To MSFlexGrid1.Rows - 1
SQL = ""
SQL = "INSERT INTO Tb_Mahasiswa(Nim,Nama,alamat,jurusan) " _
& "VALUES ('" & MSFlexGrid1.TextMatrix(i, 1) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 2) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 3) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 4) & "')"
Conn.Execute (SQL)
DoEvents
Next i
MsgBox "Import data berhasil, Silahkan di cek...", vbInformation, ".:: Sukses..."
Exit Sub
AdaError:
If Err.Number = -2147467259 Then
MsgBox "NIM " & MSFlexGrid1.TextMatrix(i, 1) & " sudah ada dalam database..." & vbCrLf & _
"Pada file excelnya di baris " & i + 1 & " , silahkan hapus terlebih dahulu lalu ulangi..", vbCritical, ".:: Gagal...!!!"
Exit Sub
Else
MsgBox "Error No : " & Err.Number & vbCrLf & _
Err.Description, vbCritical + vbOKOnly, "Error......"
End If
End Sub
Private Sub Form_Load()
Call AktifMSFlexGrid1
End Sub
Dim strSql As String
Dim Baris As Long
Dim SQL As String
'Untuk Mengatur Tampilan MSFlexGrid1
Sub AktifMSFlexGrid1()
MSFlexGrid1.Cols = 5
MSFlexGrid1.RowHeightMin = 300
'-------------------------------------------------
MSFlexGrid1.Col = 0
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NO"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(0) = 500
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 1
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NIM"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(1) = 900
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 2
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "NAMA MAHASISWA"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(2) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'-------------------------------------------------
MSFlexGrid1.Col = 3
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "ALAMAT"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(3) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
'---------------------------------------------------
MSFlexGrid1.Col = 4
MSFlexGrid1.Row = 0
MSFlexGrid1.Text = "JURUSAN"
MSFlexGrid1.CellFontBold = True
MSFlexGrid1.ColWidth(4) = 2000
MSFlexGrid1.AllowUserResizing = flexResizeColumns
MSFlexGrid1.CellAlignment = flexAlignCenterCenter
End Sub
Private Sub CmdBuka_Click()
MSFlexGrid1.Clear
Call AktifMSFlexGrid1
Baris = 0
'Memilih File Excel
With CommonDialog1
.DialogTitle = "Pilih File Excelnya (.xlsx)"
.InitDir = App.Path
.Filter = "SQL Files (*.xlsx)|*.xlsx"
'jika menggunakan file excel 2007 keatas
'untuk .Filter = "SQL Files (*.xls)|*.xls" '
'Ganti dengan .Filter = "SQL Files (*.xlsx)|*.xlsx"
.ShowOpen
End With
'menampilkan nama filenya di textbox
TxtNamaFile.Text = CommonDialog1.FileName
'Membuka File Excel
If openExcelFile(CommonDialog1.FileName) Then
'selanjutnya data yg diambil ada di sheet1,
'sheet disini sama seperti tabel yang ada di database
strSql = "SELECT * FROM [Sheet1$]" ' penting !!!, jangan lupa menambahkan karakter $
Set rsExcel = New ADODB.Recordset
rsExcel.Open strSql, conXls, adOpenForwardOnly, adLockReadOnly
'tampilkan data yg ada sheet1 ke MSFlexGrid1
If Not rsExcel.EOF Then
Do While Not rsExcel.EOF
Baris = Baris + 1
MSFlexGrid1.Rows = Baris + 1
MSFlexGrid1.TextMatrix(Baris, 0) = Baris
MSFlexGrid1.TextMatrix(Baris, 1) = rsExcel(0).Value
MSFlexGrid1.TextMatrix(Baris, 2) = rsExcel(1).Value
MSFlexGrid1.TextMatrix(Baris, 3) = rsExcel(2).Value
MSFlexGrid1.TextMatrix(Baris, 4) = rsExcel(3).Value
rsExcel.MoveNext
DoEvents
Loop
End If
rsExcel.Close
Set rsExcel = Nothing
End If
End Sub
Private Sub CmdClose_Click()
Unload Me
End Sub
Private Sub CmdImport_Click()
'On Error GoTo AdaError
Dim i As Integer
Call KonekDb
For i = 1 To MSFlexGrid1.Rows - 1
SQL = ""
SQL = "INSERT INTO Tb_Mahasiswa(Nim,Nama,alamat,jurusan) " _
& "VALUES ('" & MSFlexGrid1.TextMatrix(i, 1) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 2) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 3) & "'," _
& "'" & MSFlexGrid1.TextMatrix(i, 4) & "')"
Conn.Execute (SQL)
DoEvents
Next i
MsgBox "Import data berhasil, Silahkan di cek...", vbInformation, ".:: Sukses..."
Exit Sub
AdaError:
If Err.Number = -2147467259 Then
MsgBox "NIM " & MSFlexGrid1.TextMatrix(i, 1) & " sudah ada dalam database..." & vbCrLf & _
"Pada file excelnya di baris " & i + 1 & " , silahkan hapus terlebih dahulu lalu ulangi..", vbCritical, ".:: Gagal...!!!"
Exit Sub
Else
MsgBox "Error No : " & Err.Number & vbCrLf & _
Err.Description, vbCritical + vbOKOnly, "Error......"
End If
End Sub
Private Sub Form_Load()
Call AktifMSFlexGrid1
End Sub
Setelah selesai menuliskan kode program di atas, berikutnya tambahkan MODULE untuk membuat PROCEDURE Koneksi ke dalam database MySQL dan PROCEDURE FUNCTION untuk membuka file Excel 2000 atau 2007.
Berikut ini adalah kode program untuk MODULE.
Public conXls As ADODB.Connection
Public Conn As New MYSQL_CONNECTION
'Function Ini di gunakan untuk koneksi ke file excel
Public Function openExcelFile(ByVal excelFile As String) As Boolean
On Error GoTo errHandle
'-----------------------
'Jika menggunakan Office 2007 ke atas ganti Provider=Microsoft.Jet.OLEDB.4.0;
'menjadi Provider=Microsoft.ACE.OLEDB.12.0;
Set conXls = New ADODB.Connection
conXls.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & Replace(excelFile, Chr$(0), "") & ";" _
& "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
conXls.Open
'-------------------
openExcelFile = True
Exit Function
errHandle:
openExcelFile = False
End Function
'Untuk koneksi database ms access pd waktu memasukan data ke dlm database
Public Sub KonekDb()
If Conn.State = adStateOpen Then
Conn.CloseConnection
Set Conn = New MYSQL_CONNECTION
Conn.OpenConnection "127.0.0.1", "root", "", "db_import_export", 3306
Else
Conn.OpenConnection "127.0.0.1", "root", "", "db_import_export", 3306
End If
Conn.CursorLocation = adUseClient
End Sub
Public Conn As New MYSQL_CONNECTION
'Function Ini di gunakan untuk koneksi ke file excel
Public Function openExcelFile(ByVal excelFile As String) As Boolean
On Error GoTo errHandle
'-----------------------
'Jika menggunakan Office 2007 ke atas ganti Provider=Microsoft.Jet.OLEDB.4.0;
'menjadi Provider=Microsoft.ACE.OLEDB.12.0;
Set conXls = New ADODB.Connection
conXls.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & Replace(excelFile, Chr$(0), "") & ";" _
& "Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1"""
conXls.Open
'-------------------
openExcelFile = True
Exit Function
errHandle:
openExcelFile = False
End Function
'Untuk koneksi database ms access pd waktu memasukan data ke dlm database
Public Sub KonekDb()
If Conn.State = adStateOpen Then
Conn.CloseConnection
Set Conn = New MYSQL_CONNECTION
Conn.OpenConnection "127.0.0.1", "root", "", "db_import_export", 3306
Else
Conn.OpenConnection "127.0.0.1", "root", "", "db_import_export", 3306
End If
Conn.CursorLocation = adUseClient
End Sub
No comments:
Post a Comment