Untuk menjalankan kode di bawah, ikuti langkah-langkah berikut:
- Buat project Add-Ins baru dengan cara Klik File, klik New Project, klik Addin
- Gantilah seluruh kode yang terdapat pada frmAddin dengan kode di bawah
- Lakukan compile dengan cara klik File, klik Make MyAddin.dll
- Simpan project, dan tutuplah aplikasi Visual Basic 6
Public VBInstance As VBIDE.VBE
Public Connect As Connect
Option Explicit
Private Sub CancelButton_Click()
Connect.Hide
End Sub
Private Sub OKButton_Click()
AddModule "moDatabase", ConnectionCode
End Sub
Public Function AddModule(ModulName As String, Optional strCode As String) As Boolean
Dim newModule As VBComponent
On Error GoTo ErrHandler
Set newModule = VBInstance.ActiveVBProject.VBComponents.Add(vbext_ct_StdModule)
With newModule
.Name = ModulName
.CodeModule.AddFromString strCode
End With
Exit Function
ErrHandler:
MsgBox Err.Description
End Function
Private Function ConnectionCode() As String
Dim sMsg As String
sMsg = sMsg & "" & vbCrLf
sMsg = sMsg & "Public conn As ADODB.Connection" & vbCrLf
sMsg = sMsg & "" & vbCrLf
sMsg = sMsg & "Public Function OpenDatabase(Filename As String) As Boolean" & vbCrLf
sMsg = sMsg & "" & vbCrLf
sMsg = sMsg & " Dim c As String" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & " On Error GoTo ErrHandler" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & " c = " & Chr(34) & "Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Chr(34) & " & Filename" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & " Set conn = New ADODB.Connection" & vbCrLf
sMsg = sMsg & " conn.ConnectionString = c" & vbCrLf
sMsg = sMsg & " conn.Open" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & " OpenDatabase = True" & vbCrLf
sMsg = sMsg & " Exit Function" & vbCrLf
sMsg = sMsg & "" & vbCrLf
sMsg = sMsg & "ErrHandler:" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & " OpenDatabase = False" & vbCrLf
sMsg = sMsg & " " & vbCrLf
sMsg = sMsg & "End Function" & vbCrLf
ConnectionCode = sMsg
End Function
Untuk mengakses MyAddin.dll, buka project baru klik Add-in Addin Manager.... klik My Add-in, selanjutnya tekanlah tombol OK dan lihatlah hasilnya.