Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!wuarchive!gem.mps.ohio-state.edu!apple!agate!shelby!lindy!liemandt
From: liemandt@lindy.Stanford.EDU (Joe Liemandt)
Newsgroups: comp.sys.mac
Subject: Re: 4D 2.0 question
Message-ID: <4991@lindy.Stanford.EDU>
Date: 3 Oct 89 05:49:31 GMT
References: <35206@apple.Apple.COM>
Sender: liemandt@lindy.Stanford.EDU (Joe Liemandt)
Reply-To: liemandt@lindy.Stanford.EDU (Joe Liemandt)
Organization: Stanford University (Disneyland of the North)
Lines: 49

In article <35206@apple.Apple.COM> chuq@Apple.COM (Chuq Von Rospach) writes:
>Here's a question for the folks who are using 4D. I've got a file with a
>unique key. I'm going to be generating that key in a procedure, and I want
>to check for the previous existence of that key programmatically. What is
>the most efficient way of doing that?
>
>I can see two approaches:
>
>do a SEARCH based on that key. If "number of records in selection" > 0, it's
>already there.
>
>try to SAVE RECORD the thing. If it fails (the field is marked unique) then
>it exists.
>
>In both cases, I want to loop and generate the key again until I do get it
>right. Any suggestions on the cleanest way to do this?
>

Using a search rather than aborting a save is probably best.

From your description, it seems that your key is not a sequence
number, rather some sort of concatenation. For example a subscriber id,
first character of first and last name and then a number.

JL001	-> Joe Liemandt
JL002	-> John Lynch

Below is the code to do this:

theNum:=0
Repeat
	theNum:=theNum+1
	UniqueKey:=FirstName<1>+LastName<1>+theNum
	Search ([FileName];[FileName]UniqueField=UniqueKey)
Until (Records in selection([FileName])=0)
Create Record([FileName])
[FileName]UniqueField:=UniqueKey
Save Record([FileName])

The above will work until you start to get a lot of names in the database.

It is much better to store what the next sequence number is rather
than building one and searching for it each time.  But it sounds like
that will not work in your situation.

Hope this helped.

Joe Liemandt
liemandt@lindy.stanford.edu