Page 1 of 1

An issue with API function

Posted: Wed Dec 14, 2011 12:51 am
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

Re: An issue with API function

Posted: Wed Dec 14, 2011 2:23 am
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.

Re: An issue with API function

Posted: Wed Dec 14, 2011 2:52 am
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?

Re: An issue with API function

Posted: Wed Dec 14, 2011 3:02 am
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

Re: An issue with API function

Posted: Wed Dec 14, 2011 3:03 am
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.

Re: An issue with API function

Posted: Wed Dec 14, 2011 3:19 am
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


Re: An issue with API function

Posted: Wed Dec 14, 2011 3:30 am
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!!!