Normal Topic XResultSet commands (Read 1318 times)
CapitalG
Full Member
Members
***
Offline



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
XResultSet commands
May 12th, 2008 at 9:55pm
Print Post Print Post  
I am attempting to use the XResultSet of commands that was demonstrated in the Friday Session a couple of weeks ago.  I have found them very useful and I think I have slowly learned how to use the different commands on a very basic level.  I am trying to figure out how to sort/calculate on a result set once I have retrieved the result set.  I have an application that contains three databases.  One database is different Investments, one is Investors, and the last is transactions.  I can retrieve a result set that has all of the transactions for a given Investment.  I am stuck on how to go about getting a total of those transactions by investor within that result set.  Do I do this by looping through the result set?  How do I get the total for each investor?  Does it involve an array?  

I am lost and need some assistance.  Undecided

Thank you.
  
Back to top
 
IP Logged
 
Hammer
YaBB Administrator
Lanticans
*****
Offline


Fire bad. Tree pretty.

Posts: 3436
Location: Ohio
Joined: Nov 22nd, 2002
Re: XResultSet commands
Reply #1 - May 13th, 2008 at 2:06pm
Print Post Print Post  
Yes, you loop through the records and use an array to collect the results. Here is a set of routines you can drop into a button in Customers or in the example XResultSet dsr I gave you. It collects the amounts by State. There are several small generic utility routines for working with the array plus the main body of code. Let me know if you need any of it clarified.

Code
Select All
var vRS as Int
var vState as String
var vOldState as String
var vAmount as Money
var vPos as Int
var vLoop as Int
var vCount as Int
var vStateArray as Array[52, 2] of String

// Initializes all array elements to ""
SUBROUTINE ClearArray()
var lLoop1 as Int
var lLoop2 as Int
var lMax1 as Int
var lMax2 as Int

	lMax1 = DimLimit(1, vStateArray)
	lMax2 = DimLimit(2, vStateArray)
	For lLoop1 = 1 To lMax1
		For lLoop2 = 1 To lMax2
			vStateArray[lLoop1, lLoop2] = ""
		Next
	Next

END SUBROUTINE

// Utility routine to display filled array
SUBROUTINE EmitArray()
var lLoop as Int
var lMax as Int

	lMax = DimLimit(1, vStateArray)
	WriteLn(@Text(50, "-"))
	For lLoop = 1 To lMax
		If (vStateArray[lLoop, 1] + vStateArray[lLoop, 2]) <> ""
		{
			WriteLn(vStateArray[lLoop, 1] + ": $" + @Decimals(vStateArray[lLoop, 2], 2))
		}
	Next

END SUBROUTINE

// Find array position of matching entry.
// Create if not found.
FUNCTION GetArrayPos(aVal as String) as Int
var lRet as Int
var lLoop as Int
var lMax as Int

	lRet = 0
	lMax = DimLimit(1, vStateArray)
	lLoop = 1
	While (lLoop <= lMax) And (lRet = 0)
	{
		If aVal = vStateArray[lLoop, 1]
		{
			lRet = lLoop
		}
		lLoop = lLoop + 1
	}

	// If not found, find a blank slot and use it.
	If aVal <> ""
	{
		If lRet = 0
		{
			lRet = GetArrayPos("")
			If lRet > 0
			{
				vStateArray[lRet, 1] = aVal
			}
		}
	}

	Return lRet

END FUNCTION

	// Get all Customer records
	vRS = @XResultSetSearch(@FN, "Customers", SEARCH_MODE_AND,SEARCH_SYNTAX_QA, "")
	If vRS > -1
	{
		// Sort by state for efficiency
		XResultSetSort(vRS, "State:-1")
		vCount = @XResultSetTotal(vRS)
		ClearArray()
		vOldState = "something that never occurs"
		vPos = 0
		For vLoop = 1 To vCount
			XResultSetCurrentPosition(vRS, vLoop)
			vState = @XResultSetValue(vRS, "State")
			If vState <> vOldState
			{
				vPos = GetArrayPos(vState)
				vOldState = vState
			}
			If vPos > 0
			{
				vAmount = @XResultSetValue(vRS, "Currency")
				If vAmount > 0
				{
					vAmount = vAmount + @ToMoney(vStateArray[vPos, 2])
					vStateArray[vPos, 2] = @Str(vAmount)
				}
			}
			Else
			{
				WriteLn("Error: Failed to get array position for " + vState)
			}
		Next
		XResultSetClose(vRS)
		EmitArray()
	}
	Else
	{
		@MsgBox("Error: failed to open result set.", "", "")
	}

 

  

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



Posts: 143
Location: Phoenix, Arizona
Joined: Mar 4th, 2003
Re: XResultSet commands
Reply #2 - May 13th, 2008 at 4:17pm
Print Post Print Post  
Smiley

Thanks.  I think I was able to follow everything.  

Of course, it worked like a charm and even worked when I made changes so it would work in my application.  Thanks again.

Greg Smiley
  
Back to top
 
IP Logged