Export Database Ke Excel

  1. Tambah References, dengan komponen Excel ( Klik “Project”, “References”,”Microsoft Axcel 12.0 Object Library”)
  2. Ketik / Copy source code berikut pada form :

Public Adoconn As New ADODB.Connection

Public RsInventaris As New ADODB.Recordset

Private Sub Export_Click()
Dim aa As New ExportExcel
aa.RecordSource = RsInventaris
aa.ExportToExcel
End Sub

Private Sub Form_Load()
Adoconn.CursorLocation = adUseClient
Adoconn.Open “DSN=MyAkses4”
RsInventaris.Open “SELECT * FROM `inventaris`”, Adoconn, adOpenDynamic, adLockPessimistic
RsKondisi.Open “SELECT * FROM `kondisi`”, Adoconn, adOpenDynamic, adLockPessimistic
End Sub

3. Add form “Clas Module”, dan ketik / copy source code berikut :

Dim rs As New ADODB.Recordset

Public Property Let RecordSource(m_rs As Recordset)
Set rs = m_rs
End Property

Public Sub ExportToExcel()
Dim App As Application
Dim Wb As Workbook
Dim Wk As Worksheet
Dim Baris As Long, Kolom As Integer
Set App = Excel.Application
Set Wb = App.Workbooks.Add
Set Wk = Wb.Worksheets(1)

With rs
.MoveFirst
Baris = 1
Kolom = 0
For Kolom = 1 To rs.Fields.Count
Wk.Cells(1, Kolom) = rs.Fields(Kolom – 1).Name
Next Kolom
While Not .EOF
Baris = Baris + 1
For Kolom = 1 To rs.Fields.Count
Wk.Cells(Baris, Kolom) = rs.Fields(Kolom – 1)
Next Kolom
.MoveNext
Wend
End With

App.Visible = True

Set Wk = Nothing
Set Wb = Nothing
Set App = Nothing
End Sub

“Selamat Mencoba”

2 Balasan ke Export Database Ke Excel

  1. cakbudi mengatakan:

    Microsoft Axcel 12.0 Object Library
    kok gak ada ya…ūüôā

  2. astry mengatakan:

    Quote : cakbudi

    Klo blum ada..
    1. Klik Menu Project, Pilih Preferences, Klik Browse, Cari directory Ms Office,
    2. Pada File of type, Pilih All Files (*.*);
    3. Pilih “EXCEL.EXE”, Klik “Open”.
    4. Udah ada kan di References project???

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: