PS 2007 SetField fails

M

mike.mahoney

Hi

I am trying to capture actual hours from last week and store in an
entereprise field, but no matter how I phrase the setfield method I
get an error. Code is below - any suggestions greatly appreciated.
Note Enterprise field LastWeekAct is defined as type Duration

Thanks

Mike

Sub Snapshot()
Dim LastWeekAct As String
Dim FieldRef As String

Dim Tsk As Task
For Each Tsk In ActiveProject.Tasks
If Not Tsk Is Nothing Then ' tests for blank row

LastWeekAct = Tsk.GetField(FieldID:=pjTaskActualWork)
FieldRef = FieldNameToFieldConstant(FieldName:="LastWeekAct",
FieldType:=pjTask)

' Tsk.SetField pjTaskBaseline9Work, LastWeekAct - This line works
' Tsk.SetField
FieldID:=FieldNameToFieldConstant(FieldName:="LastWeekAct",
FieldType:=pjTask), Value:=LastWeekAct _ ' This line fails

' Next line also fails - Run time error 1101 - Argument value is
not valid
Tsk.SetField FieldRef, LastWeekAct

End If
Next Tsk
End Sub
 
J

Jack Dahlgren

The underscore at the end of your value _ is the line continuation character
which concatenates the line with the next one. It is no wonder it fails. Try
using quotes around the value if that really is the value you want to set.

-Jack Dahlgren
 
M

mike.mahoney

The underscore at the end of your value _ is the line continuation character
which concatenates the line with the next one. It is no wonder it fails. Try
using quotes around the value if that really is the value you want to set.

-Jack Dahlgren












- Show quoted text -

Jack

The underscore is just a typo. The line fails with the same error as
the current final line. Th variable "LastWeekAct" simply hold a
duration value (e.g. 6 days). i was wondering whether I had to
convert string to duration before passing via setfield.

regards

Mike
 
R

Rod Gill

Hi,

Is LastWeekAct an Enterprise field in Proejct Server 2007? If so, then you
need to convert the field name to an index number for use with GetField and
SetField. EG:
Tsk.SetField FieldNameToFieldConstant("LastWeekAct"), cLng(LastWeekAct)

Concert to Long type if LastWeekAct is a numeric field
If you are not using Project Server, then why not just use the underlying
custom field name. So if LastWeekAct is custom field Number1 then use:
Tsk.Number1=LastWeekAct

--

Rod Gill
Project MVP

Project VBA Book, for details visit:
http://www.projectvbabook.com

NEW!! Web based VBA training course delivered by me. For details visit:
http://projectservertraining.com/learning/index.aspx
 
J

Jack Dahlgren

Mike,

It is hard to troubleshoot when the code has typos. I find it best to cut
and paste the actual code.
If you are passing a string to a date field you may have a problem if the
string is not properly formed.
It is always good practice to be explicit about type. I know that VBA lets
you get away with being loose with type, but it has helped me in the past to
be explicit in declaring variables. The time saved in debugging outweighs
the few seconds up front.

-Jack Dahlgren
 
M

mike.mahoney

Mike,

It is hard to troubleshoot when the code has typos. I find it best to cut
and paste the actual code.
If you are passing a string to a date field you may have a problem if the
string is not properly formed.
It is always good practice to be explicit about type. I know that VBA lets
you get away with being loose with type, but it has helped me in the past to
be explicit in declaring variables. The time saved in debugging outweighs
the few seconds up front.

-Jack Dahlgren





- Show quoted text -

Chaps

Its been a while since I played with macros. however I discovered the
same syntax for a local field does not work with an enterprise field.
So will pick up on Rod's suggestion tomorrow (past my bedtime)

Thanks

Mike
 
M

mike.mahoney

Chaps

Its been a while since I played with macros. however I discovered the
same syntax for a local field does not work with an enterprise field.
So will pick up on Rod's suggestion tomorrow (past my bedtime)

Thanks

Mike- Hide quoted text -

- Show quoted text -

Ok Problem solved. I am passing values to enterprise fields rather
than local fields and I have reasons for doing that. The syntax below
works for different field types. Howver it fails if the definition of
the numeric field (number or duration) specifies a rollup mechanism
other than None (Calculation of summary rows). Possibly the syntax
below could be set to exclude summary rows, but no longer an issue for
me.

Thanks

Sub Snapshot()

Dim vLastWeekWork As String
Dim Tsk As Task

For Each Tsk In ActiveProject.Tasks
If Not Tsk Is Nothing Then ' tests for blank row
vLastWeekWork = Tsk.GetField(FieldID:=pjTaskActualWork) ' -
Picks up local Actual Work value

'Enterprise text field
Tsk.SetField
FieldID:=FieldNameToFieldConstant(FieldName:="LastWeekWorkText",
FieldType:=pjTask), Value:=vLastWeekWork
'Enterprise duration field
Tsk.SetField
FieldID:=FieldNameToFieldConstant(FieldName:="LastWeekWorkDurn",
FieldType:=pjTask), Value:=vLastWeekWork
'Enterprise numeric field
Tsk.SetField
FieldID:=FieldNameToFieldConstant(FieldName:="LastWeekWorkNumber",
FieldType:=pjTask), Value:=DurationValue(vLastWeekWork)

End If
Next Tsk
End Sub
 
J

Jack Dahlgren

Glad you got it solved and thanks for posting the working code.

-Jack Dahlgren
 

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