Wednesday, June 22, 2011

Membaca Struktur Table dan Menerapkannya Dalam GUI

Setelah bepanjang lebar membicarakan Addins, maka sekarang kita akan menerapkannya dalam sebuah project yang bermanfaat (jika dilanjutkan memprogramnya) yaitu: generator database yang sangat sederhana (hanya untuk MS Access saja, untuk database lainnya, silakan modifikasi kodenya). Project ini hanya akan mengenerate GUI-nya saja (tanpa kode), adapun jika jika ingin menambahkan kode-kode yang sesuai Anda dapat merujuk pada tautan di samping, menambah kode pada module (tentu saja setelah dimodifikasi dan disesuaikan).

Langkah-Langkah:
  • Buat project Addin
  • Masukan kode-kode di bawah ini:
Kode pada frmAddin:
Public VBInstance As VBIDE.VBE 
Public Connect As Connect

Option Explicit

Dim
strCon As String

Private Sub
cboTables_Click()
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = DBase
Dim c As ADOX.Column
lstAll.Clear
For Each c In cat.Tables(GetTabelValidName(cboTables.Text)).Columns
lstAll.AddItem c.Name
Next
If
lstAll.ListCount > 0 Then lstAll.ListIndex = 0
Set cat = Nothing
End Sub

Private Function
GetTabelValidName(strName As String) As String
Dim
s() As String
s =
Split(strName, " : ")
GetTabelValidName = s(1)
End Function

Private Sub
cmdConnect_Click()
On Error GoTo ErrHandler
strCon = getADOConnectionString()
If strCon = "" Then Exit Sub
txtCon.Text = strCon
If OpenDataBase(strCon) = True Then
FillComboWithTables
End If
Exit Sub
ErrHandler:
MsgBox Err.Number & vbNewLine & Err.Description, vbExclamation + vbOKOnly, "Connection Error"
lstAll.Clear
cboTables.Clear
End Sub

Private Function
FillComboWithTables()
Set cat = New ADOX.Catalog
Set cat.ActiveConnection = DBase
Dim i As Integer
cboTables.Clear
lstAll.Clear
For i = 0 To cat.Tables.Count - 1
If cat.Tables(i).Type <> "SYSTEM TABLE" And cat.Tables(i).Type <> "ACCESS TABLE" Then
If
cat.Tables(i).Type = "TABLE" Then
cboTables.AddItem "Table : " & cat.Tables(i).Name
Else
cboTables.AddItem "query : " & cat.Tables(i).Name
End If
End If
Next i
Set
cat = Nothing
End Function

Private Sub
Command1_Click()
AddFormAndControls Replace(GetTabelValidName(cboTables.Text), " ", "_"), GetTabelValidName(cboTables.Text)
End Sub

Private Function
AddFormAndControls(f As String, c As String)

On Error Resume Next

Dim
frm As VBIDE.VBComponent
Dim ctl As VBControl
Dim frmCurrent As VBForm
Dim i As Integer, x As Integer, y As Integer, k As Integer
Set
frm = VBInstance.ActiveVBProject.VBComponents.Add(vbext_ct_VBForm)
Set frmCurrent = VBInstance.SelectedVBComponent.Designer

For i =
0 To lstAll.ListCount - 1
'kode di bawah ini untuk menambah TextBox
Set ctl = frmCurrent.VBControls.Add("VB.TextBox")
With ctl
.Properties("Name") = "txt" & Replace(lstAll.List(i), " ", "_")
If i = 0 Then
.Properties("Top") = 500
x = 500
Else
x = x +
400 'spasi (jarak) untuk TextBox
End If
.Properties("Top") = x
.Properties("Left") = 2500
.Properties("Width") = 4000
.Properties("Height") = 330
.Properties("Text") = lstAll.List(i)
'.properties dan lain-lain, disesuaikan kebutuhan
End With
If i =
lstAll.ListCount - 1 Then
y = x +
2000 'Form Height
k = x + 900 'CommandButton Top
End If
'kode di bawah ini untuk menambah label
Set ctl = frmCurrent.VBControls.Add("VB.Label")
With ctl
.Properties("Name") = lstAll.List(i)
.Properties("Top") = x
.Properties("Left") = 465
.Properties("Width") = 2000
.Properties("Height") = 255
.Properties("Caption") = lstAll.List(i)
.Properties("BackStyle") = 0 'transparent
'.properties dan lain-lain, disesuaikan kebutuhan
End With
Next
Set
ctl = frmCurrent.VBControls.Add("VB.CommandButton")
With ctl
.Properties("Name") = "cmdUpdate"
.Properties("Top") = k
.Properties("Left") = 5040
.Properties("Width") = 1455
.Properties("Height") = 375
.Properties("Caption") = "&Update"
'.properties dan lain-lain, disesuaikan kebutuhan
End With
Set
ctl = frmCurrent.VBControls.Add("VB.CommandButton")
With ctl
.Properties("Name") = "cmdCancel"
.Properties("Top") = k
.Properties("Left") = 3480
.Properties("Width") = 1455
.Properties("Height") = 375
.Properties("Caption") = "&Cancel"
'.properties dan lain-lain, disesuaikan kebutuhan
End With
InsertOCX "{2CDCDF4C-4914-4DBC-99CB-12359BE472E1}"
Set ctl = frmCurrent.VBControls.Add("Liner.cLiner")
With ctl
.Properties("Top") = k - 300
.Properties("Left") = -5
.Properties("Width") = 7000
.Properties("Height") = 30
End With
With
frm
.Properties("Name") = "frm" & f
.Properties("Width") = 7155
.Properties("Caption") = c
.Properties("Height") = y
End With
'--------------------------------------------------------------------------------------------------
'tambahkan kontrol lain-lain ThirdParty OCX
'tambahkan pula kode-kode yang sesuai
'maaf, belum dibuatkan....
'--------------------------------------------------------------------------------------------------
End Function

Public Function
InsertOCX(ProgID As String) As Boolean
On Error GoTo
ErrHandler
'Add OCX
VBInstance.ActiveVBProject.AddToolboxProgID ProgID
InsertOCX = True
Exit Function
ErrHandler:
InsertOCX = False
End Function

Kode pada modDatabase:
Option Explicit 

Public
DBase As New ADODB.Connection
Public cat As ADOX.Catalog

Function
OpenDataBase(sFilename As String) As Boolean
'// Membuat koneksi ke database
Set DBase = New ADODB.Connection
With DBase
.CursorLocation = adUseClient
.Open "Provider= Microsoft.Jet.OLEDB.4.0;Persist security info=False;Data Source=" & sFilename & ";Jet OLEDB:Database;"
End With
OpenDataBase = True
End Function
Kode pada modDataLinks: silakan copy dan pastekan dari tautan di samping module data links.

READ MORE - Membaca Struktur Table dan Menerapkannya Dalam GUI

Merubah Nama Menu Add-Ins Yang Akan Ditampilkan - VB6 Add-In

Untuk merubah caption sub menu (Addin yang dibuat sendiri) yang ditampilkan di bawah menu Add-Ins, maka ikutilah langkah-langkah berikut:
  • Buatlah project Add-Ins dengan cara klik menu File >> New Project >> pada kotak dialog New Project pilihlah template/project Addin
  • Secara default akan terbentuk satu project dengan dua component, masing-masing frmAddIn dan Connect
  • Klik kanan Connect, dan pilihlah menu View Code
  • Carilah di bawah ini (yang terdapat pada Connect):

    If ConnectMode = ext_cm_External Then 
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set
mcbMenuCommandBar = AddToAddInCommandBar("My AddIn")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If
  • Gantilah menu My Addin di atas dengan nama menu yang Anda Inginkan.
  • Selanjutnya Compile dan lakukan register jika perlu.
READ MORE - Merubah Nama Menu Add-Ins Yang Akan Ditampilkan - VB6 Add-In

Sedikit Tentang Code Module dan Object Member - VB6 Add-Ins

Dalam sebuah code module (editor tempat kita menulis kode di VB6), maka berapapun banyaknya kode yang kita tulis (puluhan baris sampai puluhan ribu baris), kode tersebut dapat diklasifikasikan menjadi lima unsur saja, Adapun yang kelima unsur tersebut, diantaranya:
  1. Variable
  2. Constanta
  3. Method (Sub, Function, Event Procedure)
  4. Events
  5. dan terakhir Property.
Dari kelima di atas, maka semuanya memiliki scope (jangkauan akses), adapun scope hanya ada 3 saja (berapapun banyaknya kode yang kita tulis), berturut-turut:
  1. Private
  2. Public
  3. Friend
Jadi, jika bukan private maka public atau friend, jika bukan public maka private atau friend, dst. Untuk membuktikannya, buatlah project Add-ins, tambahkan satu ListBox (dengan nama default saja List1)
Public VBInstance As VBIDE.VBE 
Public Connect As Connect

Option Explicit

Private Sub
OKButton_Click()
EnumAllMembers
End Sub

Private Function
EnumAllMembers()
Dim m As Member
Dim t As String
List1.Clear
For Each m In VBInstance.SelectedVBComponent.CodeModule.Members
Select Case m.Type
Case vbext_MemberType.vbext_mt_Const
t = "Constanta"
Case vbext_MemberType.vbext_mt_Event
t = "Event"
Case vbext_MemberType.vbext_mt_Method
t = "Method"
Case vbext_MemberType.vbext_mt_Property
t = "Property"
Case vbext_MemberType.vbext_mt_Variable
t = "Variable"
End Select
List1.AddItem "Nama: " & m.Name & vbTab & vbTab & "Type: " & t & vbTab & vbTab & "Scope: " & m.Scope
Next
End Function
Walaupun kode di atas tampak sederhana, akan tetapi ia akan sangat berguna, kira-kira untuk apa?
READ MORE - Sedikit Tentang Code Module dan Object Member - VB6 Add-Ins

Sunday, May 22, 2011

Manifest Injector - 1 kali Klik Untuk XP Style - Tools VB6

Setelah kita memahami cara menambah module, menambahkan kode, mengubah Startup Object, maka sekarang kita akan membuat sebuah tools sederhana yang bermanfaat dan akan banyak digunakan yaitu Manifest Injector sebuah generator kode sederhana.
Langkah-langkah:
  • Buat project Add-Ins
  • Hapus frmAddIns (form default saat membuat project Addins)
  • Tambahkan kode-kode di bawah ini:
-modAddIns
Public VBInstance As VBIDE.VBE
Public Connect As Connect

Option Explicit

Public Sub OKButton_Click()
AddResource 'and sure, some code too..
End Sub

Private Function AddResource() As Boolean

Dim newResourcee As VBComponent
Dim PathName As String

PathName = GetName(VBInstance.ActiveVBProject.Filename, PathNameOnly)
On Error GoTo ErrHandler
If Not IsProjectSaved Then
MsgBox "Simpan terlebih dahulu projectnya!", vbInformation, "Project belum disimpan"
Exit Function
End If
FileCopy App.Path & "\XP.manifest.res", PathName & "\XP.manifest.res"
Set newResourcee = VBInstance.ActiveVBProject.VBComponents.AddFile(PathName & "\XP.manifest.res")
InsertXPCode
AddModule "modManifestRes", "Public Declare Sub InitCommonControls Lib " & Chr(34) & "comctl32.dll" & Chr(34) & "()"
Exit Function

ErrHandler:

MsgBox Err.Description, vbCritical, "Warning"

End Function

Private Function IsProjectSaved() As Boolean
IsProjectSaved = Not (VBInstance.ActiveVBProject.Filename = "")
End Function

Private Sub InsertXPCode()

Dim CD As CodePane
Dim CM As CodeModule
Dim strName As String
strName = GetStartUpName
If GetStartUpName <> "Sub Main" Then
Set CD = VBInstance.ActiveVBProject.VBComponents(strName).CodeModule.CodePane
Set CM = VBInstance.ActiveVBProject.VBComponents(strName).CodeModule

Dim frm As VBIDE.VBForm

With VBInstance
With .ActiveVBProject.VBComponents(strName)

Set frm = .Designer
If .Type = vbext_ct_VBMDIForm Then
If Not IsExistProc("MDIForm_Initialize", CD) Then
MakeProcedure "Initialize", "MDIForm", CM
End If
CM.InsertLines CD.CodeModule.ProcBodyLine("MDIForm_Initialize", vbext_pk_Proc) + 1, " InitCommonControls"
ElseIf .Type = vbext_ct_VBForm Then
If Not IsExistProc("Form_Initialize", CD) Then
MakeProcedure "Initialize", "Form", CM
End If
CM.InsertLines CD.CodeModule.ProcBodyLine("Form_Initialize", vbext_pk_Proc) + 1, " InitCommonControls"
End If
End With
End With
Else
Set CD = VBInstance.ActiveVBProject.VBComponents(GetSubMain).CodeModule.CodePane
Set CM = VBInstance.ActiveVBProject.VBComponents(GetSubMain).CodeModule
CM.ReplaceLine CM.Members("main").CodeLocation, "Sub Main()" & vbCrLf & " InitCommonControls"
End If

End Sub

Private Function IsExistProc(Procname As String, oCodePane As CodePane) As Boolean
Dim i As Integer
For i = 1 To oCodePane.CodeModule.CountOfLines
If oCodePane.CodeModule.ProcOfLine(i, vbext_pk_Proc) <> "" Then
If Procname = oCodePane.CodeModule.ProcOfLine(i, vbext_pk_Proc) Then
IsExistProc = True
Exit For
End If
End If
Next i
End Function

Private Function MakeProcedure(EventName, ObjectName, oCodeModule As CodeModule)
oCodeModule.CreateEventProc EventName, ObjectName
End Function

Private Function GetStartUpName() As String
On Error GoTo ErrHandler
GetStartUpName = VBInstance.ActiveVBProject.VBComponents.StartUpObject.Name
Exit Function
ErrHandler:
GetStartUpName = "Sub Main"
End Function

Private Function GetSubMain() As String
Dim v As VBComponent
Dim l As Long, s As String
For Each v In VBInstance.ActiveVBProject.VBComponents
With v
If .Type = vbext_ct_StdModule Then
l = v.CodeModule.CountOfLines
If l > 0 Then
s = v.CodeModule.Lines(1, l)
If InStr(1, s, "sub main()", vbTextCompare) > 0 Then
GetSubMain = v.Name
Exit Function
End If
End If
End If
End With
Next
End Function

Private 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

  • Tambahkan module ini, gantilah namanya menjadi modFileAndDirectory
  • Ganti seluruh kode yang terdapat pada Connect.dsr dengan kode di bawah:
Connect.dsr
Option Explicit

Public FormDisplayed As Boolean
Public VBInstance As VBIDE.VBE
Dim mcbMenuCommandBar As Office.CommandBarControl
'Dim mfrmAddIn As New frmAddIn
Public WithEvents MenuHandler As CommandBarEvents 'command bar event handler

'Sub Hide()
'
' On Error Resume Next
'
' FormDisplayed = False
' mfrmAddIn.Hide
'
'End Sub

Sub Show()

' On Error Resume Next

' If mfrmAddIn Is Nothing Then
' Set mfrmAddIn = New frmAddIn
' End If

Set modAddIns.VBInstance = VBInstance
Set modAddIns.Connect = Me
' FormDisplayed = True
' mfrmAddIn.Show
OKButton_Click
End Sub

'------------------------------------------------------
'this method adds the Add-In to VB
'------------------------------------------------------
Private Sub AddinInstance_OnConnection(ByVal Application As Object, ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, ByVal AddInInst As Object, custom() As Variant)
On Error GoTo error_handler

'save the vb instance
Set VBInstance = Application

'this is a good place to set a breakpoint and
'test various addin objects, properties and methods
Debug.Print VBInstance.FullName

If ConnectMode = ext_cm_External Then
'Used by the wizard toolbar to start this wizard
Me.Show
Else
Set mcbMenuCommandBar = AddToAddInCommandBar("XP-Theme")
'sink the event
Set Me.MenuHandler = VBInstance.Events.CommandBarEvents(mcbMenuCommandBar)
End If

If ConnectMode = ext_cm_AfterStartup Then
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End If

Exit Sub

error_handler:

MsgBox Err.Description

End Sub

'------------------------------------------------------
'this method removes the Add-In from VB
'------------------------------------------------------
Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
On Error Resume Next

'delete the command bar entry
mcbMenuCommandBar.Delete

'shut down the Add-In
If FormDisplayed Then
SaveSetting App.Title, "Settings", "DisplayOnConnect", "1"
FormDisplayed = False
Else
SaveSetting App.Title, "Settings", "DisplayOnConnect", "0"
End If

' Unload mfrmAddIn
' Set mfrmAddIn = Nothing
Set modAddIns.VBInstance = Nothing
Set modAddIns.Connect = Nothing

End Sub

Private Sub IDTExtensibility_OnStartupComplete(custom() As Variant)
If GetSetting(App.Title, "Settings", "DisplayOnConnect", "0") = "1" Then
'set this to display the form on connect
Me.Show
End If
End Sub

'this event fires when the menu is clicked in the IDE
Private Sub MenuHandler_Click(ByVal CommandBarControl As Object, handled As Boolean, CancelDefault As Boolean)
Me.Show
End Sub

Function AddToAddInCommandBar(sCaption As String) As Office.CommandBarControl
Dim cbMenuCommandBar As Office.CommandBarControl 'command bar object
Dim cbMenu As Object

On Error GoTo AddToAddInCommandBarErr

'see if we can find the Add-Ins menu
Set cbMenu = VBInstance.CommandBars("Add-Ins")
If cbMenu Is Nothing Then
'not available so we fail
Exit Function
End If

'add it to the command bar
Set cbMenuCommandBar = cbMenu.Controls.Add(1)
'set the caption
cbMenuCommandBar.Caption = sCaption

Set AddToAddInCommandBar = cbMenuCommandBar

Exit Function

AddToAddInCommandBarErr:

End Function

  • Compile Project dan Register.
  • Tambahkan XP.manifest.res (XP.manifest.res adalah resource manifest yang telah dicompile menggunakan RC.EXE) bisa Anda download disini.
  • Satukah XP.manifest.res dengan DLL yang berasal dari project yang telah dicompile tadi.
  • Jika ingin langsung menggunakan, download project jadinya di bawah ini:

READ MORE - Manifest Injector - 1 kali Klik Untuk XP Style - Tools VB6