From: Palis on
Hi!

Need som help, Im trying to merge one textfile with computernames and one
list of computers that an account in my AD is aloud to log to.
When I try to Redim in the For Each Loop I receive an error 800a008 The
array is locked. Why?

Dim ForReading
Dim bolTest
Dim fso, objFile
Dim index,strTemp
Dim arrCompFromText()
ForReading = 1
bolTest = False
index = 0

ReDim arrCompFromText(index)

Set fso = CreateObject("Scripting.FileSystemObject")
Set objUser = GetObject("LDAP://cn=mpp,ou=anvandare,dc=hemma,dc=local")

Set objFile = fso.OpenTextFile("Datorer.txt", ForReading, True)

arrCompFromAD = Split(objUser.userWorkstations,",")

Do Until objFile.AtEndOfLine
strTemp = objFile.ReadLine
ReDim Preserve arrCompFromText(index)
arrCompFromText(index) = strTemp
index = index + 1
Loop

For Each compText In arrCompFromText
For Each compAD In arrCompFromAD
If compText = compAD Then
bolTest = True
End If
strTemp = compAD
Next

If Not bolTest Then
ReDim Preserve arrCompFromText(index) // recives an error 800A008 Array is
locked
arrCompFromText(index) = strTemp
index = index + 1
End If

bolTest =False

Next

For Each item In arrCompFromText
strTemp = item
If strResult = "" Then
strResult = strTemp
End If
strResult = strResult & "," & strTemp
Next

WScript.Echo strResult

/palis


From: ekkehard.horner on
Palis schrieb:
> Hi!
>
> Need som help, Im trying to merge one textfile with computernames and one
> list of computers that an account in my AD is aloud to log to.
> When I try to Redim in the For Each Loop I receive an error 800a008 The
> array is locked. Why?
>
> Dim ForReading
> Dim bolTest
> Dim fso, objFile
> Dim index,strTemp
> Dim arrCompFromText()

This line dims/declares an empty fixed array; use "Dim arrCompFromText"
to dim/declare an empty variable that can be ReDimed later.

> ForReading = 1
> bolTest = False
> index = 0
>
> ReDim arrCompFromText(index)

[will fail if "Dim arrCompFromText()" made VBScript to see arrCompFromText
as an array of fixed size]
>
> Set fso = CreateObject("Scripting.FileSystemObject")
> Set objUser = GetObject("LDAP://cn=mpp,ou=anvandare,dc=hemma,dc=local")
>
> Set objFile = fso.OpenTextFile("Datorer.txt", ForReading, True)
>
> arrCompFromAD = Split(objUser.userWorkstations,",")
>
> Do Until objFile.AtEndOfLine
> strTemp = objFile.ReadLine
> ReDim Preserve arrCompFromText(index)
> arrCompFromText(index) = strTemp
> index = index + 1
> Loop
>
> For Each compText In arrCompFromText
> For Each compAD In arrCompFromAD
> If compText = compAD Then
> bolTest = True
> End If
> strTemp = compAD
> Next
>
> If Not bolTest Then
> ReDim Preserve arrCompFromText(index) // recives an error 800A008 Array is
> locked
> arrCompFromText(index) = strTemp
> index = index + 1
> End If
>
> bolTest =False
>
> Next
>
> For Each item In arrCompFromText
> strTemp = item
> If strResult = "" Then
> strResult = strTemp
> End If
> strResult = strResult & "," & strTemp
> Next
>
> WScript.Echo strResult
>
> /palis
>
>
From: Microsoft on
Didn't help.. The first two redim works but not the one in the for each
loop.. is it something Im missing with the scope or with the for each
loop???

"ekkehard.horner" <ekkehard.horner(a)arcor.de> skrev i meddelandet
news:484e6d12$0$6558$9b4e6d93(a)newsspool3.arcor-online.net...
> Palis schrieb:
>> Hi!
>>
>> Need som help, Im trying to merge one textfile with computernames and one
>> list of computers that an account in my AD is aloud to log to.
>> When I try to Redim in the For Each Loop I receive an error 800a008 The
>> array is locked. Why?
>>
>> Dim ForReading
>> Dim bolTest
>> Dim fso, objFile
>> Dim index,strTemp
>> Dim arrCompFromText()
>
> This line dims/declares an empty fixed array; use "Dim arrCompFromText"
> to dim/declare an empty variable that can be ReDimed later.
>
>> ForReading = 1
>> bolTest = False
>> index = 0
>>
>> ReDim arrCompFromText(index)
>
> [will fail if "Dim arrCompFromText()" made VBScript to see arrCompFromText
> as an array of fixed size]
>>
>> Set fso = CreateObject("Scripting.FileSystemObject")
>> Set objUser = GetObject("LDAP://cn=mpp,ou=anvandare,dc=hemma,dc=local")
>>
>> Set objFile = fso.OpenTextFile("Datorer.txt", ForReading, True)
>>
>> arrCompFromAD = Split(objUser.userWorkstations,",")
>>
>> Do Until objFile.AtEndOfLine
>> strTemp = objFile.ReadLine
>> ReDim Preserve arrCompFromText(index)
>> arrCompFromText(index) = strTemp
>> index = index + 1
>> Loop
>>
>> For Each compText In arrCompFromText
>> For Each compAD In arrCompFromAD
>> If compText = compAD Then
>> bolTest = True
>> End If
>> strTemp = compAD
>> Next
>>
>> If Not bolTest Then
>> ReDim Preserve arrCompFromText(index) // recives an error 800A008 Array
>> is locked
>> arrCompFromText(index) = strTemp
>> index = index + 1
>> End If
>>
>> bolTest =False
>>
>> Next
>>
>> For Each item In arrCompFromText
>> strTemp = item
>> If strResult = "" Then
>> strResult = strTemp
>> End If
>> strResult = strResult & "," & strTemp
>> Next
>>
>> WScript.Echo strResult
>>
>> /palis
>>

From: Richard Mueller [MVP] on
You are ReDim'ing the array inside a For Each loop that is enumerating the
array. The For Each loop locks the array.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Microsoft" <palis(a)inbox.com> wrote in message
news:efi80nvyIHA.4864(a)TK2MSFTNGP06.phx.gbl...
> Didn't help.. The first two redim works but not the one in the for each
> loop.. is it something Im missing with the scope or with the for each
> loop???
>
> "ekkehard.horner" <ekkehard.horner(a)arcor.de> skrev i meddelandet
> news:484e6d12$0$6558$9b4e6d93(a)newsspool3.arcor-online.net...
>> Palis schrieb:
>>> Hi!
>>>
>>> Need som help, Im trying to merge one textfile with computernames and
>>> one list of computers that an account in my AD is aloud to log to.
>>> When I try to Redim in the For Each Loop I receive an error 800a008 The
>>> array is locked. Why?
>>>
>>> Dim ForReading
>>> Dim bolTest
>>> Dim fso, objFile
>>> Dim index,strTemp
>>> Dim arrCompFromText()
>>
>> This line dims/declares an empty fixed array; use "Dim arrCompFromText"
>> to dim/declare an empty variable that can be ReDimed later.
>>
>>> ForReading = 1
>>> bolTest = False
>>> index = 0
>>>
>>> ReDim arrCompFromText(index)
>>
>> [will fail if "Dim arrCompFromText()" made VBScript to see
>> arrCompFromText
>> as an array of fixed size]
>>>
>>> Set fso = CreateObject("Scripting.FileSystemObject")
>>> Set objUser = GetObject("LDAP://cn=mpp,ou=anvandare,dc=hemma,dc=local")
>>>
>>> Set objFile = fso.OpenTextFile("Datorer.txt", ForReading, True)
>>>
>>> arrCompFromAD = Split(objUser.userWorkstations,",")
>>>
>>> Do Until objFile.AtEndOfLine
>>> strTemp = objFile.ReadLine
>>> ReDim Preserve arrCompFromText(index)
>>> arrCompFromText(index) = strTemp
>>> index = index + 1
>>> Loop
>>>
>>> For Each compText In arrCompFromText
>>> For Each compAD In arrCompFromAD
>>> If compText = compAD Then
>>> bolTest = True
>>> End If
>>> strTemp = compAD
>>> Next
>>>
>>> If Not bolTest Then
>>> ReDim Preserve arrCompFromText(index) // recives an error 800A008
>>> Array is locked
>>> arrCompFromText(index) = strTemp
>>> index = index + 1
>>> End If
>>>
>>> bolTest =False
>>>
>>> Next
>>>
>>> For Each item In arrCompFromText
>>> strTemp = item
>>> If strResult = "" Then
>>> strResult = strTemp
>>> End If
>>> strResult = strResult & "," & strTemp
>>> Next
>>>
>>> WScript.Echo strResult
>>>
>>> /palis
>>>
>


From: ekkehard.horner on
Microsoft schrieb:
> Didn't help.. The first two redim works but not the one in the for each
> loop.. is it something Im missing with the scope or with the for each
> loop???
[...]
Sorry about misleading you. Let my try again: I hope that this

Dim arrCompFromText : arrCompFromText = Array( "line 1", "line 2" )
Dim sLine
For Each sLine In arrCompFromText
WScript.Echo sLine
If sLine = "line 2" Then
On Error Resume Next
ReDim Preserve arrCompFromText( UBound( arrCompFromText ) + 1 )
WScript.Echo Err.Number, Err.Description
On Error GoTo 0
End If
Next

is a correct simple model of your more complicated code: while using
For Each on the array, you try to insert another element into it. For this
I get the output:

line 1
line 2
10 Array ist unveränderlich oder momentan gesperrt.
[array can't be modified or is locked]

That's reasonable because the code needs the array to iterate over it.

Changing the loop to

Dim nIdx
For nIdx = 0 To UBound( arrCompFromText )
sLine = arrCompFromText( nIdx )
WScript.Echo sLine
If sLine = "line 2" Then
ReDim Preserve arrCompFromText( UBound( arrCompFromText ) + 1 )
arrCompFromText( UBound( arrCompFromText ) ) = "line 3"
End If
Next
WScript.Echo "arrCompFromText:", Join( arrCompFromText, ", " )

results in:

line 1
line 2
arrCompFromText: line 1, line 2, line 3

Now the loop code doesn't need/see arrCompFromText and the appending
succeeds.

I hope you can use this in your code.