Memindahkan Data Dari Database ke File HTML

Memindahkan data yang terdapat di dalam database ke file HTML
Persiapan: 1. Buat 1 Project baru dengan 1 Form, 2 TextBox, dan 1  Commandbutton.
2. Tambahkan reference Microsoft DAO 3.5 Object Library.
3. Copy-kan coding berikut ke dalam editor form yang bertalian.

Option Explicit ‘Setiap variabel yang digunakan harus dideklarasikan!
Private Sub Command1_Click()
Dim fnum As Integer
Dim db As Database
Dim rs As Recordset
Dim num_fields As Integer
Dim i As Integer
Dim num_processed As Integer
On Error GoTo MiscError
‘Buka output file.
fnum = FreeFile
Open Text2.Text For Output As fnum
‘Tuliskan informasi header HTML
Print #fnum, “<HTML>”
Print #fnum, “<HEAD>”
‘Ganti “Ini Judul Atas” dengan judul file HTML yang
‘Anda inginkan tampil di bagian atas bar jendela
Print #fnum, “<TITLE>Ini Judul Atas</TITLE>”
Print #fnum, “</HEAD>”
Print #fnum, “”
Print #fnum, “<BODY TEXT=#000000 BGCOLOR=white>”
‘Ganti “Judul Tabel” dengan judul yang Anda inginkan
‘tampil di bagian atas halaman html tersebut
Print #fnum, “<H1>Judul Tabel</H1>”
‘Mulai buat tabel HTML
Print #fnum, “<TABLE WIDTH=100% CELLPADDING=2 CELLSPACING=2 BGCOLOR=#00C0FF BORDER=1>”
‘Buka database.
Set db = OpenDatabase(Text1.Text)
‘Buka tabel di database.
‘Ganti “t_mhs” dengan nama tabel di database Anda
‘dan “NIM” dengan nama field yang Anda inginkan disortir
‘Jika Anda tidak ingin menyortir tabel, Anda dapat
‘menghilangkan “ORDER BY NIM”
Set rs = db.OpenRecordset(“SELECT * FROM t_mhs ORDER BY NIM”)
‘Gunakan nama field sebagai judul setiap field/kolom
Print #fnum, ” <TR>” ‘ Mulai sebuah baris…
num_fields = rs.Fields.Count
For i = 0 To num_fields – 1
Print #fnum, ” <TH>”;
Print #fnum, rs.Fields(i).Name;
Print #fnum, “</TH>”
Next i
Print #fnum, ” </TR>”

‘Proses semua record…
Do While Not rs.EOF
num_processed = num_processed + 1
‘Mulai dengan sebuah baris baru untuk record ini
Print #fnum, ” <TR>”;
For i = 0 To num_fields – 1
Print #fnum, ” <TD>”;
Print #fnum, rs.Fields(i).Value;
Print #fnum, “</TD>”
Next i
Print #fnum, “</TR>”;
rs.MoveNext ‘Maju ke record berikutnya
Loop
‘Akhir tabel di file HTML
Print #fnum, “</TABLE>”
Print #fnum, “<P>”
Print #fnum, “<H3>” & _
Format$(num_processed) & _
” records ditampilkan…</H3>”
Print #fnum, “</BODY>”
Print #fnum, “</HTML>”
rs.Close ‘Tutup tabel di database
db.Close ‘Tutup database
Close fnum ‘Tutup file teks, lalu tampilkan pesan
MsgBox “Berhasil memproses ” _
& Format$(num_processed) & ” records.”, _
vbInformation, “Sukses”

Exit Sub
MiscError: ‘Jika terjadi error, tampilkan pesan nomor
‘dan deskripsi errornya…
MsgBox “Error ” & Err.Number & _
vbCrLf & Err.Description
End Sub

Private Sub Form_Load()
‘Tempatkan nama file database Anda di Text1
‘dan nama file HTML yang akan dibuat di Text2
Text1.Text = “C:\My Documents\mahasiswa.mdb”
Text2.Text = “C:\My Documents\DataHTML.html”
End Sub

oleh: Masino Sinaga (masino_sinaga@posindonesia.co.id)

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: