Need help substituting variables in loop

J

Jeff

I am trying to substitute a palceholde "holder" with variables "pjTask",
pjResources, "pjResourceEnterprise" everytime the loop iterates through the
loop.

when I use the Locals window my variables seem to be right even tjhough I
get an error during the first loop at the "If
ts(it).GetField(FieldNameToFieldConstant(myType & i, holder)) <> "" Then"
line of code.

================================================
Sub Test()
Dim mycheck As Boolean
Dim myType, usedfields As String
Dim T As task
Dim ts As Tasks
Dim i, it As Integer
Dim holder As String
Dim Testing As Integer


Set ts = ActiveProject.Tasks
usedfields = "Custom Fields used in this file" & vbCrLf

For Testing = 1 To 3
If Testing = 1 Then holder = pjTask
If Testing = 2 Then holder = pjResource
If Testing = 3 Then holder = pjResourceEnterprise


myType = "Text"
usedfields = usedfields & vbCrLf & "--" & UCase(myType) & "--" &
vbCrLf
For i = 1 To 30
mycheck = False
it = 0
While Not mycheck And (it < ts.Count)
it = it + 1
If Not ts(it) Is Nothing Then
If ts(it).GetField(FieldNameToFieldConstant(myType & i,
holder)) <> "" Then
usedfields = usedfields & myType & CStr(i) & vbCr
mycheck = True
End If
End If
Wend
Next i

myType = "Number"
usedfields = usedfields & vbCrLf & "--" & UCase(myType) & "--" &
vbCrLf
For i = 1 To 20
mycheck = False
it = 0
While Not mycheck And (it < ts.Count)
it = it + 1
If Not ts(it) Is Nothing Then
If ts(it).GetField(FieldNameToFieldConstant(myType & i,
holder)) <> 0 Then
usedfields = usedfields & myType & CStr(i) & vbCr
mycheck = True
End If
End If
Wend
Next i
Next Testing


MsgBox usedfields
End Sub
 
J

John

I am trying to substitute a palceholde "holder" with variables "pjTask",
pjResources, "pjResourceEnterprise" everytime the loop iterates through the
loop.

when I use the Locals window my variables seem to be right even tjhough I
get an error during the first loop at the "If
ts(it).GetField(FieldNameToFieldConstant(myType & i, holder)) <> "" Then"
line of code.

================================================
Sub Test()
Dim mycheck As Boolean
Dim myType, usedfields As String
Dim T As task
Dim ts As Tasks
Dim i, it As Integer
Dim holder As String
Dim Testing As Integer


Set ts = ActiveProject.Tasks
usedfields = "Custom Fields used in this file" & vbCrLf

For Testing = 1 To 3
If Testing = 1 Then holder = pjTask
If Testing = 2 Then holder = pjResource
If Testing = 3 Then holder = pjResourceEnterprise


myType = "Text"
usedfields = usedfields & vbCrLf & "--" & UCase(myType) & "--" &
vbCrLf
For i = 1 To 30
mycheck = False
it = 0
While Not mycheck And (it < ts.Count)
it = it + 1
If Not ts(it) Is Nothing Then
If ts(it).GetField(FieldNameToFieldConstant(myType & i,
holder)) <> "" Then
usedfields = usedfields & myType & CStr(i) & vbCr
mycheck = True
End If
End If
Wend
Next i

myType = "Number"
usedfields = usedfields & vbCrLf & "--" & UCase(myType) & "--" &
vbCrLf
For i = 1 To 20
mycheck = False
it = 0
While Not mycheck And (it < ts.Count)
it = it + 1
If Not ts(it) Is Nothing Then
If ts(it).GetField(FieldNameToFieldConstant(myType & i,
holder)) <> 0 Then
usedfields = usedfields & myType & CStr(i) & vbCr
mycheck = True
End If
End If
Wend
Next i
Next Testing


MsgBox usedfields
End Sub

Jeff,
What error message do you get?

John
 
J

Jeff

John, I get an error" Runtime Error 1101" and "The argument value is
not valid" when I try to run the software.

I assume that my variables are not properly defined but I'm not sure
how else to do this.

(This is my first foray into programming, so I'm not sure about a lot
of things)

JEff
 
J

John

John, I get an error" Runtime Error 1101" and "The argument value is
not valid" when I try to run the software.

I assume that my variables are not properly defined but I'm not sure
how else to do this.

(This is my first foray into programming, so I'm not sure about a lot
of things)

JEff

Jeff,
The first thing that I see is an improper declaration. The variable
"holder" should be declared as a long, not as a string. However I ran
your code just to see if I could replicate the error and I did NOT get
an error although I should have with the incorrect declaration.
Apparently the compiler is a little more flexible than it is sometimes.

I did get a runtime error when the "Testing" variable was 2. That is
because the code is trying to get a resource field (i.e. pjResource)
from a task object (i.e. ts(i)). I suspect the error occurred during the
second iteration of the first "For" loop and not on the first.

John
 

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

Top