cancel
Showing results for 
Search instead for 
Did you mean: 

Bad index type for collection access ???

Former Member
0 Kudos

Hi there,

I am writing a script in VBA to connect to SAP.

I encountered something strange.

Why does this work:

Set oSession = oConnection.Children(0)

But this isn't:

X = 0

Set oSession = oConnection.Children(X)

It results in an error: "Bad index type for collection access"

Regards, Bas Prins

Accepted Solutions (1)

Accepted Solutions (1)

former_member583013
Active Contributor
0 Kudos

Maybe you need to do this...


DIM X AS INTEGER = 0
Set oSession = oConnection.Children(X)

Greetings,

Blag.

Former Member
0 Kudos

Thanks,

Although that is not proper VBA syntax I understand your suggestion.

In VBA that would be:


DIM X AS INTEGER
X = 0
Set oSession = oConnection.Children(X)

But I tried several datatypes, all resulted in error.

Regards, Bas.

Answers (2)

Answers (2)

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.

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.

DIM X AS LONG
X=0

Set oSession = oConnection.Children(Clng(X))
Sandra_Rossi
Active Contributor

To extend the good answer, the following works (tests in VBA Office 64 bits with SAP GUI 7.60):

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)

This works also for a fixed number:

Set oSession = oConnection.Children(0)
Set oSession = oConnection.Children.ElementAt(0)
Set oSession = oConnection.Children.Item(0)

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"):

Set oSession = oConnection.Children.Item(x)

Note that I don't have any issue with VBScript, everything works fine.

BrianTran
Explorer
0 Kudos

This really works with me: oConnection.Children((x))

Thanks so much for your sharing!

Former Member
0 Kudos

Use a calculation to determine the index.

Example:

Dim X As Integer, oGuiApplication As Object, oConnection As Object, Session As Object

X = 0

Set Session = oConnection.children(0 + X)

- or -

Set oConnection = oGuiApplication.children(oGuiApplication.Connections.Count - 1)

Thanks,

Cas