Hot Topic (More than 10 Replies) Automatic numbering (Read 1299 times)
etejeira
Junior Member
**
Offline


No personal text

Posts: 60
Joined: Aug 6th, 2005
Automatic numbering
Feb 5th, 2006 at 3:36pm
Print Post Print Post  
I have programmed:
If numberfield = "" Then numberfield = @number
on Entry
and
I have typed the number 159 in the Application Property Manager.
This works well and the next record that I want to add will show 160 in the numberfield and 161 for the next etc...
However, if, after I enter the Add Mode and the correct number appears, I decide to Exit the Add mode and not add any records, the next time I try to add a record it will give me the number after the one that appeared before and I wind up loosing a number.
I would like to be able to use the number that appeared the first time without having to go to Design Mode and change the initial @number value.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Automatic numbering
Reply #1 - Feb 5th, 2006 at 4:07pm
Print Post Print Post  
To avoid skipping numbers, you can assign the @Number on a different event than On Form Entry. For example, if you wait until On Form Exit, or until you actually type something in your record, you won't assign a number simply because you arrived at a record.

Another way to avoid skipping numbers is to use this method:
http://www.lantica.com/Support/sesame_onesheet_11.html
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
etejeira
Junior Member
**
Offline


No personal text

Posts: 60
Joined: Aug 6th, 2005
Re: Automatic numbering
Reply #2 - Feb 6th, 2006 at 12:53pm
Print Post Print Post  
Why is it better to use:
If(@IsNew) and If(@Modified) instead of:
If numberfield = ""    and  @number ?


  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Automatic numbering
Reply #3 - Feb 6th, 2006 at 1:14pm
Print Post Print Post  
You should use whatever indicates to you that it is time to assign a value.

@IsNew is mainly meant to be used where you used @Add in Q&A. Just being in add mode does not mean that you are not F9ing back through an existing record. Using @IsNew ensures that you only do things if a record is new and has never been saved, regardless of what mode you are in.

In the example I showed you, @Modified is used because the autonumber is being done On Form Exit rather than Entry. It assigns the number when the record is saved, but only if it is new and has been changed by the user (so that it is not a blank record).

If you want to check if your number field is blank, use If @IsBlank(numberfield) instead of checking for "". Numbers are actually numbers in Sesame. @IsBlank is type safe. Checking for "" is only reliable with Text fields.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
etejeira
Junior Member
**
Offline


No personal text

Posts: 60
Joined: Aug 6th, 2005
Re: Automatic numbering
Reply #4 - Feb 6th, 2006 at 1:25pm
Print Post Print Post  
Thanks for the very clear explanation.
Now, what about the use of @number versus @XLookupR or Global value ?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Automatic numbering
Reply #5 - Feb 6th, 2006 at 2:10pm
Print Post Print Post  
Quote:
Thanks for the very clear explanation.
Now, what about the use of @number versus @XLookupR or Global value ?


@Number simply increments itself every time you ask. It has no "smarts" that enable it to not skip numbers or prevent duplicates. The only thing it knows how to do is increment itself and hand you the result. It does not know if you actually use the number, so has no ability to "back up" if you exit without saving the record.

@XLookupR (as used in the example) looks into the database and returns the highest number that exists. You can then add 1 to that to get the next unused number. This method prevents skipped numbers.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
etejeira
Junior Member
**
Offline


No personal text

Posts: 60
Joined: Aug 6th, 2005
Re: Automatic numbering
Reply #6 - Feb 6th, 2006 at 2:37pm
Print Post Print Post  
Thanks again.
  
Back to top
 
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Automatic numbering
Reply #7 - Feb 6th, 2006 at 5:31pm
Print Post Print Post  
I agree that the @XLR is the preferred method.

But .... just a note that the @XLR approach can lead to duplicate numbers on a network.  If executed on FormEnter, more than one user will end up with the same "NEXT" number.  It is usually safer to use the @XLR on FormExit.

But if you must assign the number on entry for some reason, then you may want to check for duplicates with FormExit and  if you have a duplicate, then show a message, stop the Save, and run the @XLR routine again to get a new number.
  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
NICEBERG
Full Member
Members
***
Offline


I came, I saw, I'm still
trying to figure it out!

Posts: 208
Location: Green Bay, WI
Joined: Dec 17th, 2003
Re: Automatic numbering
Reply #8 - Feb 11th, 2006 at 3:58pm
Print Post Print Post  
I like the @XLR idea BUT what if I want to generate the next available number and don't really have a "key" field to latch on to?

Example: Number field needs to be incremented by last highest number used plus one. Suppose I want the next number generated as soon as I enter the database. All fields are blank. There are no blank fields in any of my existing (saved) records. (I used "blank" as an example because ANY of my records could contain any of a couple of dozen different values, and I don't want to only add one number based on the last matching value of a particular field-- I merely want to keep a record number associated with a record, which makes searching very easy.)

I tried the following using Number as the field I want to increment:

Number = @XLR(@Fn, Number, "Number", "Number") +1

This, of course, always returns "1" as my new record number.

So - how do I add "1" to the last record number generated when first entering a blank record?
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Automatic numbering
Reply #9 - Feb 11th, 2006 at 5:24pm
Print Post Print Post  
1. @XLR does not match an exact value. Used as above, it will get the highest existing number that is less than 999999. It does not matter that the current record number is blank. It is not matching the current record number.

2. Maybe this method is more to your liking:
http://www.lantica.com/Support/sesame_onesheet_10.html

  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Bob_Hansen
Senior Member
Members
*****
Offline


WOW, They have the Internet
on computers now!

Posts: 1861
Location: Salem, NH
Joined: Nov 24th, 2002
Re: Automatic numbering
Reply #10 - Feb 11th, 2006 at 5:29pm
Print Post Print Post  
Change From:
Number = @XLR(@Fn, Number, "Number", "Number") +1

Change To:
Number = @ToNumber( @XLR(@Fn, 99999999, "Number", "Number") ) +1

1.  The @XLR returns a string so you need to convert the XLR result to a number:
@ToNumber( @XLR(........) ) +1 

2.  The second value of that expression is the number the XLR is looking for.  When it does not find that value, it keeps decreasing by one, until it finds the highest value in "Number".  And then it does +1 to increase the value by one.

-------------------------------------
Four examples:
1.  If records have "Number" values from 1 to 158: the XLR looks for 99999999, does not find it, but does get the highest value,158, adds one, and returns a value of 159 for the next value to be assigned.

2. If entering the first record: it does not find 99999999, does  get the value of 0, adds one, and returns a value of 1 of the next value to be assigned.

3a.  If you had records with "Number" values from 1 to 278, and used XLR starting with 150: then the XLR would return the value of 151 because 150 does exist, and one is added. 

3b.  Same as above, but if you used XLR  starting with 999, then the XLR would return 279, because 999 did not exist, the closest value to it was 278, one was added and 279 was returned.
----------------------------------

The initial 99999999 is just a very high number that will always be higher than any value assigned to "Number" in normal operations.

  



Bob Hansen
Sesame Database Manager Professional
Sensible Solutions Inc.
Salem, NH
603-898-8223
Skype ID = sensiblesolutions
Back to top
IP Logged
 
NICEBERG
Full Member
Members
***
Offline


I came, I saw, I'm still
trying to figure it out!

Posts: 208
Location: Green Bay, WI
Joined: Dec 17th, 2003
Re: Automatic numbering
Reply #11 - Feb 11th, 2006 at 9:18pm
Print Post Print Post  
Thank you Bob - both for the solution and the explanation. Much more clearerererer now. And a VERY nice solution to not losing numbers by using the old @number!

Ooops, Thank You also, Erika. I just automatically scrolled to the last reply so I missed yours.
  
Back to top
 
IP Logged