In FNCPlanner, I am reading a list of resources from a database. These already have IDs associated with them. I add the resources using Resources.add, but I can't use the ID from the database as the ID of the resource.
So when I post a new item back in the planner, the DBKey I have set in the DatabaseAdapter doesn't get my ID, but instead gets the resource ID from the planner.
What is the way to set the correct ID when i create the resource, so that when the new event is written back to the database, it has the resource ID from the Database ?
You can use the OnFieldsToItem & OnItemToFields events for that, mapping the correct resource index on the field instead of letting the internal DBKey handle this.
Thank you for the quick reply. I'm not sure I explained correctly. When I add or load items from the database, they are not assigned to the correct resource. I cannot see a property on the resource that maps to the Resource item in the database adapter.
I am adding resources programatically using TMSFNCPlanner1.Resources.Add, but where is the property on this resource that maps to the underlying database field ?
The resource is index based. so when adding three resources, the first resource has index 0, then 1, then 2. This cannot be changed. If you have resources in the dataset that also start based on 0, then this will be correct, if you have another index, then you need to convert to the correct index using the events mentioned earlier.
I have tried your suggestion, and maybe I don't understand what should be a simple process.
In my database, I have existing events where the Resource column is populated using existing staff IDs.
For example, I have a user ID no. 2108. When the database adapter reads events, the Resource field is correctly populated - I can see this if I insert a breakpoint - but the AItem.Index is a read only property so can't be changed. The events are not assigned to the correct resource, so events for Resource ID are not displayed because there are not 2108 resources in the planner.
When a new Item is created, AItem.Resource is the Index (i.e. the Column number). Why does this change ?
It doesn't matter what I change, the events are not mapped correctly to the resource and events written back to the database do not have the correct resource IDs.
What I mean is, when loading the resources, they will begin at index 0. The index of the resource in the resources collection. When loading the items, the Resource property will be set to 2108, but this does not correspond with the index of the resource in the collection. If you have 10 resources, the first resource index will be 0, the last resource index will be 9. The Resource property of the item needs to be between 0 and 9. As this is 2108, the item will not be visible. When adding a new item, the resource is again between 0 and 9, and this is persisted like this in the database. So to workaround this. The OnItemToFields and OnFieldsToItem events need to be used to map the correct resource to the correct index in the database
procedure TForm79.TMSFNCPlannerDatabaseAdapter1FieldsToItem(Sender: TObject;
AFields: TFields; AItem: TTMSFNCPlannerItem);
if AFields.FieldByName('Resource').AsInteger = 2108 then
AItem.Resource := 0;
procedure TForm79.TMSFNCPlannerDatabaseAdapter1ItemToFields(Sender: TObject;
AItem: TTMSFNCPlannerItem; AFields: TFields);
if AItem.Resource = 0 then
AFields.FieldByName('Resource').AsInteger := 2108
Please note that I just simplified things here, but you'll need a conversion table to map the resources in the planner to the resources in the database
Thanks Pieter, from your sample I managed to work it out.