Sunday, May 22, 2011

Menambah Module Yang Disertai Kode Melalui VB6 Add-Ins

Menjelaskan mengenai cara menambah module melalui Visual Basic 6 Add-Ins - Sebelumnya saya telah memposting mengenai cara menambah Form, Menu, CommandButton, menambah referensi komponen OCX dan DLL melalui pemrograman Add-Ins, maka untuk melengkapi mengenai cara membuat robot software atau generator code tentulah harus dapat menambahkan Module, Class, UserControl, Resource, dsb.

Untuk menjalankan kode di bawah, ikuti langkah-langkah berikut:
  1. Buat project Add-Ins baru dengan cara Klik File, klik New Project, klik Addin
  2. Gantilah seluruh kode yang terdapat pada frmAddin dengan kode di bawah
  3. Lakukan compile dengan cara klik File, klik Make MyAddin.dll
  4. 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.