![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi, What is the best way to validate a relationship will be valid before creating records. e.g. I have a function that creates a new record in a table taking as a parameter an id of another record in another table. I want to validate that the value in the parameter really is a valid id. A couple options might be: To do script a find in the other table and check the result, but in that case I'd need to add a utility layout. To do create globals, and base relationships off of that, then assign the global value, and see if there is a record through the relationship. obviously that will require global fields, relationships, and table instances. Neither is ideal... but either can work... is there something else people do for this? Of the two options above which one is preferable to the other? (and why?) |
#3
| |||
| |||
|
|
The second, Global fields, method is probably the best and easiest. I don't really know what "table instances" are, |
|
but once the Relationship is defined you don't actually need any related fields on the current layout to accessing them - just a script command along the lines of: Set Field [GlobalField, LinkData] If [Count(Relationship::KeyField) = 0] {do whatever for no related records} Else {related records do already exist} End If |

#4
| |||
| |||
|
|
The second, Global fields, method is probably the best and easiest. I don't really know what "table instances" are, Pardon me "Table Occurrences" (on the Relationships Graph) but once the Relationship is defined you don't actually need any related fields on the current layout to accessing them - just a script command along the lines of: Set Field [GlobalField, LinkData] If [Count(Relationship::KeyField) = 0] {do whatever for no related records} Else {related records do already exist} End If Interesting. Yeah, I can see the simplicity advantage in script coding, but the expense is in design complexity, as the validation check can't be contained by a script but instead has to spill-over into the data design, adding fields, table occurrences, and relationships. snip |
#5
| |||
| |||
|
|
In article <1176347935.042623.298... (AT) e65g2000hsc (DOT) googlegroups.com>, "d-42" <db.pors... (AT) gmail (DOT) com> wrote: The second, Global fields, method is probably the best and easiest. I don't really know what "table instances" are, Pardon me "Table Occurrences" (on the Relationships Graph) but once the Relationship is defined you don't actually need any related fields on the current layout to accessing them - just a script command along the lines of: Set Field [GlobalField, LinkData] If [Count(Relationship::KeyField) = 0] {do whatever for no related records} Else {related records do already exist} End If Interesting. Yeah, I can see the simplicity advantage in script coding, but the expense is in design complexity, as the validation check can't be contained by a script but instead has to spill-over into the data design, adding fields, table occurrences, and relationships. snip You can use a similar method in a field's Validation by Calculation options. eg. Count(Relationship::KeyField) <> 0 where "<>" can be either that or the "not equals" symbol, it makes no difference since they both mean the same thing. BUT, the problem is that you can't set the GlobalField value first unless the user has done that manually or you do it via a script anyway, so the current Relationship may not be the one you want. Helpful Harry Hopefully helping harassed humans happily handle handiwork hardships ;o) |
key) <> 0
key) and leave it at that. Its
#6
| |||
| |||
|
|
One other note about your suggestion of: count(table: key) <> 0Why not just validate >0 ? It simply can't be <0 and it dodges the whole "<>" vs "not equal to symbol" discussion entirely. |
)
Personally, I just count(table: key) and leave it at that. Itsultimately a boolean evaluation so if the count is 0 its false, and if its nonzero its true. I can see the sense of explicitly comparing it to 0 in terms of writing self-documenting code; I can see how you might argue the explicit comparison to 0 might be a little better at communicating your intent. In any case, its a matter of personal style. |
#7
| |||
| |||
|
|
There's also other tests you could perform to achieve the same thing. eg. IsEmpty(Relationship::KeyField) |
#8
| |||
| |||
|
|
There's also other tests you could perform to achieve the same thing. eg. IsEmpty(Relationship::KeyField) |
|
Or Isvalid(Relationship::KeyField) |
|
The cCount has the disadvantage it does not return 0 when there are no related records, but returns empty. (at least in my v8.5 adv). You have to build a wrapper to return a 1 or 0. I observed this some time ago and even built a testfile to recreate this. |
#9
| |||
| |||
|
|
Or Isvalid(Relationship::KeyField) You know Ursus, I haven't ever used isvalid. Can it actually be used to test for related records? And if so what are the constraints? |
|
The cCount has the disadvantage it does not return 0 when there are no related records, but returns empty. (at least in my v8.5 adv). You have to build a wrapper to return a 1 or 0. I observed this some time ago and even built a testfile to recreate this. Are you sure, in my experience count returns 0 when there are no related records with only one exception: that its defined in a calc field with "do not evaluate if all related fields are empty" (in which case its blank) |
|
And in that one exception, building a wrapper doesn't work, because if(count(xx)="";0;1) is STILL going to be blank, because all the related fields are still empty, so it still won't be evaluated. |
#10
| |||
| |||
|
|
Or Isvalid(Relationship::KeyField) You know Ursus, I haven't ever used isvalid. Can it actually be used to test for related records? And if so what are the constraints? Are you sure, in my experience count returns 0 when there are no related records with only one exception: that its defined in a calc field with "do not evaluate if all related fields are empty" (in which case its blank) Yes I am very sure. I have built a testfile. Create two tables with a relation. Two related records and one not related. |
![]() |
| Thread Tools | |
| Display Modes | |
| |