0

I am trying to get members from AD in a DL and add those members into an array while also displaying them in a listbox. My issue is the array is saying there are 8 elements in the array, but the list box shows 7. So when counting from 0 from whats in the listbox, it should say 6, not 8. Also, when the objGroup.Members changes due to the distGroup I'm returning, the array also stays at 8, when it should be more. Can anyone help me? I'm not a VB guru.

Dim distGroup As String
Dim asset As String
Dim distArray() As String
Dim distArrayElements As Integer = 0

 Sub getMembers()

    Dim objGroup, objUser, objFSO, strDomain
    assetListBox.Items.Clear()

    If distGroup = Nothing Then
        Exit Sub
    End If

    'Change DomainName to the name of the domain the group is in
    strDomain = "Mydomain.com"

    objGroup = GetObject("LDAP://CN=" & distGroup & ",OU=Loaners,OU=Accounts,DC=myDomain,DC=com")

    For Each objUser In objGroup.Members

        assetListBox.Items.Insert(0, objUser.displayName)
        assetListBox.Sorted = True
        asset = assetListBox.Items.Item(0)
        If distArrayElements > assetListBox.Items.Count Then
            distArrayElements = Nothing
        End If
    Next
    Dim i As Integer
    For i = 0 To assetListBox.Items.Count
        AddElementToStringArray("assetListBox.Items " & i)
    Next
    MsgBox("Number of elements in array is: " & distArray.Length & vbCrLf & vbCrLf & "Asset: " & asset)
End Sub

Public Sub AddElementToStringArray(ByVal stringToAdd As String)
    ReDim Preserve distArray(distArrayElements)
    distArray(distArrayElements) = stringToAdd
    distArrayElements += 1
End Sub
7
  • Try setting DistArrayElements to -1. Make sure that you start your macro with Option Base 0 if you want the arrays to be 0-based. Commented Jan 26, 2015 at 18:19
  • If I do that I get, Index was outside the bounds of the array. it doesn't have to start at zero... Commented Jan 26, 2015 at 19:03
  • 1
    Try this assetListBox.Items.Count-1 I think the count is 1-based. Commented Jan 26, 2015 at 19:08
  • Now it equals 7 on the first distribution list which is correct, I have 2 more that have 10 and 9 members. Those are not correct. Commented Jan 26, 2015 at 19:12
  • Ok, that works, I moved my IF statement outside the loop. Edit above. Commented Jan 26, 2015 at 19:30

1 Answer 1

1

Suggested subtracting 1 off assetListBox.Items.Count

Dim distGroup As String
Dim asset As String
Dim distArray() As String
Dim distArrayElements As Integer = 0

 Sub getMembers()

    Dim objGroup, objUser, objFSO, strDomain
    assetListBox.Items.Clear()

    If distGroup = Nothing Then
        Exit Sub
    End If

    'Change DomainName to the name of the domain the group is in
    strDomain = "Mydomain.com"

    objGroup = GetObject("LDAP://CN=" & distGroup & ",OU=Loaners,OU=Accounts,DC=myDomain,DC=com")

    For Each objUser In objGroup.Members

        assetListBox.Items.Insert(0, objUser.displayName)
        assetListBox.Sorted = True
        asset = assetListBox.Items.Item(0)
        If distArrayElements > assetListBox.Items.Count Then
            distArrayElements = Nothing
        End If
    Next
    Dim i As Integer
    For i = 0 To assetListBox.Items.Count - 1
        AddElementToStringArray("assetListBox.Items " & i)
    Next
    MsgBox("Number of elements in array is: " & distArray.Length & vbCrLf & vbCrLf & "Asset: " & asset)
End Sub

Public Sub AddElementToStringArray(ByVal stringToAdd As String)
    ReDim Preserve distArray(distArrayElements)
    distArray(distArrayElements) = stringToAdd
    distArrayElements += 1
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.