[comp.sys.mac] 4D 2.0 question

chuq@Apple.COM (Chuq Von Rospach) (10/03/89)

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?


-- 

Chuq Von Rospach <+> Editor,OtherRealms <+> Member SFWA/ASFA
chuq@apple.com <+> CI$: 73317,635 <+> [This is myself speaking]
Future home of the San Jose Photons!

I was a Kings fan before it was politically correct. NHL to San Jose!

liemandt@lindy.Stanford.EDU (Joe Liemandt) (10/03/89)

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

bmug@garnet.berkeley.edu (BMUG) (10/03/89)

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?
>

The way I'd do it would be to enter the key into a variable, do a PUSH
RECORD, search for the key (should take a few milliseconds if the key
field is indexed), do a POP RECORD if the search comes up empty (and
continue entering your record), or bring up an appropriate alert or
dialog if the key already exists, and give the user the opportunity
to enter a new key (then search, etc.) before POPping the RECORD.

4D will automatically catch an attempted entry into a unique field
where a key already exists, but if you'll be generating field contents
procedurally, this will give the user some added control, with an
extremely small sacrifice in speed.

John Heckendorn
                                                             /\
BMUG                      ARPA: bmug@garnet.berkeley.EDU    A__A
1442A Walnut St., #62     BITNET: bmug@ucbgarne             |()|
Berkeley, CA  94709       Phone: (415) 549-2684             |  |