Help with reading / retrieving from arrays

I have inherited an application that has a button on a form that is supposed to read through a series of fields and if it finds a match, return the value in a matching field.

For X = 1 To Ubound( doc.Technician )

xstring = Trim(Str(x))

If “doc.Group” & xstring & “(0)” = “2” Then

  doc2.Technician(x-1) = "doc.Tech" & xstring & "(0)"

End If

Next

doc and doc2 are dim’ed above this piece of code. I need to get a count from a field and using that index walk through a seperate field, comparing the value to a string. If the string matches, I then need to get the value in a different field using the same index.

Two problems are occuring. The first is that the IF statement is not finding the value “2” even though it is using the string doc.Group1(0) where 1 is the index. The second is that I am not sure how to redim the array if I get more than 1 hit - which is what I have been told is the actual problem.

Any ideas?

Subject: Try:

Redim d2Tech(Ubound(doc.Technician)For x = 1 To Ubound(doc.Technician) + 1 'Make sure you process the last value

xstring = Cstr(x)

iv = doc.GetItemValue(“Group” + xstring)(0)

If iv = “2” Then

  d2Tech(x-1) = doc.GetItemValue("Tech" + xstring)(0)

End If

Next

doc2.Technician = d2Tech

Subject: Array is not your biggest problem here

If “doc.Group” & xstring & “(0)” = “2” Thendoc2.Technician(x-1) = “doc.Tech” & xstring & “(0)”

Try using something like this:

Dim fieldName as string

fieldName = “Group” & xstring

myItem = doc.GetFirstItem(fieldName)

doc2.Technician(x-1) = myItem.Values(0)

Hope this helps.

Jason

Added, this is just to show a way of thinking about the field name issue and to show you that you can’t use doc.Part & “ofAName” & “(0)”

Not complete code. You would check to see if myItem is nothing and that it has a value instead of just referencing (0) blindly as well.

Subject: RE: Array is not your biggest problem here

Illeagal USE of PROPERTY on the doc2.Technician(x-1) = myItem.Values(0) line

Any other ideas?

Subject: Help with reading / retrieving from arrays

Thanks to all for the ideas and code. I got it to work.

Subject: Help with reading / retrieving from arrays

I suppose you want to do something like that:

X = 1 To Ubound( doc.Technician )

xstring = Trim(Str(x))

If doc.hasitem(“Group” + xstring) then

If doc.GetFirstItem(“Group” + xstring)(0) = “2” Then

doc2.Technician(x-1) = doc.GetFirstItem(“Group” + xstring)(0)

End If

End if

Next

“doc.Group” means that you are looking for the string “doc.Group” and not the value in doc.Group…

Subject: RE: Help with reading / retrieving from arrays

Close. I rewrote it slightly and now have an issue with the doc2.Technician(x-1) = Item2.text line.

For X = 1 To Ubound( doc.Technician )

	xstring = Trim(Str(x))

	If doc.hasitem("Group" + xstring) Then

		Set Item = doc.GetFirstItem("Group" + xstring) 

		If Item.Text = "2" Then

			Set Item2 = doc.GetFirstItem("Tech" + xstring)

			doc2.Technician(x-1) = Item2.text

		End If

	End If

Next

Illegal Use of Property ??

Subject: RE: Help with reading / retrieving from arrays

Basically you can’t write to a filed in that way. What you’ll have to do is create an array first and then write the array to the field after the loop:

dim technicianarray(ubound(doc.technician)) as string

//Then instead of the line giving you a problem

technicianarray(x-1) = item2.text

//Then after the loop

do2.Technician = technicianarray

hth

Dan