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
DistArrayElementsto -1. Make sure that you start your macro withOption Base 0if you want the arrays to be 0-based.assetListBox.Items.Count-1I think the count is 1-based.