|
|
|
Navigate: | My Mac Online | The Archives | February 2000 | FileMaker 101 - Part 23 | |
![]() |
|
![]() Part 23
My Mac Magazine manavesh@mymac.com
Auto-Enter or Lookup? This month I'm going to revisit the problem of marking the first entry of a unique value in a field. There are several reasons you might want to do this. A common one is to show only unique entries in a portal, by relating a "1" constant field to the marking field. Otherwise you often have duplicates showing, making the portal useless as a value list for making choices. I've recently discovered another method, using an Auto-enter by calculation, one of the Auto-enter Options available in Define Fields.
Lookup Method (earlier article)
This works well enough, but it has the serious limitation that you absolutely cannot reset or Relookup the key* field later. If you do, for some other use, forgetting about the 1st Mark lookup, the mark will be erased. All the records having that value will no longer be visible in the portal.
Auto-enter by Calculation Method Marking the first entry relies on a self-relationship based on the key field you are marking for uniqueness. The field is empty when you create a new record, so there is no self-relationship to evaluate.
Delaying the Auto-enter
In this case we also want to stop it from happening if the self-relationship is valid; because this would mean there was already a match, an earlier duplicate value.
In the example file, the field we want to mark is Job ID. *The second line is not totally necessary for the mark to occur the first time, but is useful if the record is deleted (more below).
Deleting 1st Mark! You might think, "Just use a calculation that will re-evaluate." Unfortunately, because it would depend on a self-relationship, such a result could not be indexed. It couldn't be used as the target of any relationship, making it useless for many purposes, such as portals. The only thing you can do if the mark is deleted is to run an operation on those records to reset the mark into the first of them. It's easy to isolate them, using; Go To Related Record [Show, "Self-Job ID"].
Replace 1st Mark, Lookup
Replace 1st Mark, Auto-enter Calculation It comes to the same thing, but I like it built directly into the definition. Another advantage is that if have more than one mark field (more later), they will all be re-evaluated and reset.
Button for Deleting Records Since the Job ID of that record will no longer be available to use in the self-relationship after it's deleted, we need to set it into a global field first. Then we'll check for that after deletion. [This time we are using the Job ID g field only as a "flag" field. It isn't doing anything else but letting us leave a flag before we delete the record. We could also create a new global flag field to do it. It is very important to "initialize" (set to nothing) such a flag at the beginning of the script.] The subroutine would look like this:
Set Field ["Job IDg", """"]
*This step will only work with the Auto-enter calculation method, not with the Lookup method (which will not lookup a new mark). With that method you'd have to explicitly set the marking field, using this step instead:
Set vs. Replace *If, however, you've sorted that relationship, as you can now do in v.4 or 5, it will go the first record in that sort order, which probably won't be the first record that was entered. In the case when you just want to mark one of each group, but don't care particularly which one, this doesn't matter. If it does matter, you can duplicate the relationship, but make the new one unsorted; use that one for the Go To Related Record ["Self-relationship"] steps. Or, if this is the only place where the problem occurs, just add these steps before setting the Job ID field:
Unsort One other advantage of using Set Field, rather than Replace or Relookup, is that it doesn't require the key field to be on the current layout. The auto-enter by calculation doesn't require the mark field to be on the layout, either. This makes it even safer.
Imports or Mass Deletions
Concatenating Fields for Multiple Marks The other mark fields would be based on concatenations with the key field. In the example file, there is the field, Job\Person ID = If(not IsEmpty(Job ID) and not IsEmpty(PersonID), Job ID & " " & PersonID, "") The "not IsEmpty(Job ID)" parts ensure that the field is only filled when both its part fields are filled. This is important, as otherwise the mark calculation could not tell which was the first entry of both parts.
Its marking field, Job\Pers\MarkCalc = This is the same as the earlier calcs, but in this case it uses a concatenated field.
Replace Deleted Mark with Concatenated Fields The general rule is go to the related records for the "encompassing" key field. In the case above you'd go the records for the Job ID, and do all the records for that job. Basically, it works easily if there is a "one-to-many" relationship between the two groups. In the case above, many people work for a company, but each person only works for one company. If each person can work for several different companies, you've got a "many-to-many" relationship(s), and things are considerably more difficult to mark; though not by any means impossible. You'd have to capture the IDs of all companies the person worked for, and do the Replace on each company group separately; or just do all records. You'd probably want separate files as well.
New Records Methods
Fenton Jones
Websites mentioned:
FileMaker 101 - Previous Columns
|
|
Copyright ©1995-2000 My Mac Productions, All Rights Reserved |