An issue with API function

Post Reply
kiss351523
Posts: 51
Joined: Thu May 05, 2011 2:05 am
OLAP Product: TM1
Version: 9.5
Excel Version: 2007 and 2003
Location: Asia

An issue with API function

Post by kiss351523 »

Deal All:
My codes'purpose is get an element list from the dimension given a name,the "eCount" variable is the number of elements from dimension given a name.
when give it an empty dimension,the "eCount" 's value is 91,why not 0?

Code: Select all

Public Sub listElements(sServerName, sDimensionName)
    Dim hUser As Long
    Dim hPool As Long
    Dim hServerHandle As Long
    Dim hDim As Long
    Dim eHCount As Long
    Dim sErr As String * 100
    Dim eCount As Integer
    Dim i As Integer
    
    hUser = TM1_API2HAN()
    
    If hUser = 0 Then
        MsgBox "No Connect to API!"
    End If
    
    hServerHandle = TM1SystemServerHandle(hUser, sServerName)
    
    If hServerHandle = 0 Then
        MsgBox "No Connect to Server"
    End If
    
    hPool = TM1ValPoolCreate(hUser)
    
    hDim = TM1ObjectListHandleByNameGet(hPool, hServerHandle, TM1ServerDimensions(), TM1ValString(hPool, sDimensionName, 0))
    
    If TM1ValObjectType(hUser, hDim) = TM1ValTypeError() Then
      TM1ValStringGet_VB hUser, hDim, sErr, 100
      MsgBox sErr
      TM1ValPoolDestroy (hPool)
      Exit Sub
    End If
    
    eHCount = TM1ObjectListCountGet(hPool, hDim, TM1DimensionElements())
    
    eCount = TM1ValIndexGet(hUser, eHCount)
    
    MsgBox eCount
    
    TM1ValPoolDestroy (hPool)
    
    'For i = 1 To eCount
      'TM1ObjectListHandleByIndexGet(hpool,hdim,tm1dimensionelements(),i)
        
     ' i = i + 1
    'Next
            
End Sub
rmackenzie
MVP
Posts: 733
Joined: Wed May 14, 2008 11:06 pm

Re: An issue with API function

Post by rmackenzie »

You should check the TM1ValType of eHCount before you pass it to TM1ValIndexGet in case for some reason it isn't an index value - this might explain why TM1ValIndexGet isn't returning 0.
Robin Mackenzie
kiss351523
Posts: 51
Joined: Thu May 05, 2011 2:05 am
OLAP Product: TM1
Version: 9.5
Excel Version: 2007 and 2003
Location: Asia

Re: An issue with API function

Post by kiss351523 »

rmackenzie wrote:You should check the TM1ValType of eHCount before you pass it to TM1ValIndexGet in case for some reason it isn't an index value - this might explain why TM1ValIndexGet isn't returning 0.
Then,How to check the eHCount,could you give me some codes?
rmackenzie
MVP
Posts: 733
Joined: Wed May 14, 2008 11:06 pm

Re: An issue with API function

Post by rmackenzie »

kiss351523 wrote:How to check the eHCount,could you give me some codes?
I take it then, that you didn't write the code that you posted originally?

To check the value type of eHCount:

Code: Select all

Dim intCheckType As Integer
intCheckType = TM1ValType(hUser, eHCheck)
Debug.Print intCheckType
Robin Mackenzie
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: An issue with API function

Post by Alan Kirk »

kiss351523 wrote:
rmackenzie wrote:You should check the TM1ValType of eHCount before you pass it to TM1ValIndexGet in case for some reason it isn't an index value - this might explain why TM1ValIndexGet isn't returning 0.
Then,How to check the eHCount,could you give me some codes?
Download TM1 Tools. Look in the bas_TM1APIUtilities module for the CheckValueCapsuleType and related functions that I wrote. There are demonstrations of its use in functions like the GetClientsCollection one.

By the way, you may want to take a closer look at the Ifs that you're using in your code. If the handle that you're testing returns zero then you're showing a message box, but then happily continuing on trying to execute the rest of the code. That's a pretty much guaranteed way to crash Excel eventually.
"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
Alan Kirk
Site Admin
Posts: 6667
Joined: Sun May 11, 2008 2:30 am
OLAP Product: TM1
Version: PA2.0.9.18 Classic NO PAW!
Excel Version: 2013 and Office 365
Location: Sydney, Australia
Contact:

Re: An issue with API function

Post by Alan Kirk »

Incidentally, I'm working off the assumption that you're trying to learn how to work with the API. However as there is a school of thought that it should be de rigueur to second guess what people ask, I'll mention, for the sake of completeness, that the specific instance that you have here can be done in a much more straightforward fashion using good ol' TM1 macro functions, like so:

Code: Select all

Sub ListElements(ServerName As String, DimensionName As String)

Dim s_FullDimName As String
Dim l_DimSiz As Long, l_Idx As Long

On Error GoTo ExitPoint

s_FullDimName = ServerName & ":" & DimensionName

l_DimSiz = Application.Run("DimSiz", s_FullDimName)

If l_DimSiz = 0 Then

    Debug.Print s_FullDimName & " has no elements."

Else

    Debug.Print s_FullDimName & " has " & _
     l_DimSiz & " element" & IIf(l_DimSiz = 1, ".", "s.")

    For l_Idx = 1 To l_DimSiz
    
        Debug.Print Application.Run("DimNm", s_FullDimName, l_Idx)
    
    Next

End If

ExitPoint:

End Sub

"To them, equipment failure is terrifying. To me, it’s 'Tuesday.' "
-----------
Before posting, please check the documentation, the FAQ, the Search function and FOR THE LOVE OF GLUB the Request Guidelines.
kiss351523
Posts: 51
Joined: Thu May 05, 2011 2:05 am
OLAP Product: TM1
Version: 9.5
Excel Version: 2007 and 2003
Location: Asia

Re: An issue with API function

Post by kiss351523 »

Alan Kirk wrote:Incidentally, I'm working off the assumption that you're trying to learn how to work with the API. However as there are some around who regard it as de rigueur to second guess what people ask I'll mention, for the sake of completeness, that the specific instance that you have here can be done in a much more straightforward fashion using good ol' TM1 macro functions, like so:
um! Great, this is really a good thing, you nice!Tks!!!
Post Reply