Tutorial - Creating a Sample Add-in
Step through the topics in this section to create a sample user interface add-in. If you work through these exercises in order, you will create a project in Microsoft® Visual Basic® that performs the following functions:
- Registers the custom add-in.
- Initializes a Visual Basic project.
- Adds code to implement a form and place new tree nodes within the Analysis Manager tree pane.
- Adds menu items to the new tree nodes.
Registering a Custom Add-in
Add-ins are registered in the following registry key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OLAP Server\Olap Manager Info\Addins
Each custom add-in contains a string value entry in this registry key and its own key in the registry as well. The string value in the Addins key and the Addins key itself must have the same name.
Caution The registry keys DSOInfo and MoveRepository are default keys created when you install Microsoft SQL Server™ 2000 Analysis Services. They should not be modified or deleted. Doing so will have adverse affects on the intended operation of Analysis Manager and may result in the loss of data.
Creating an Addins Key and Key Values
First, in Registry Editor, navigate to the following key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OLAP Server\Olap Manager Info\Addins
Second, create a new String Value entry for the Addins key named OlapSampleAddIn. Set the value of this new entry to True. This instructs the Microsoft OLAP Services Add-Ins Manager library to automatically load the add-in when Microsoft Management Console (MMC) is started. Leaving this value blank or setting it to False will prevent the add-in from loading.
Third, create a new registry key for the OlapSampleAddIn string value. This new key must have the same name as the registry key, that is, OlapSampleAddIn. You should now have a string value in the Addins key and a new registry key with the same name.
Fourth, select the new OlapSampleAddIn key and add four new string values to this key. They are listed in the following table.
Key string value | Description |
---|---|
ClassName | The project and the exposed interface class |
Name | The name displayed on the Add-ins tab of the Properties dialog box in Analysis Manager |
Description | The description displayed on the Add-ins tab of the Properties dialog box in Analysis Manager |
Priority | The loading priority that the Microsoft OLAP Services Add-Ins Manager library uses to load add-ins |
The values for each key string are listed in the following table.
Key string value | Setting |
---|---|
ClassName | OlapSampleAddIn.MyAddIn |
Name | Sample AddIn |
Description | Sample OLAP Manager AddIn |
Priority | 2 |
Note If you are providing custom add-ins to others, you will need to provide an installation procedure that describes or automatically creates the required registry entries before your add-in will function.
Creating the MyAddIn Project in Visual Basic
First, create a Microsoft ActiveX® DLL project in Visual Basic and set the name of the project to OlapSampleAddIn. Change the name of the publicly exposed class to MyAddIn. (This corresponds to the ClassName registry key value.)
Next, set Instancing to 5 - MultiUse in the Properties window for the class.
Then, add Microsoft OLAP Services Add-ins Manager to the project references. (You must have previously installed Microsoft SQL Server™ 2000 Analysis Services for this reference to be available.)
Next, add a form to the project named SampleForm. Add a command button named cmdClose to this form.
Lastly, add the following code to the Form class:
Public Index As Integer
Private Sub cmdClose_Click()
Me.Hide
End Sub
Implementing IOlapAddIn
The example code in this topic implements the IOlapAddIn interface. Place the following code in the Declarations section of the MyAddIn class:
Option Explicit
Implements IOlapAddIn
Const ThisAddInName = "My Sample AddIn"
Private m_SampleForms As Collection
Private Enum MenuActions
mnuactRename = 1
mnuactAddNewForm
mnuactRefreshList
mnuactDeleteSampleForm
mnuactShowSampleForm
mnuactShowTop
mnuactShowCenter
mnuactShowBottom
End Enum
Private Enum SampleIcons
icoForms = 1
icoForm
End Enum
In the Objects box, click IOlapAddIn.
In the Procedures box, select each method that the IOlapAddIn interface provides. This creates an implementation for each method within your class.
Add the following code to the Class_Initialize method:
Private Sub Class_Initialize()
On Error GoTo Initialize_Err
Set m_SampleForms = New Collection
Dim frmSample As New SampleForm
frmSample.Caption = "Sample Form 1"
frmSample.Index = 1
m_SampleForms.Add frmSample, "Sample Form 1"
Exit Sub
Initialize_Err:
Debug.Print Err.Number, Err.Description, Err.Source
Debug.Assert False
MsgBox "An Error Occurred in Class_Initialize"
Err.Clear
Exit Sub
End Sub
Add the following code to the IOlapAddIn_Name property method:
Private Property Get IOlapAddIn_Name() As String
IOlapAddIn_Name = ThisAddInName
End Property
Adding New Tree Nodes
The example code in this topic initializes your custom add-in to add new nodes to the tree node display. You must implement this code before the examples later in this section will work.
Add the following code to the IOlapAddIn_ProvideChildNodes method:
Private Sub IOlapAddIn_ProvideChildNodes( _
ParentNode As DSSAddInsManager.OlapTreeNode, _
OlapTreeNodes As DSSAddInsManager.OlapTreeNodes)
On Error GoTo IOlapAddIn_ProvideChildNodes_Err
If ParentNode.Caption = "Analysis Servers" Then
OlapTreeNodes.Add "Sample Forms", icoForms
ElseIf ParentNode.Caption = "Sample Forms" Then
Dim frm As Form
For Each frm In m_SampleForms
OlapTreeNodes.Add frm.Caption, icoForm
Next
End If
Exit Sub
IOlapAddIn_ProvideChildNodes_Err:
Debug.Print Err.Number, Err.Description, Err.Source
Debug.Assert False
MsgBox "Provide Child Nodes Failed"
Err.Clear
Exit Sub
End Sub
Run the application.
With the Visual Basic project executing, start Analysis Manager, and then browse the tree pane. You should see the new tree nodes added to the bottom of the tree.
Adding New Menu Items
The example code in this step adds new menu items to the tree nodes that were added in the previous exercise. You must implement this code before the examples later in this section will work.
Add the following code to the IOlapAddIn_ProvideMenuItems method:
Private Sub IOlapAddIn_ProvideMenuItems( _
CurrentNode As DSSAddInsManager.OlapTreeNode, _
MenuItems As DSSAddInsManager.OlapMenuItems)
On Error GoTo IOlapAddIn_ProvideMenuItems_Err
Dim iFlags As OlapMenuFlags
' The Microsoft OLAP Services Add-Ins Manager calls this method
' each time a tree node is accessed.
' Because multiple add-ins are supported,
' verify that this is the correct tree node to respond to.
If CurrentNode.OwnerAddInName <> ThisAddInName Then Exit Sub
Select Case CurrentNode.Caption
Case "Sample Forms"
MenuItems.Add mnuStandard, "&Form", _
mnuactAddNewForm, , mnuflagNew
MenuItems.Add mnuStandard, "&Refresh", _
mnuactRefreshList, , mnuflagNew
Case Else
MenuItems.Add mnuStandard, "&Show", _
mnuactShowSampleForm, , mnuflagPopup
MenuItems.Add mnuStandard, "&Top", mnuactShowTop, _
mnuactShowSampleForm, mnuflagSubmenu
MenuItems.Add mnuStandard, "&Center", _
mnuactShowCenter, mnuactShowSampleForm, _
mnuflagSubmenu
MenuItems.Add mnuStandard, "&Bottom", _
mnuactShowBottom, mnuactShowSampleForm, _
mnuflagSubmenu
MenuItems.Add mnuSeparator
MenuItems.Add mnuStandard, "&Rename"
MenuItems.Add mnuStandard, "&Delete", _
mnuactDeleteSampleForm, , mnuflagDeleteKey
End Select
Exit Sub
IOlapAddIn_ProvideMenuItems_Err:
Debug.Print Err.Number, Err.Description, Err.Source
Debug.Assert False
MsgBox "Provide Menu Items Failed"
Err.Clear
Exit Sub
End Sub
Run the application.
With the Visual Basic project executing, start Analysis Manager, and then browse the tree pane. Right-click one of the new tree nodes, and then examine the added menu items.
Responding to Menu Item Selection
The example code in this topic adds code to respond to user selection of the menu items added in the previous example.
Add the following method to the MyAddIn class:
Private Function IsNameUsed(szName As String, _
col As Collection) As Boolean
On Error GoTo IsNameUsed_Err
Dim vTmp As Variant
Set vTmp = col(szName)
IsNameUsed = True
Exit Function
IsNameUsed_Err:
IsNameUsed = False
Err.Clear
Exit Function
End Function
Add the following code to the IOlapAddIn_ExecuteMenuItems method:
Private Function IOlapAddIn_ExecuteMenuItem( _
CurrentNode As DSSAddInsManager.OlapTreeNode, _
MenuItem As DSSAddInsManager.OlapMenuItem) _
As DSSAddInsManager.RefreshTreeTypes
On Error GoTo IOlapAddIn_ExecuteMenuItem_Err
Dim frmSample As SampleForm
Dim szFormCaption As String
Dim szNodeCaption As String
Dim iFormIndex As Integer
szNodeCaption = CurrentNode.Caption
Select Case MenuItem.Key
Case mnuactRename
Dim szName As String
Dim tmpForm As SampleForm
Set tmpForm = m_SampleForms(szNodeCaption)
m_SampleForms.Remove szNodeCaption
Do
szName = InputBox("Please enter the new name:", _
"Rename a Form", szNodeCaption)
If Len(szName) = 0 Then
MsgBox _
"The name must not be a zero length string", _
vbExclamation, "Invalid Name"
Else
Exit Do
End If
Loop
tmpForm.Caption = szName
If tmpForm.Index <= m_SampleForms.Count Then
m_SampleForms.Add tmpForm, szName, tmpForm.Index
Else
' This is the only item in the list
' or it was at the end of the list.
' No need to specify a before value
m_SampleForms.Add tmpForm, szName
End If
' Manually tell the Microsoft OLAP Services Add-Ins Manager to
' refresh the tree
IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow
Case mnuactShowTop
Set frmSample = m_SampleForms(szNodeCaption)
frmSample.Move (Screen.Width - frmSample.Width) / 2, 0
frmSample.Show vbModal
Case mnuactShowCenter
Set frmSample = m_SampleForms(szNodeCaption)
frmSample.Move (Screen.Width - frmSample.Width) / 2, _
(Screen.Height - frmSample.Height) / 2
frmSample.Show vbModal
Case mnuactShowBottom
Set frmSample = m_SampleForms(szNodeCaption)
frmSample.Move (Screen.Width - frmSample.Width) / 2, _
Screen.Height - frmSample.Height
frmSample.Show vbModal
Case mnuactAddNewForm
Set frmSample = New SampleForm
iFormIndex = m_SampleForms.Count
Do
iFormIndex = iFormIndex + 1
szFormCaption = "Sample Form " & iFormIndex
Loop While IsNameUsed(szFormCaption, m_SampleForms)
frmSample.Caption = szFormCaption
frmSample.Index = iFormIndex
m_SampleForms.Add frmSample, szFormCaption
Set frmSample = Nothing
' Tell Microsoft OLAP Services Add-Ins Manager to refresh the tree
IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow
Case mnuactDeleteSampleForm
m_SampleForms.Remove szNodeCaption
Case mnuactRefreshList
IOlapAddIn_ExecuteMenuItem = reftreeCurrentAndBelow
End Select
Exit Function
IOlapAddIn_ExecuteMenuItem_Err:
Debug.Print Err.Number, Err.Description, Err.Source
Debug.Assert False
MsgBox "Execute Menu Item Failed"
Err.Clear
Exit Function
End Function
Run the application.
With the Visual Basic project executing, start Analysis Manager, and then browse the tree pane. Right-click one of the new tree nodes, and then click a menu item.