Question : Iterate through Pivot Items from Cube

Hello everybody!

I'm working on an Excel 2007 Addin written in VB.net using VS 2008.

My problem is that I need to find out how to programmatically determine what elements are on a certain level of a hierarchy fetched from a cube.

Assume a simple hierarchy based on locations: Country -> Region -> City

I found out how to get the 3 levels (PivotFields), but not the items contained within them (PivotItem, I guess...).
For example, what I want to do is to find all countries (regions, cities, ...) represented in the hierarchy.

I figured out a bit of code, but it's not delivering the wanted results, because there seem to be no PivotItems contained by the PivotFields. Instead of "United States", "United Kingdom" and "Germany" it just gives me an empty result. (CType(pf.PivotItems, Excel.PivotItems).count = 0)

Is there anyone who can help me? Any push into the right direction is greatly appreciated!
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
Dim prompt As String = ""
Try
    For Each pf As Excel.PivotField In CType(pt.PivotFields, Excel.PivotFields)
        For Each pi As Excel.PivotItem In CType(pf.PivotItems, Excel.PivotItems)
            prompt &= pi.Name & vbCrLf
            ' pi.Value doesn't work as well 
        Next
    Next
Catch ex As Exception
    ' Do nothing
Finally
    MsgBox(prompt)
End Try

Answer : Iterate through Pivot Items from Cube

Just in case someone is interested: I solved the problem without having to use MDX.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
Dim conn As New adomd.AdomdConnection()
 
            conn.ConnectionString = pt.PivotCache.Connection.ToString.Replace("OLEDB;", "") ' The PivotTables ConnectionString started with "OLEDB;" - needs to be sorted out'
 
' [Dimension].[Hierarchy] -> Dimension.Hierarchy'
            Dim str_tmp As String = combox_search_in.Text.Replace("]", "").Replace("[", "")
 
 
            Dim char_tmp As Char() = ".".ToCharArray
 
' Separate the two strings and put them into an array'
            ' 0. Name Dimension '
            ' 1. Name Hierarchy '
            Dim strarr_dim_hier As String() = str_tmp.Split(char_tmp(0))
 
            conn.Open()
 
' Iterate (simplified, filtering removed) '
            For Each cube As adomd.CubeDef In conn.Cubes
                For Each dimension As adomd.Dimension In cube.Dimensions
                    For Each hierarchy As adomd.Hierarchy In dimension.Hierarchies
                        For Each level As adomd.Level In hierarchy.Levels
                            For Each member As adomd.Member In level.GetMembers
                                msgbox(member.name)
                            Next
                        Next
                    Next
                Next
            Next
 
            conn.Close()
Random Solutions  
 
programming4us programming4us