<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>Question Re: Bad index type for collection access ??? in Additional Q&amp;A</title>
    <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466763#M21716</link>
    <description>&lt;P&gt;To extend the good answer, the following works (tests in VBA Office 64 bits with SAP GUI 7.60):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children((x)) ' Pass by value instead of passing by reference
Set oSession = oConnection.Children(CInt(x))
Set oSession = oConnection.Children(CLng(x))
Set oSession = oConnection.Children.ElementAt(x)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This works also for a fixed number:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children(0)
Set oSession = oConnection.Children.ElementAt(0)
Set oSession = oConnection.Children.Item(0)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This doesn't work ("Run-time error '618': Bad index type for collection access" or "Run-time error '618': Application-defined or object-defined error"):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children.Item(x)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that I don't have any  issue with &lt;STRONG&gt;VBScript&lt;/STRONG&gt;, everything works fine.&lt;/P&gt;</description>
    <pubDate>Mon, 09 Aug 2021 14:41:57 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2021-08-09T14:41:57Z</dc:date>
    <item>
      <title>Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaq-p/3466758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am writing a script in VBA to connect to SAP.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I encountered something strange.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Why does this work:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Set oSession = oConnection.Children(0)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But this isn't:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;X = 0&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Set oSession = oConnection.Children(X)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It results in an error: "Bad index type for collection access"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, Bas Prins&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2008 13:28:16 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaq-p/3466758</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-02-26T13:28:16Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466759#M21712</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Maybe you need to do this...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DIM X AS INTEGER = 0
Set oSession = oConnection.Children(X)
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Greetings,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Blag.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2008 13:53:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466759#M21712</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-02-26T13:53:19Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466760#M21713</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Although that is not proper VBA syntax I understand your suggestion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In VBA that would be:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;
DIM X AS INTEGER
X = 0
Set oSession = oConnection.Children(X)
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I tried several datatypes, all resulted in error.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards, Bas.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Feb 2008 14:07:01 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466760#M21713</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-02-26T14:07:01Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466761#M21714</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use a calculation to determine the index.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Example:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    Dim X As Integer, oGuiApplication As Object, oConnection As Object, Session As Object&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    X = 0&lt;/P&gt;&lt;P&gt;    Set Session = oConnection.children(0 + X)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; - or -&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;    Set oConnection = oGuiApplication.children(oGuiApplication.Connections.Count - 1)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Cas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 01 Apr 2008 12:46:46 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466761#M21714</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2008-04-01T12:46:46Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466762#M21715</link>
      <description>&lt;P&gt;Sorry to revive this old thread. I came across a similar issue today and found an (odd) solution, which I'm sharing in case it can help anyone else in the future. &lt;/P&gt;&lt;P&gt;The collection index needs to be a LONG, not an INTEGER. For some mysterious reason even if you declare it as a LONG it still won't work. But if you also cast it to a long, then it works.&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;DIM X AS LONG
X=0

Set oSession = oConnection.Children(Clng(X))
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jun 2020 09:17:12 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466762#M21715</guid>
      <dc:creator>former_member1223202</dc:creator>
      <dc:date>2020-06-11T09:17:12Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466763#M21716</link>
      <description>&lt;P&gt;To extend the good answer, the following works (tests in VBA Office 64 bits with SAP GUI 7.60):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children((x)) ' Pass by value instead of passing by reference
Set oSession = oConnection.Children(CInt(x))
Set oSession = oConnection.Children(CLng(x))
Set oSession = oConnection.Children.ElementAt(x)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This works also for a fixed number:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children(0)
Set oSession = oConnection.Children.ElementAt(0)
Set oSession = oConnection.Children.Item(0)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This doesn't work ("Run-time error '618': Bad index type for collection access" or "Run-time error '618': Application-defined or object-defined error"):&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Set oSession = oConnection.Children.Item(x)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Note that I don't have any  issue with &lt;STRONG&gt;VBScript&lt;/STRONG&gt;, everything works fine.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Aug 2021 14:41:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466763#M21716</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2021-08-09T14:41:57Z</dc:date>
    </item>
    <item>
      <title>Re: Bad index type for collection access ???</title>
      <link>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466764#M21717</link>
      <description>&lt;P&gt;This really works with me: oConnection.Children((x))&lt;/P&gt;&lt;P&gt;Thanks so much for your sharing!&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2023 06:36:43 GMT</pubDate>
      <guid>https://community.sap.com/t5/additional-q-a/bad-index-type-for-collection-access/qaa-p/3466764#M21717</guid>
      <dc:creator>BrianTran</dc:creator>
      <dc:date>2023-10-10T06:36:43Z</dc:date>
    </item>
  </channel>
</rss>

