Hot Topic (More than 10 Replies) Retrieving data from same database (Read 1529 times)
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Retrieving data from same database
Mar 30th, 2004 at 8:17pm
Print Post Print Post  
Does anybody know how to carry a value forward from the previous record in a database to the next record?  I've tried the various xlookup functions and don't think that any of them will do it.  I need to carry forward a total from calculations on the previous record to the next record.  Can Sesame do this?  I have had some success with @ditto but, its not getting the job done right because the field I am ditto-ing is used in the new record for the next round of calculations.  Any help would be appreciated.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: Retrieving data from same database
Reply #1 - Mar 30th, 2004 at 8:22pm
Print Post Print Post  
Try using a Global Variable or @GlobalValue() / GlobalValue().  Smiley
  

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


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #2 - Mar 30th, 2004 at 8:29pm
Print Post Print Post  
Thanks, I'll see if that works.
  
Back to top
 
IP Logged
 
Bharat_Naik
Senior Member
Members
*****
Offline


Ever ready to learn and
share

Posts: 1202
Location: Chicago,  Illinois
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #3 - Mar 30th, 2004 at 8:39pm
Print Post Print Post  
If you have counter in the database, you can accomplish that using @xlookupr.

Let's say you have following fields

Counter:
Total:

You want to advance the value of Total to the next record - following code will in Form Entry Event.

Total  = @xlookupr (@fn, 9999999999, "Counter", "Total")

Since the value is allocated on form entry event, you can use it in other calculations and also update it as required.
  
Back to top
 
IP Logged
 
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #4 - Mar 30th, 2004 at 9:06pm
Print Post Print Post  
Do you mean the "counter" field adds a sequential number to the record as in @number?
  
Back to top
 
IP Logged
 
TJCajun
Junior Member
**
Offline


"Laissez les bons temps
rouler"

Posts: 72
Location: Louisiana
Joined: Nov 25th, 2002
Re: Retrieving data from same database
Reply #5 - Mar 31st, 2004 at 2:37am
Print Post Print Post  
What the programming example is "saying" is this...  Look in the same database  (@fn); and look for a value of 9999999999 in the Counter field.  Because there hopefully WON'T be such a value in the Counter field, the @xlookupr will then look for the highest value that is below 9999999999 in the Counter field.  If other programming is working properly, the value in the Counter field of your previous record should be the highest value in the database (and thus will be the highest value below 9999999999).  Upon finding this record (your previous record), the programming will retrieve the value from Total, and place it in the current record's Total field. 

The Counter field will have to be incremented using other programming, such as @Number.

Hope I'm close on this explanation, and that it is of some help.

  
Back to top
IP Logged
 
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #6 - Mar 31st, 2004 at 2:35pm
Print Post Print Post  
Merci beaucoup Messieur Cajun.  Do you think this would work if I already have a numbered field on each record?  In other words, does the "counter" have to be generated by a @number or can it be manually enterred for each record? I think I know the answer, but want to be sure.
  
Back to top
 
IP Logged
 
TJCajun
Junior Member
**
Offline


"Laissez les bons temps
rouler"

Posts: 72
Location: Louisiana
Joined: Nov 25th, 2002
Re: Retrieving data from same database
Reply #7 - Mar 31st, 2004 at 4:35pm
Print Post Print Post  
I don't think the Counter field would have to be generated by @Number.  But the value would have to be present before the xlookupR programming fired, or you would grab data from a record that was not the immediately previous record.  Manually entering this value in the Counter field could allow data entry errors (such as entering 3456 instead of 3465) to allow the xluR to pull from the wrong record.

There's also what I perceive as problems with your scenario if one or more previous records are deleted.  Then the "Total" value that you have been carrying forward from record to record would no longer be the correct "Total".  If your application is something like a checking account appl., or anything similar, then this could be a problem.

I'm very rusty on my Q&A programming, and not at all proficient in Sesame.  I'm hoping someone else can step up and offer comments and suggestions.  Hammer's suggestion may be an excellent way to maintain the "proper" value across records in an application.
  
Back to top
IP Logged
 
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #8 - Mar 31st, 2004 at 6:51pm
Print Post Print Post  
You are right about records being deleted or entering the wrong number.  This data base, however, has a max of 52 records and each record is tied to a pay period.  So the data entry-er (is that a word?) has to use the right sequential number to get the date range for the pay period.  So, I'm not worried about deletions or incorrect entries.  I am worried about the order of execution, though.  I'm not having any luck with the global values option at this time as it is a new concept to me and I don't have any examples to really look at and analyze.  The manual is pretty sketchy.
  
Back to top
 
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Retrieving data from same database
Reply #9 - Apr 1st, 2004 at 2:13am
Print Post Print Post  
I haven't tried using global values yet either, but I have been using this code for years in Q&A to keep a running balance of a checkbook database. This is basically what Bharat and TJ were refering to.
Code
Select All
if FormNo="" and CheckNo<>"" then
    FormNo=@xlr(@fn,99999,"FormNo","FormNo") + 1
;
if CheckNo<>"" then
 {if FormNo>1 then
  Balance= @tn( @xlr(@fn, FormNo - 1, "FormNo", "Balance") ) - CheckAmount
 else
  Balance= -CheckAmount
 } 


The first part assigns a new sequential number to the FormNo field. The second part retrieves the balance of the record with a FormNo that is one less than the current record's FormNo, then subtracts the current check amount, and places that value in the balance field.

(Deleting records or updating check amounts at a later date will cause an incorrect balance to display. I get around this with a macro that reassigns all the form numbers and mass updates to recalculate all the balances.)
  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Retrieving data from same database
Reply #10 - Apr 1st, 2004 at 2:03pm
Print Post Print Post  
GlobalValue is comprised of two routines - a subroutine "GlobalValue(KeyString, ValueString)" and a function @GlobalValue(KeyString). When GlobalValue is called the ValueString is stored by the database engine under the "tag" passed in in "KeyString". So, in a simple case, you could store someone's name with:

GlobalValue("name", "Mark Lasersohn")

To then retrieve that name, you would use:

var str as string
str = @GlobalValue("name")

GlobalValues, once set are retained unless reset. So you can shutdown Sesame, turn off your computer, go on vacation, and when you return - it will still be set. They are held on the server so programming in any layout in that application can get to the value or set it - as can any user using that server. In that sense they are similar to a Lookup Table - but a lot more flexible and unlimited.

For example, lets say that you want to hold a singular piece of information by date. You can use the @date function to supply the "KeyValue":

GlobalValue(@str(@date), "Filled Tank")

Then you can access the different GlobalValues stored under different dates, by passing the date you interested in to @GlobalValue:

var str as string
str = @GlobalValue("2001/02/01")
if(str = "Filled Tank")
{
    writeln("I filled the tank on February 1st, 2001")
}

Or you could use a zip code as a KeyString and list the cities that fall under that zip code as the value:

GlobalValues(44122, "Cleveland;Euclid;Wickliffe")

Then access it for use in a Popup Menu:

var zip_code as string
var result as string

// Zip is a LE on the form
zip_code = Zip

result = @PopupMenu(@GlobalValue(zip_code), "Select a City")

The number of cases where global values can (and should) be used is infinite. They can be used to communicate between clients using the same server (kind of like an email). They can be used to store unique or important events. They make a very good simple lookup mechanism. They can pass data between forms and other forms and reports. Etc...
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
Re: Retrieving data from same database
Reply #11 - Apr 1st, 2004 at 2:45pm
Print Post Print Post  
Thanks for the great examples and explanations.  I think I can figure it out now.
  
Back to top
 
IP Logged
 
bstone30
Member
*
Offline


No personal text

Posts: 28
Location: USA
Joined: Dec 16th, 2003
!!
Reply #12 - Apr 2nd, 2004 at 12:02am
Print Post Print Post  
Yea!  I did it.  Thanks to all of you who helped me out.  Glboalvalues is a powerful  tool that I'm sure  that I will be using a lot in the future!
  
Back to top
 
IP Logged