home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 32 / IOPROG_32.ISO / SOFT / SqlEval7 / MSOLAP / samples / Samples.exe / VbDSOCreateSmallCube / frmMain.frm (.txt) < prev   
Encoding:
Visual Basic Form  |  1998-10-30  |  39.9 KB  |  916 lines

  1. VERSION 5.00
  2. Begin VB.Form frmMain 
  3.    Caption         =   "Form1"
  4.    ClientHeight    =   5976
  5.    ClientLeft      =   48
  6.    ClientTop       =   276
  7.    ClientWidth     =   4128
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   5976
  10.    ScaleWidth      =   4128
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.CommandButton btnBuildCube 
  13.       Caption         =   "Build Cube"
  14.       Height          =   252
  15.       Left            =   2400
  16.       TabIndex        =   13
  17.       Top             =   240
  18.       Width           =   1692
  19.    End
  20.    Begin VB.CommandButton btnDone 
  21.       Caption         =   "Done"
  22.       Height          =   252
  23.       Left            =   1440
  24.       TabIndex        =   3
  25.       Top             =   5400
  26.       Width           =   1212
  27.    End
  28.    Begin VB.Frame frmProgramSteps 
  29.       Caption         =   "Steps to Make a Cube"
  30.       Height          =   5052
  31.       Left            =   120
  32.       TabIndex        =   2
  33.       Top             =   600
  34.       Width           =   3972
  35.       Begin VB.CheckBox chkProgramStep 
  36.          Caption         =   "Step 9 - Write Enable the Cube"
  37.          Enabled         =   0   'False
  38.          Height          =   252
  39.          Index           =   8
  40.          Left            =   240
  41.          TabIndex        =   12
  42.          Top             =   4200
  43.          Width           =   3012
  44.       End
  45.       Begin VB.CheckBox chkProgramStep 
  46.          Caption         =   "Step -8 Process the Database"
  47.          Enabled         =   0   'False
  48.          Height          =   252
  49.          Index           =   7
  50.          Left            =   240
  51.          TabIndex        =   11
  52.          Top             =   3720
  53.          Width           =   3132
  54.       End
  55.       Begin VB.CheckBox chkProgramStep 
  56.          Caption         =   "Step 7 - Create the Cube"
  57.          Enabled         =   0   'False
  58.          Height          =   252
  59.          Index           =   6
  60.          Left            =   240
  61.          TabIndex        =   10
  62.          Top             =   3240
  63.          Width           =   3132
  64.       End
  65.       Begin VB.CheckBox chkProgramStep 
  66.          Caption         =   "Step 6 - Create the ""Product"" Dimension"
  67.          Enabled         =   0   'False
  68.          Height          =   372
  69.          Index           =   5
  70.          Left            =   240
  71.          TabIndex        =   9
  72.          Top             =   2760
  73.          Width           =   3612
  74.       End
  75.       Begin VB.CheckBox chkProgramStep 
  76.          Caption         =   "Step 5 - Create the ""Customer"" Dimension"
  77.          Enabled         =   0   'False
  78.          Height          =   252
  79.          Index           =   4
  80.          Left            =   240
  81.          TabIndex        =   8
  82.          Top             =   2280
  83.          Width           =   3492
  84.       End
  85.       Begin VB.CheckBox chkProgramStep 
  86.          Caption         =   "Step 4 - Create a New DataSource"
  87.          Enabled         =   0   'False
  88.          Height          =   372
  89.          Index           =   3
  90.          Left            =   240
  91.          TabIndex        =   7
  92.          Top             =   1680
  93.          Width           =   3492
  94.       End
  95.       Begin VB.CheckBox chkProgramStep 
  96.          Caption         =   "Step 3 - Create a new Database"
  97.          Enabled         =   0   'False
  98.          Height          =   252
  99.          Index           =   2
  100.          Left            =   240
  101.          TabIndex        =   6
  102.          Top             =   1200
  103.          Width           =   3132
  104.       End
  105.       Begin VB.CheckBox chkProgramStep 
  106.          Caption         =   "Step 2 - Connect to Server"
  107.          Enabled         =   0   'False
  108.          Height          =   252
  109.          Index           =   1
  110.          Left            =   240
  111.          TabIndex        =   5
  112.          Top             =   720
  113.          Width           =   2772
  114.       End
  115.       Begin VB.CheckBox chkProgramStep 
  116.          Caption         =   "Step 1 - Initialize"
  117.          Enabled         =   0   'False
  118.          Height          =   252
  119.          Index           =   0
  120.          Left            =   240
  121.          TabIndex        =   4
  122.          Top             =   240
  123.          Width           =   2412
  124.       End
  125.    End
  126.    Begin VB.TextBox txtServerName 
  127.       Height          =   288
  128.       Left            =   120
  129.       TabIndex        =   0
  130.       Text            =   "LocalHost"
  131.       Top             =   240
  132.       Width           =   1932
  133.    End
  134.    Begin VB.Label Label1 
  135.       Caption         =   "Enter Server name"
  136.       Height          =   252
  137.       Left            =   120
  138.       TabIndex        =   1
  139.       Top             =   0
  140.       Width           =   1932
  141.    End
  142. Attribute VB_Name = "frmMain"
  143. Attribute VB_GlobalNameSpace = False
  144. Attribute VB_Creatable = False
  145. Attribute VB_PredeclaredId = True
  146. Attribute VB_Exposed = False
  147. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  148. ' Create a small cube - Example Program.
  149. ' Creates a small cube from the foodmart database intended to be used
  150. ' with the writeback example program. This cube has the following properties:
  151. ' Dimensions:
  152. '               Products
  153. '               Customers
  154. ' Measures:
  155. '               Sales_Fact_1998
  156. '(C) Copyright 1998, Microsoft Corporation. All rights reserved.
  157. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  158. Option Explicit
  159. Option Compare Text
  160. ' Declare module level (global) variables.
  161. Public m_dsoServer   As DSO.Server
  162. Public m_dsoDatabase As DSO.MDStore
  163. Public m_dsoCube     As DSO.MDStore
  164. ' Declare an integer to keep track of the program steps as they are executed.
  165. Private m_count As Integer
  166. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  167. ' btnBuldCube_Click() - Executed when the user pressed the "Build Cube" button.
  168. ' Runs through each step in building a cube, and checks off each step as it goes.
  169. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  170. Private Sub btnBuildCube_Click()
  171.     ' Initialize the server and close it if is already open.
  172.     DoNextStep
  173.     Initialize
  174.     FinishStep
  175.     ' Connect to the server specified in ther txtServerName txt box.
  176.     DoNextStep
  177.     ConnectToServer
  178.     FinishStep
  179.     ' Create a Database on that server.
  180.     DoNextStep
  181.     CreateDatabase
  182.     FinishStep
  183.     'Create a Datasource in that Database.
  184.     DoNextStep
  185.     CreateDatasource
  186.     FinishStep
  187.     'Create the Customer Dimension.
  188.     DoNextStep
  189.     CreateDimensionCustomer
  190.     FinishStep
  191.         
  192.     'Create the Product Dimension.
  193.     DoNextStep
  194.     CreateDimensionProduct
  195.     FinishStep
  196.     ' Create the cube and save it in the meta data repository.
  197.     DoNextStep
  198.     CreateCube
  199.     FinishStep
  200.     ' Process the cube.
  201.     DoNextStep
  202.     ProcessDatabase
  203.     FinishStep
  204.     ' After this procedure has been completed, write enable the cube
  205.     ' by running the VbDsoWriteEnable sample.
  206. End Sub
  207. ' Unloads the form and exits the program.
  208. Private Sub btnDone_Click()
  209.     Unload Me
  210. End Sub
  211. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  212. ' DoNextStep() - 'Enables' Each program step check mark showing what is
  213. ' currently in progress.
  214. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  215. Private Sub DoNextStep()
  216.     Screen.MousePointer = vbHourglass
  217.     chkProgramStep(m_count).Enabled = True
  218.     chkProgramStep(m_count).Value = 0
  219.     chkProgramStep(m_count).ForeColor = vbRed
  220.     chkProgramStep(m_count).Refresh
  221.     DoEvents
  222. End Sub
  223. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  224. ' FinishStep() - Checks off the current program setp check mark as each
  225. ' Program Step is accomplished. Increments the program step counter.
  226. '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  227. Private Sub FinishStep()
  228.     chkProgramStep(m_count).Value = 1
  229.     chkProgramStep(m_count).Refresh
  230.     chkProgramStep(m_count).ForeColor = vbBlack
  231.     DoEvents
  232.     m_count = m_count + 1
  233.     Screen.MousePointer = vbDefault
  234. End Sub
  235. Private Sub Form_Load()
  236.     Me.Refresh
  237.     m_count = 0
  238. End Sub
  239. '   Initialize - initialize the sample application
  240. Public Sub Initialize()
  241.     ' If the DSO server object has already been in use
  242.     '   then call CloseServer method
  243.     ' NOTE: CloseServer method releases all of the resources
  244.     '       acquired by DSO.  It is important to call this
  245.     '       method when the DSO server object is not needed
  246.     '       any longer
  247.     If Not m_dsoServer Is Nothing Then
  248.         m_dsoServer.CloseServer
  249.     End If
  250. End Sub
  251. '   ConnectToServer - create and connect to the DSO server
  252. Public Sub ConnectToServer()
  253.     ' create an instance of the server object
  254.     Dim dsoServer As DSO.Server
  255.     Set dsoServer = New DSO.Server
  256.     ' connect to the server
  257.     On Error GoTo Err_Connect
  258.     dsoServer.Connect frmMain.txtServerName
  259.                                         
  260.     ' store the reference to the server object
  261.     ' so that other methods can use it
  262.     Set m_dsoServer = dsoServer
  263.     Debug.Print 'Connected to server " & m_dsoServer.name
  264. Exit Sub
  265. Err_Connect:
  266.     ' Failed to connect to the server.
  267.     ' Possible reasons:
  268.     '   - the Microsoft SQL Server OLAP Server is not running
  269.     '   - the machine on which the OLAP Server is running cannot be reached
  270.     '   - the machine on which DSO application is running is not connected
  271.     '       to the network
  272.     '   - you are not a member of the OLAP Administrators user group on the
  273.     '       server machine
  274. End Sub
  275. '   CreateDatabase - create new database
  276. Public Sub CreateDatabase()
  277.     ' call the database "DSOSample"
  278.     Dim sDatabaseName As String
  279.     sDatabaseName = "FoodMart"
  280.     ' check if a database with the same name
  281.     ' already exists on the server
  282.     If m_dsoServer.MDStores.Find(sDatabaseName) Then
  283.         
  284.         Set m_dsoDatabase = m_dsoServer.MDStores("FoodMart")
  285.         Debug.Print "Found Database."
  286.     Else
  287.         ' create the new database
  288.         On Error GoTo Err_CreateDatabase
  289.         Dim dsoDatabase As DSO.MDStore
  290.         Set dsoDatabase = m_dsoServer.MDStores.AddNew(sDatabaseName)
  291.         
  292.         ' set the database description
  293.         dsoDatabase.Description = "SampleDSO database contains Warehouse cube."
  294.         
  295.         ' save the database definition in the OLAP server's metadata repository
  296.         On Error GoTo Err_Update
  297.         dsoDatabase.Update
  298.         Debug.Print "Database created."
  299.         ' store the reference to the database object
  300.         ' so that other methods can use it
  301.         Set m_dsoDatabase = dsoDatabase
  302.     End If
  303. Exit Sub
  304. Err_DeleteDatabase:
  305.     ' Failed to remove the database from the server.
  306.     ' Possible reasons:
  307.     '   - the OLAP server is not running or unreachable
  308.     '   - the database is being used by another DSO application
  309. Err_CreateDatabase:
  310.     ' Failed to create the database on the server.
  311.     ' Possible reasons:
  312.     '   - the OLAP server is not running or unreachable
  313.     '   - the DSO server object is being locked by another DSO application
  314.     '       the server object is locked while AddNew method is executing on
  315.     '       the server's MDStores collection
  316.     '   - the metadata repository is unreachable
  317. Err_Update:
  318.     ' Failed to persist the database definition in the metadata repository
  319.     ' Possible reasons:
  320.     '   - the metadata repository is unreachable
  321.     '       you can see where the metadata repository resides by looking
  322.     '       up the following registry entry:
  323.     '       HKEY_LOCAL_MACHINE\Software\Microsoft\OLAP Server\Server Connection Info
  324.     '           Repository Connection String
  325.     '   - the DSO database object is being locked by another DSO application
  326.     '       it is not possible for two DSO apps to persist the same object
  327.     '       at the same time
  328.     '       it is not possible to persist a DSO object, it another DSO app
  329.     '       has explicitly locked it
  330.     MsgBox "Create database failed" & vbCrLf & Err.Description
  331. End Sub
  332. '   CreateDatasource - create the new datasource
  333. Public Sub CreateDatasource()
  334.     ' create the new datasource that points to the
  335.     ' sample FoodMart database
  336.     On Error GoTo Err_CreateDatasource
  337.     Dim dsoDatasource As DSO.DataSource
  338.         Set dsoDatasource = m_dsoDatabase.DataSources.AddNew("Foodmart Sample Database")
  339.         
  340.         ' set the OleDB connection string
  341.         '   the connection string is used to establish the connection
  342.         '   to the relational database that contains the dimension and
  343.         '   fact tables
  344.         ' we will use OleDB provider for ODBC drivers
  345.         dsoDatasource.ConnectionString = _
  346.                 "Provider=MSDASQL.1;Data Source=FoodMart;Connect Timeout=15"
  347.         
  348.         ' save the datasource definition in the metadata repository
  349.         On Error GoTo Err_Update
  350.         dsoDatasource.Update
  351.         Debug.Print "Datasource added to database."
  352. Exit Sub
  353. Err_CreateDatasource:
  354.     ' Failed to create the datasource.
  355.     ' Possible reasons:
  356.     '   - the DSO database object is being locked by another DSO application
  357.     '       a DSO object is locked while AddNew method is executing on
  358.     '       one of its collections (Datasources, MDStores, ...)
  359.     '   - the metadata repository is unreachable
  360. Err_Update:
  361.     ' Failed to persist the datasource definition in the metadata repository
  362.     ' Possible reasons:
  363.     '   - the metadata repository is unreachable
  364.     '       you can see where the metadata repository resides by looking
  365.     '       up the following registry entry:
  366.     '       HKEY_LOCAL_MACHINE\Software\Microsoft\OLAP Server\Server Connection Info
  367.     '           Repository Connection String
  368.     '   - the DSO datasource object is being locked by another DSO application
  369.     '       it is not possible for two DSO apps to persist the same object
  370.     '       at the same time
  371.     '       it is not possible to persist a DSO object, it another DSO app
  372.     '       has explicitly locked it
  373.     MsgBox "Create datasource failed" & vbCrLf & Err.Description
  374. End Sub
  375. '   CreateDimensionCustomer - create the Customer dimension
  376. '   NOTE: the Customer dimension is an example of a dimension
  377. '         that is based on one dimension table (star schema)
  378. Public Sub CreateDimensionCustomer()
  379.     ' create the Customer dimension in the database's
  380.     ' Dimensions collection
  381.     On Error GoTo Err_Create
  382.     Dim dsoDimension As DSO.Dimension
  383.     Set dsoDimension = m_dsoDatabase.Dimensions.AddNew("Customer")
  384.     ' set the dimension description
  385.     dsoDimension.Description = "The Customers hierarchy"
  386.     ' set the dimension's datasource
  387.     Set dsoDimension.DataSource = m_dsoDatabase.DataSources("Foodmart Sample Database")
  388.     ' get the quoting characters from the datasource
  389.     Dim sLQuote As String, sRQuote As String
  390.     sLQuote = dsoDimension.DataSource.OpenQuoteChar
  391.     sRQuote = dsoDimension.DataSource.CloseQuoteChar
  392.     ' set the dimension type
  393.     dsoDimension.DimensionType = dimRegular
  394.     ' set the comma separeted list of the dimension tables
  395.     ' NOTE: the tables must be quoted
  396.     ' the Customer dimension uses only the "Customer" table
  397.     dsoDimension.FromClause = sLQuote & "customer" & sRQuote
  398.                               
  399.     ' define the joins between tables used by this dimension
  400.     ' the format of this property is
  401.     '   (table1.columnX = table2.columnY) and (table2.columnZ = table3.columnW)
  402.     ' since the Customer dimension is based on only one table, we have no joins
  403.     dsoDimension.JoinClause = ""
  404.     '
  405.     ' create dimension levels
  406.     '
  407.     Dim dsoLevel As DSO.Level
  408.     '
  409.     ' create the All level
  410.     '   the All level is the top-most level in the dimension hierarcy
  411.     On Error GoTo Err_Create
  412.     Set dsoLevel = dsoDimension.Levels.AddNew("All")
  413.     ' set the level type
  414.     dsoLevel.LevelType = levAll
  415.     ' the All level has only one member
  416.     ' set the MemberKeyColumn of the All level to a constant
  417.     ' this constant is at the same time the name of that single member
  418.     dsoLevel.MemberKeyColumn = "All Customers"
  419.     '
  420.     ' create the Customer Country level
  421.     '   the Customer Country level contains all of the countries
  422.     On Error GoTo Err_Create
  423.     Set dsoLevel = dsoDimension.Levels.AddNew("Customer Country")
  424.     ' set the level type
  425.     dsoLevel.LevelType = levRegular
  426.     ' define which column contains the level member keys
  427.     ' sLQuote and sRQuote are obtained from dsoDimension.Datasource
  428.     ' NOTE: the tables and columns must be quoted using the quoting
  429.     '       characters obtained from the dimension's datasource
  430.     dsoLevel.MemberKeyColumn = sLQuote & "customer" & sRQuote & "." & _
  431.                                sLQuote & "country" & sRQuote
  432.     ' tell DSO how many members this level has
  433.     ' this information will be used during the design of aggregations
  434.     '   when determining the optimal set of aggregations
  435.     ' there are 3 countries in the "Customer" table: Mexico, Canada, and USA
  436.     dsoLevel.EstimatedSize = 3
  437.     ' this level contains unique members
  438.     dsoLevel.IsUnique = True
  439.     ' DSO needs to know what is the type and maximum size of the members keys
  440.     ' NOTE: the ColumnSize property needs to be set only for
  441.     '       levels that have string members
  442.     dsoLevel.ColumnType = adChar
  443.     dsoLevel.ColumnSize = 6         ' the longest members ("Mexico", "Canada")
  444.                                     ' can fit in 6 characters
  445.     ' specify how should the level members be ordered
  446.     ' we want the countries ordered by Name
  447.     dsoLevel.Ordering = orderName
  448.     '
  449.     ' create the Customer State level
  450.     '   the Customer State level contains all of the states
  451.     On Error GoTo Err_Create
  452.     Set dsoLevel = dsoDimension.Levels.AddNew("Customer State")
  453.     ' set the level type
  454.     dsoLevel.LevelType = levRegular
  455.     ' "cusomter_state" column contains members for this level
  456.     dsoLevel.MemberKeyColumn = sLQuote & "customer" & sRQuote & "." & _
  457.                                sLQuote & "state_province" & sRQuote
  458.     ' there are 10 distinct states in the "Customer" table
  459.     dsoLevel.EstimatedSize = 10
  460.     ' this level contains unique members,
  461.     ' i.e. no state appears in more than one country
  462.     dsoLevel.IsUnique = True
  463.     ' DSO needs to know what is the type and maximum size of the members keys
  464.     dsoLevel.ColumnType = adChar
  465.     dsoLevel.ColumnSize = 9             ' the longest member ("Zacatecas")
  466.                                         ' can fit in 9 characters
  467.     ' we want the states ordered by Name
  468.     dsoLevel.Ordering = orderName
  469.     '
  470.     ' create the Customer City level
  471.     '   the Customer City level contains all of the cities
  472.     On Error GoTo Err_Create
  473.     Set dsoLevel = dsoDimension.Levels.AddNew("Customer City")
  474.     ' set the level type
  475.     dsoLevel.LevelType = levRegular
  476.     ' "Customer_city" column contains members for this level
  477.     dsoLevel.MemberKeyColumn = sLQuote & "customer" & sRQuote & "." & _
  478.                                sLQuote & "city" & sRQuote
  479.     ' there are 23 distinct cities in the "customer" table
  480.     dsoLevel.EstimatedSize = 23
  481.     ' this level contains unique members,
  482.     ' i.e. no city appears in more than one state
  483.     ' NOTE: this is often not the case,
  484.     '       for example: Portland (Oregon) and Portland (Maine)
  485.     dsoLevel.IsUnique = False
  486.     ' DSO needs to know what is the type and maximum size of the members keys
  487.     dsoLevel.ColumnType = adChar
  488.     dsoLevel.ColumnSize = 13            ' the longest member ("San Francisco")
  489.                                         ' can fit in 13 characters
  490.     ' we want the cities ordered by Name
  491.     dsoLevel.Ordering = orderName
  492.     '
  493.     ' create the customer Name level
  494.     '   the customer Name level contains all of the individual stores
  495.     On Error GoTo Err_Create
  496.     Set dsoLevel = dsoDimension.Levels.AddNew("Customer Name")
  497.     ' set the level type
  498.     dsoLevel.LevelType = levRegular
  499.     ' "customer_id" column contains members for this level
  500.     dsoLevel.MemberKeyColumn = sLQuote & "customer" & sRQuote & "." & _
  501.                                sLQuote & "customer_id" & sRQuote
  502.     ' we will use column "fname lname" for member names
  503.     dsoLevel.MemberNameColumn = sLQuote & "customer" & sRQuote & "." & _
  504.                                 sLQuote & "lname" & sRQuote
  505.     ' there are 24 distinct stores in the "store" table
  506.     dsoLevel.EstimatedSize = 24
  507.     ' this level contains unique members,
  508.     ' i.e. no customer appears in more than one city
  509.     dsoLevel.IsUnique = False
  510.     ' DSO needs to know what is the data type of the level members keys
  511.     ' since this level has numeric member keys, we do not need to set the ColumnSize
  512.     dsoLevel.ColumnType = adInteger
  513.     'dsoLevel.ColumnSize = 20
  514.     ' we want the stores ordered by Name
  515.     dsoLevel.Ordering = orderName
  516.     '
  517.     ' create the member properties for the Store Name level
  518.     '
  519.     Dim dsoMemberProperty As DSO.MemberProperty
  520.     ' save the dimension definition in the metadata repository
  521.     On Error GoTo Err_Update
  522.     dsoDimension.Update
  523.     Debug.Print "Dimension Customer added to database."
  524. Exit Sub
  525. Err_Create:
  526.     ' Failed to create the object.
  527.     ' Possible reasons:
  528.     '   - the DSO database object is being locked by another DSO application
  529.     '       a DSO object is locked while AddNew method is executing on
  530.     '       one of its collections (Datasources, MDStores, Dimensions...)
  531.     '   - the metadata repository is unreachable
  532. Err_Update:
  533.     ' Failed to persist the dimension definition in the metadata repository
  534.     ' Possible reasons:
  535.     '   - the metadata repository is unreachable
  536.     '       you can see where the metadata repository resides by looking
  537.     '       up the following registry entry:
  538.     '       HKEY_LOCAL_MACHINE\Software\Microsoft\OLAP Server\Server Connection Info
  539.     '           Repository Connection String
  540.     '   - the DSO dimension object is being locked by another DSO application
  541.     '       it is not possible for two DSO apps to persist the same object
  542.     '       at the same time
  543.     '       it is not possible to persist a DSO object, it another DSO app
  544.     '       has explicitly locked it
  545.     MsgBox "Create dimension Store failed" & vbCrLf & Err.Description
  546. End Sub
  547. '   CreateDimensionProduct - create the Product dimension
  548. '   NOTE: the Product dimension is an example of a snowflake dimension
  549. '         (dimension based on multiple tables)
  550. Public Sub CreateDimensionProduct()
  551.     ' create the Product dimension in the database's
  552.     ' Dimensions collection
  553.     On Error GoTo Err_Create
  554.     Dim dsoDimension As DSO.Dimension
  555.     Set dsoDimension = m_dsoDatabase.Dimensions.AddNew("Product")
  556.     ' set the dimension description
  557.     dsoDimension.Description = "The Product hierarchy"
  558.     ' set the dimension's datasource
  559.     Set dsoDimension.DataSource = m_dsoDatabase.DataSources("Foodmart Sample Database")
  560.     ' get the quoting characters from the datasource
  561.     Dim sLQuote As String, sRQuote As String
  562.     sLQuote = dsoDimension.DataSource.OpenQuoteChar
  563.     sRQuote = dsoDimension.DataSource.CloseQuoteChar
  564.     ' set the dimension type
  565.     dsoDimension.DimensionType = dimRegular
  566.     ' set the comma separeted list of the dimension tables
  567.     ' NOTE: the tables must be quoted
  568.     ' the Product dimension uses "product" and "product_class" tables
  569.     dsoDimension.FromClause = sLQuote & "product" & sRQuote & _
  570.                               ", " & _
  571.                               sLQuote & "product_class" & sRQuote
  572.                                   
  573.     '
  574.     ' create dimension levels
  575.     '
  576.     Dim dsoLevel As DSO.Level
  577.     '
  578.     ' create the Product Name level
  579.     '   this level contains all of the brand names
  580.     On Error GoTo Err_Create
  581.     Set dsoLevel = dsoDimension.Levels.AddNew("Product Name")
  582.     ' set the level type
  583.     dsoLevel.LevelType = levRegular
  584.     ' we will use the "product_name" column for member names
  585.     dsoLevel.MemberKeyColumn = sLQuote & "product" & sRQuote & "." & _
  586.                                sLQuote & "product_name" & sRQuote
  587.     ' there are 1560 distinct products
  588.     ' NOTE: you can use the following query to
  589.     '       count the distinct members for this level
  590.     '       SELECT DISTINCT product_class.product_family,
  591.     '                       product_class.product_category,
  592.     '                       product.brand_name,
  593.     '                       product_id
  594.     '       FROM product_class, product
  595.     '       WHERE product_class.product_class_id = product.product_class_id
  596.     '
  597.     dsoLevel.EstimatedSize = 1560
  598.     ' this level contains unique members
  599.     dsoLevel.IsUnique = True
  600.     ' DSO needs to know what is the type and maximum size of the members keys
  601.     dsoLevel.ColumnType = adWChar
  602.     ' we want the brands ordered by name
  603.     dsoLevel.Ordering = orderName
  604.     On Error GoTo Err_Update
  605.     dsoDimension.Update
  606.     Debug.Print "Dimension Product added to database."
  607. Exit Sub
  608. Err_Create:
  609.     ' Failed to create the object.
  610.     ' Possible reasons:
  611.     '   - the DSO database object is being locked by another DSO application
  612.     '       a DSO object is locked while AddNew method is executing on
  613.     '       one of its collections (Datasources, MDStores, Dimensions...)
  614.     '   - the metadata repository is unreachable
  615. Err_Update:
  616.     ' Failed to persist the dimension definition in the metadata repository
  617.     ' Possible reasons:
  618.     '   - the metadata repository is unreachable
  619.     '       you can see where the metadata repository resides by looking
  620.     '       up the following registry entry:
  621.     '       HKEY_LOCAL_MACHINE\Software\Microsoft\OLAP Server\Server Connection Info
  622.     '           Repository Connection String
  623.     '   - the DSO dimension object is being locked by another DSO application
  624.     '       it is not possible for two DSO apps to persist the same object
  625.     '       at the same time
  626.     '       it is not possible to persist a DSO object, it another DSO app
  627.     '       has explicitly locked it
  628.     MsgBox "Create dimension Product failed" & vbCrLf & Err.Description
  629. End Sub
  630. '   CreateCube - create the Sales cube
  631. Public Sub CreateCube()
  632.     ' create the Sales cube in the database's
  633.     ' MDStores collection
  634.     On Error GoTo Err_Create
  635.     Dim dsoCube As DSO.MDStore
  636.     Dim strCubeName As String
  637.     Set dsoCube = m_dsoDatabase.MDStores.AddNew("small")
  638.     ' set the cube's description
  639.     dsoCube.Description = "The Sales cube"
  640.     ' set the cube's datasource
  641.     ' use the datasource that was created in the database's Datasources collection
  642.     dsoCube.DataSources.Add m_dsoDatabase.DataSources("Foodmart Sample Database")
  643.     ' get the quoting characters from the datasource
  644.     Dim sLQuote As String, sRQuote As String
  645.     sLQuote = dsoCube.DataSources(1).OpenQuoteChar
  646.     sRQuote = dsoCube.DataSources(1).CloseQuoteChar
  647.     ' set the source table (fact table) for the cube
  648.     dsoCube.SourceTable = sLQuote & "sales_fact_1998" & sRQuote
  649.     ' set the number of rows from the fact table that will be included in the cube
  650.     ' since we want the whole table (we did not specify anything for SourceTableFilter)
  651.     '   we can obtain this number by doing SELECT COUNT(*) FROM sales_fact_1998
  652.     dsoCube.EstimatedRows = 164558
  653.     '
  654.     ' create cube's measures
  655.     '
  656.     Dim dsoMeasure As DSO.Measure
  657.     ' create a measure for customer Sales
  658.     Set dsoMeasure = dsoCube.Measures.AddNew("Store Sales")
  659.     ' set the measure's source column, data type and the formatting
  660.     dsoMeasure.SourceColumn = dsoCube.SourceTable & "." & _
  661.                               sLQuote & "store_sales" & sRQuote
  662.     dsoMeasure.SourceColumnType = adDouble
  663.     dsoMeasure.FormatString = "Currency"
  664.     ' this measure will be aggregated by summation
  665.     dsoMeasure.AggregateFunction = aggSum
  666.      
  667.     '
  668.     '   add dimensions to the cube
  669.     '
  670.     ' add the Product dimension
  671.     ' note that by adding the dimension to the cube
  672.     '   all of the dimension levels are automatically inherited from
  673.     '   the corresponding database dimension
  674.     Dim dsoProductCubeDim As DSO.Dimension
  675.     Set dsoProductCubeDim = dsoCube.Dimensions.AddNew("product")
  676.     ' add the Store dimension
  677.     Dim dsoCustomerCubeDim As DSO.Dimension
  678.     Set dsoCustomerCubeDim = dsoCube.Dimensions.AddNew("customer")
  679.     ' get the list of all tables used in this cube
  680.     ' this list includes the fact table and the dimension tables
  681.     dsoCube.FromClause = dsoCube.SourceTable & ", " & _
  682.                          dsoCustomerCubeDim.FromClause & ", " & _
  683.                          dsoProductCubeDim.FromClause
  684.     Debug.Print "Cube FromClause =" & dsoCube.FromClause
  685.     ' define the joins between tables used by the cube
  686.     ' first define the join between the fact table and the Store table
  687.     dsoCube.JoinClause = _
  688.             "(" & _
  689.                 sLQuote & "sales_fact_1998" & sRQuote & "." & sLQuote & "customer_id" & sRQuote & _
  690.                 " = " & _
  691.                 sLQuote & "customer" & sRQuote & "." & sLQuote & "customer_id" & sRQuote & _
  692.             ")"
  693.     ' define the join between the fact table and the Product table
  694.     dsoCube.JoinClause = dsoCube.JoinClause & " AND " & _
  695.             "(" & _
  696.                sLQuote & "sales_fact_1998" & sRQuote & "." & sLQuote & "product_id" & sRQuote & _
  697.                " = " & _
  698.                sLQuote & "product" & sRQuote & "." & sLQuote & "product_id" & sRQuote & _
  699.             ")"
  700.     Debug.Print "Cube JoinClause =" & dsoCube.JoinClause
  701.     ' save the cube definition in the metadata repository
  702.     On Error GoTo Err_Update
  703.     dsoCube.Update
  704.     ' store the reference to the cube object
  705.     ' so that other methods can use it
  706.     Set m_dsoCube = dsoCube
  707. Exit Sub
  708. Err_Create:
  709.     ' Failed to create the object.
  710.     ' Possible reasons:
  711.     '   - the DSO database object is being locked by another DSO application
  712.     '       a DSO object is locked while AddNew method is executing on
  713.     '       one of its collections (Datasources, MDStores, Dimensions...)
  714.     '   - the metadata repository is unreachable
  715. Err_Update:
  716.     ' Failed to persist the cube definition in the metadata repository
  717.     ' Possible reasons:
  718.     '   - the metadata repository is unreachable
  719.     '       you can see where the metadata repository resides by looking
  720.     '       up the following registry entry:
  721.     '       HKEY_LOCAL_MACHINE\Software\Microsoft\OLAP Server\Server Connection Info
  722.     '           Repository Connection String
  723.     '   - the DSO cube object is being locked by another DSO application
  724.     '       it is not possible for two DSO apps to persist the same object
  725.     '       at the same time
  726.     '       it is not possible to persist a DSO object, it another DSO app
  727.     '       has explicitly locked it
  728.     MsgBox "Create cube Sales failed" & vbCrLf & Err.Description & vbCrLf & Err.Source
  729. End Sub
  730. '   ProcessDatabase - processes the whole DSO database
  731. Public Sub ProcessDatabase()
  732.     On Error GoTo Err_Process
  733.     m_dsoDatabase.Process
  734. Exit Sub
  735. Err_Process:
  736.     ' Processing of the database failes.
  737.     ' Possible reasons:
  738.     '   - OLAP server is not running or unreachable
  739.     '   - connection to the relational data source cannot be established
  740.     '   - cube or dimensions are not properly defined
  741.     '   - another app is processing one of the cubes or dimensions from this
  742.     '     database
  743.     '   - another app has locked one of the cubes or dimensions from this
  744.     '     database
  745.     MsgBox "Design aggregations for Partition failed" & vbCrLf & Err.Description
  746. End Sub
  747. '   WriteEnableCube - enables the Sales cube for write back
  748. '   Enabling a cube for writeback entails creation of a writeback partition.  Writeback
  749. '   partition is a ROLAP partition without any aggregations.  The source table of the
  750. '   writeback partition is called writeback table.  The writeback information is stored
  751. '   in this table.
  752. '   We will create the writeback table in the Foodmart sample database
  753. Public Sub WriteEnableCube()
  754.     ' get the cube's datasource
  755.     Dim dsoDatasource As DSO.DataSource
  756.     Set dsoDatasource = m_dsoCube.DataSources(1)
  757.     ' make sure that we are still connected
  758.     If dsoDatasource.IsConnected = False Then
  759.         MsgBox "Cannot create write enable cube. Connection to the datasource cannot be established."
  760.         Exit Sub
  761.     End If
  762.     ' what will be the writeback table name
  763.     Dim sWriteBackTableName As String
  764.     sWriteBackTableName = "Sample_Writeback_Sales"
  765.     ' get the connection to the datasource
  766.     Dim adoConnection As ADODB.Connection
  767.     Set adoConnection = dsoDatasource.Connection
  768.     ' create the writeback partition
  769.     Dim dsoWBPartition As DSO.MDStore
  770.     Set dsoWBPartition = m_dsoCube.MDStores.AddNew("Writeback")
  771.     ' set the writeback flag
  772.     dsoWBPartition.IsReadWrite = True
  773.     ' set the partition's source table
  774.     dsoWBPartition.SourceTable = dsoDatasource.OpenQuoteChar & sWriteBackTableName & dsoDatasource.CloseQuoteChar
  775.     ' set the partition's storage mode
  776.     ' NOTE: the writeback partition should not have any aggregations
  777.     dsoWBPartition.OlapMode = olapmodeRolap
  778.     ' begin transaction on the datasource connection
  779.     adoConnection.BeginTrans
  780.     ' create the writeback table
  781.     ' and map the partition levels and measures to this table
  782.     On Error GoTo Err_CreateWriteBackTable
  783.     CreateWriteBackTable dsoWBPartition, dsoDatasource, adoConnection, sWriteBackTableName
  784.     ' commit the transaction on the datasource
  785.     ' NOTE: the OLAP server uses a different connection to the
  786.     '       relational database, so we need to commit the
  787.     '       changes here
  788.     adoConnection.CommitTrans
  789.     ' save the writeback partition
  790.     dsoWBPartition.Update
  791.     ' process the write back partition
  792.     On Error GoTo Err_Process
  793.     dsoWBPartition.Process processFull
  794. Exit Sub
  795. Err_CreateWriteBackTable:
  796.     ' rollback transaction on the datasource
  797.     adoConnection.RollbackTrans
  798.     ' remove the writeback partition from the cube
  799.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  800.     MsgBox "Cannot create writeback table. " & Err.Description
  801.     Exit Sub
  802. Err_Process:
  803.     ' remove the writeback table
  804.     adoConnection.Execute "DROP TABLE " & sWriteBackTableName
  805.     ' remove the writeback partition from the cube
  806.     m_dsoCube.MDStores.Remove dsoWBPartition.Name
  807.     MsgBox "Cannot process the writeback partition. " & Err.Description
  808.     Exit Sub
  809. End Sub
  810. '   CreateWriteBackTable - create the writeback table
  811. '   The writeback table must have one column for each measure
  812. '   and level in the cube.  It also has two columns used by
  813. '   the OLAP server for storing auditing information
  814. '   This function creates the table and maps the writeback
  815. '   partition levels and measures to this table
  816. Private Sub CreateWriteBackTable(dsoWBPartition As DSO.MDStore, _
  817.                                  dsoDatasource As DSO.DataSource, _
  818.                                  adoConnection As ADODB.Connection, _
  819.                                  TableName As String)
  820.     ' drop the table if it already exists
  821.     On Error Resume Next
  822.     adoConnection.Execute "DROP TABLE " & TableName
  823.     Err.Clear
  824.     On Error GoTo 0
  825.     ' construct the create table statement
  826.     Dim sCreateTable As String
  827.     sCreateTable = "CREATE TABLE " & TableName & " "
  828.     sCreateTable = sCreateTable & " ( "
  829.     ' create two columns required by the OLAP server
  830.     ' for storing auditing information
  831.     sCreateTable = sCreateTable & _
  832.                     "MS_AUDIT_USER varchar(32), " & _
  833.                     "MS_AUDIT_TIME  DateTime"
  834.     ' create a column for each level
  835.     ' since the column names have to be unique
  836.     '   add the level OrdinalPosition to the level name
  837.     Dim dsoPartDimension As DSO.Dimension
  838.     Dim dsoPartLevel As DSO.Level
  839.     Dim sColumnName As String
  840.     For Each dsoPartDimension In dsoWBPartition.Dimensions
  841.         For Each dsoPartLevel In dsoPartDimension.Levels
  842.             If Not dsoPartLevel.LevelType = levAll And _
  843.                Not dsoPartLevel.IsDisabled And _
  844.                Not dsoPartLevel.SubClassType = sbclsVirtual Then
  845.                 ' get the column name
  846.                 sColumnName = GetColumnName(dsoPartLevel.Name & "_L" & CStr(dsoPartLevel.OrdinalPosition))
  847.                 ' update the create table statement
  848.                 sCreateTable = sCreateTable & ", " & _
  849.                                sColumnName & " " & GetColumnTypeDefinition(dsoPartLevel.ColumnType, dsoPartLevel.ColumnSize)
  850.                 
  851.                 ' map the partition level to this column
  852.                 dsoPartLevel.MemberKeyColumn = dsoWBPartition.SourceTable & _
  853.                                                "." & _
  854.                                                dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  855.             End If
  856.         Next
  857.     Next
  858.     ' create a column for each measure
  859.     ' and map the partition measure to this column
  860.     Dim dsoPartitionMeasure As DSO.Measure
  861.     For Each dsoPartitionMeasure In dsoWBPartition.Measures
  862.         ' get the column name
  863.         sColumnName = GetColumnName(dsoPartitionMeasure.Name)
  864.         ' update the create table statement
  865.         sCreateTable = sCreateTable & ", " & _
  866.                        sColumnName & " " & GetColumnTypeDefinition(dsoPartitionMeasure.SourceColumnType)
  867.         ' map the partition measure to this column
  868.         dsoPartitionMeasure.SourceColumn = dsoWBPartition.SourceTable & _
  869.                                            "." & _
  870.                                            dsoDatasource.OpenQuoteChar & sColumnName & dsoDatasource.CloseQuoteChar
  871.     Next
  872.     ' complete the create table statement
  873.     sCreateTable = sCreateTable & ")"
  874.     ' execute the create table statement
  875.     adoConnection.Execute sCreateTable
  876. End Sub
  877. '   GetColumnTypeDefinition - return a string describing the data type
  878. '                             that can be used in create table statement
  879. '   NOTE: this function works with Jet provider, other providers have different data type names
  880. Private Function GetColumnTypeDefinition(ColumnType As ADODB.DataTypeEnum, Optional ColumnSize As Integer = 50) As String
  881.     Select Case ColumnType
  882.         Case adChar, adVarChar, adWChar
  883.             GetColumnTypeDefinition = " VARCHAR (" & CStr(ColumnSize) & ") "
  884.         Case adCurrency
  885.             GetColumnTypeDefinition = " CURRENCY "
  886.         Case adInteger
  887.             GetColumnTypeDefinition = " INTEGER "
  888.         Case adSmallInt
  889.             GetColumnTypeDefinition = " SMALLINT "
  890.         Case adSingle
  891.             GetColumnTypeDefinition = " REAL "
  892.         Case adDouble
  893.             GetColumnTypeDefinition = " DOUBLE "
  894.         Case adDBTimeStamp, adDate
  895.             GetColumnTypeDefinition = " DATETIME "
  896.         Case Else
  897.             Debug.Assert False ' there is a new data type
  898.                                ' we need to update this function
  899.     End Select
  900. End Function
  901. '   GetColumnName   - create a column name based on the given string
  902. '                     get rid of spaces
  903. Private Function GetColumnName(ObjectName As String) As String
  904.     Dim s As String
  905.     Dim c As String
  906.     Dim i As Integer
  907.     For i = Len(ObjectName) To 1 Step -1
  908.         c = Mid(ObjectName, i, 1)
  909.         If c <> " " Then
  910.             ' legal char, use it
  911.             s = c + s
  912.         End If
  913.     Next
  914.     GetColumnName = s
  915. End Function
  916.