Normal Topic Preventing Duplicate Numbers in Database (Read 1102 times)
Louis Galvao
Full Member
***
Offline


"Sufferin' Succatash"

Posts: 265
Location: Canada
Joined: Feb 14th, 2005
Preventing Duplicate Numbers in Database
Jan 31st, 2006 at 4:32pm
Print Post Print Post  
I am using the following programming in lieu of @Number (from the Sesame Library) on element entry:

//Only assign the number if this is a new record

var vNextNum as Int

If(@IsNew)
{
    If(@Add)
    {
       vNextNum = @ToNumber(@GlobalValue("NextNumber")) + 1
       Project# = vNextNum
       GlobalValue("NextNumber", @Str(vNextNum))
    }
}

I noticed that I can add a duplicate number if I reset the NextNumber or by manually typing in any number.

How would I prevent a duplicate number from being assigned ?  I don't think field restrictions will do it.

Thanks,

Louis
  

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


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Preventing Duplicate Numbers in Database
Reply #1 - Jan 31st, 2006 at 4:59pm
Print Post Print Post  
You can do an @XLookup to check if the number you assigned already exists and keep incrementing until it does not.

You can also use another method of assigning a number.
http://www.lantica.com/Support/sesame_onesheet_11.html
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Louis Galvao
Full Member
***
Offline


"Sufferin' Succatash"

Posts: 265
Location: Canada
Joined: Feb 14th, 2005
Re: Preventing Duplicate Numbers in Database
Reply #2 - Jan 31st, 2006 at 6:10pm
Print Post Print Post  
Erika:

My code looks as follows on element entry:

//Only assign the number if this is a new record

var vNextNum as Int

If(@IsNew)
{
    If(@Add)
    {
     vNextNum = @ToNumber(@XLookupR(@FN, "9999999", "Project#", "Project#")) + 1

           If Not @Error
           {
                 Project# = vNextNum
           }
     }
}

In this example, I replaced "key" with my element name called Project#.

I didn't change the "9999999" reference.  What is this supposed to represent ?


Thanks,

Louis
  

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


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Preventing Duplicate Numbers in Database
Reply #3 - Jan 31st, 2006 at 6:36pm
Print Post Print Post  
Quote:
I didn't change the "9999999" reference.  What is this supposed to represent ?


@XLookupR returns the highest existing value that does not exceed the key. The 9999999 is meant to be a number higher than any that may exist to ensure you get the highest existing value.
  

- Hammer
The plural of anecdote is not data.
Back to top
IP Logged
 
Louis Galvao
Full Member
***
Offline


"Sufferin' Succatash"

Posts: 265
Location: Canada
Joined: Feb 14th, 2005
Re: Preventing Duplicate Numbers in Database
Reply #4 - Jan 31st, 2006 at 11:06pm
Print Post Print Post  
Erika:

Thanks for the info.

I was still able to duplicate a number.  How would I increment to avoid any duplicates ?

Louis
  

Louis Galvao
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Preventing Duplicate Numbers in Database
Reply #5 - Feb 1st, 2006 at 3:14am
Print Post Print Post  
Louis,

To check for duplicate entries, I use something similar to the following code in the On Element Change event of the element that I am checking. (i.e. Project#, in your case.)

Code
Select All
if Project# = @Xlookup(@FN, Project#, "MyForm!Project#", "Project#")
     @msgbox("This field should be unique.  Please verify before continuing.", "", "")
 



FYI: The 3rd parameter that includes the name of the form, is usually needed if the application has multiple databases - especially if the name is used again in another database. I've found that it is a good idea to always include the form name, so that it will still work if you later add more databases to the application.

  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
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: Preventing Duplicate Numbers in Database
Reply #6 - Feb 1st, 2006 at 5:06am
Print Post Print Post  
Just to be clear, two different functions have been discussed here.

One is how to add a unique sequential number with XLR. (Erika)
One is how to check for an existing number. (Carl)

Very similar in many respects, but the original question was how to prevent a duplicate number from being entered.

Using the XLR should add a unique number. 

But once that is done you may still want to check for duplicate numbers in case some other process has allowed that to happen.  There are many ways to prevent that, but too many to list here.

So you may want to use both examples.
Use Erika's if @NEW and use Carl's if @UPDATE
  



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


"Sufferin' Succatash"

Posts: 265
Location: Canada
Joined: Feb 14th, 2005
Re: Preventing Duplicate Numbers in Database
Reply #7 - Feb 3rd, 2006 at 7:47pm
Print Post Print Post  
So, I programmed my project# field (on element change event) with the following:

If(@IsNew)
{
    If(@Add)
         {
           If Project# = @Xlookup(@FN, Project#, "Projects!Project#", "Project#") then
           {
                 NotifyForm(1)
                 @msgbox("This Project# is a duplicate number.  Please verify before continuing.", "", "")
                 ThrowFocus(Project#)
           }
     }
}


If(@Update)
         {
           If Project# = @Xlookup(@FN, Project#, "Projects!Project#", "Project#") then
           {
                 NotifyForm(1)
                 @msgbox("This Project# is a duplicate number.  Please verify before continuing.", "", "")
                 ThrowFocus(Project#)
           }
     }

In ADD mode, it works and forces you back to the field thereby updating the number.

In Search mode, it warns you but stll allows you to save with a duplicate #, even though I have included NotifyForm(1).

Is there some other statement that I could insert to prevent it from saving in search mode ?

Thanks for all the input.

Louis
  

Louis Galvao
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: Preventing Duplicate Numbers in Database
Reply #8 - Feb 3rd, 2006 at 7:53pm
Print Post Print Post  
Quote:
In Search mode, it warns you but stll allows you to save with a duplicate #, even though I have included NotifyForm(1).


Hello Louis,

Where are you setting NotifyForm(0)? My guess is that NotifyForm(1) is being set but it is being cleared by another event.

-Ray
  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
Louis Galvao
Full Member
***
Offline


"Sufferin' Succatash"

Posts: 265
Location: Canada
Joined: Feb 14th, 2005
Re: Preventing Duplicate Numbers in Database
Reply #9 - Feb 3rd, 2006 at 7:58pm
Print Post Print Post  
Ray:

I tried NotifyForm(0) but it didn't do anything.

However, I think it is being over-ridden On Form Entry by NotifyForm(2) and NotifyForm(-2) to prevent record advance (extended mode) in Search/Update.

Thanks,

Louis
  

Louis Galvao
Back to top
 
IP Logged