multi dimensional array problem with redim

  • Thread starter Thread starter tw
  • Start date Start date
T

tw

the array is dimmed at the beginning of the form code like this
'***********************
Option Compare Database
Option Explicit
Dim arrUsers() As String
Dim arrGroups() As String
Dim arrGroupMembership As Variant
'***************************

the vaiables arrUsers() and arrGroups() are filled in the on load function
the function below, cmbUsers_Change(), is a combo box with the data from
arrUsers() as the rowsource for the valuelist. Upon change of a user I want
to fill two list boxes one with the groups that the user is a member of, the
other with the groups that the user is not a member of. In the two
dimensional array arrGroupMembership the first column of the array is the
group name the second column of the array is -1 if the user is a member and
0 if the user is not currently a member. The first iteration of the for
each loop works fine. Then during the second iteration of the for each loop
I get the message subscript out of range when trying to redim the array.
What am I doing wrong?

Private Sub cmbUsers_Change()
'initialize the array arrGroupMembership
'initialize the list boxes
Dim xloop
Dim xloopCount
Dim strMemberRowSource As String
Dim strAvailableRowSource As String

strMemberRowSource = ""
strAvailableRowSource = ""

xloopCount = 0
ReDim arrGroupMembership(xloopCount, 1)
For Each xloop In arrGroups

If xloopCount > 0 Then ReDim Preserve arrGroupMembership(xloopCount,
1)
'SUBSCRIPT OUT OF RANGE OCCURS on REDIM above during second iteration of
loop

arrGroupMembership(xloopCount, 0) = arrGroups(xloopCount)
If IsUserInGroup(arrGroups(xloopCount), Me.cmbUsers.Value) Then
arrGroupMembership(xloopCount, 1) = -1
If Len(strMemberRowSource) > 0 Then
strMemberRowSource = strMemberRowSource & "; " &
arrGroups(xloopCount)
Else
strMemberRowSource = arrGroups(xloopCount)
End If
Else
arrGroupMembership(xloopCount, 1) = 0
If Len(strAvailableRowSource) > 0 Then
strAvailableRowSource = strAvailableRowSource & "; " &
arrGroups(xloopCount)
Else
strAvailableRowSource = arrGroups(xloopCount)
End If
End If
xloopCount = xloopCount + 1
Next

Me.lstAvailable.RowSource = strAvailableRowSource
Me.lstMembership.RowSource = strMemberRowSource
Me.Refresh
End Sub
 
If you use the Preserve keyword, you can only resize the last array
dimension.
 
Ok, I changed to check the size of arrGroups and assign arrGroupMembership
the same number of rows and 2 columns. But I still question the fact that I
can't redim the number of rows. It makes more sense not to know the number
of rows you are going to have in an array than not to know the number of
columns you are going to have. Am I wrong in that assessment? In most of
my programs (vfp) I will usually always know the number of columns I'm going
to have but not always the number of rows.
 
The array just has dimensions and doesn't care which you think of as
rows and which as columns. So instead of storing N rows and 2 columns in
ary(N-1, 1) store them in ary(1, N-1).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top