Example: LCCurrency with LCFieldlist and LCField

This example illustrates the use of LCCurrency in conjunction with the LCFieldlist and LCField options.

Option Public
Option Declare
Uselsx "*lsxlc"

Sub Initialize
	' Find all account records that meet certain criteria and 
	' add $170.90 (or £, ¥, or whatever the unit of currency is)
	' to the balance.
	Dim con As New LCConnection("db2")
	con.Database = "..."
	' and so on...
	con.Metadata = "ACCOUNTS"
	con.Connect
	
	con.MapByName = True
	
	Dim addlAmount As New LCCurrency( )
	addlAmount.Value = 170.90
		' or addlAmount.Text = "170.90" (useful when working with values 
	    	' that might be too large for LotusScript to handle).
		' If you use the Text property, know whether "." or "," is 
		' the decimal point locally.
	Dim newComment As New LCStream( )
	newComment.Text = "Just because we like him..."
	Dim one As New LCNumeric( )
	one.Value = 1
	
	' Construct a key list that contains the query to find the records 
	' that need updating.
	Dim keyFields As New LCFieldList(1, LCFIELDF_KEY) ' all fields in 
		' this list are part of the query by default.
	Dim firstname As LCField, lastname As LCField
	Set firstname = keyFields.Append("FIRSTNAME", LCTYPE_TEXT)
	firstname.Value = "Andres"
	Set lastname = keyFields.Append("LASTNAME", LCTYPE_TEXT)
	lastname.Value = "Guirardes"
	
	Dim count As Long
	Dim results As New LCFieldList
	con.Writeback = True	' enable record locking and writeback, 
		' to efficiently update a record once we find it.
	
	count = con.Select(keyFields, 1, results)
	If count <> 0 Then
		' Some records were found (or else the connector doesn't 
		' supply this information on a Select).
		Dim fldBalance As LCField
		Dim fldTransCt As LCField
		Dim fldRemark As LCField
		Dim fldTransTime As LCField
		Dim cur As LCCurrency
		Dim transCt As LCNumeric
		Dim remark As LCStream
		Dim transTime As New LCDatetime
		
		' In case you're processing multiple records, it's more efficient 
		' to look up the fields by name once and store pointers to the 
		' fields in separate variables.
		Set fldBalance = results.Lookup("BALANCE")
		Set fldTransCt = results.Lookup("TRANSCOUNT")
		Set fldTransTime = results.Lookup("LASTTRANSACTIONTIME")
		Set fldRemark = results.Lookup("TRANSACTIONCOMMENT")
			' A text string in US-ASCII format, stored in binary (CLOB) 
			' in the database.
		
		While con.Fetch(results)
			' fetch the field value for the current balance.
			Set cur = fldBalance.GetCurrency(1)
			Print "old balance: " & cur.Text
			
			' Add the amount by which we're increasing the balance.
			' Note: in some countries with highly inflated currencies, we 
			' are very glad to have the full precision of the
			' LCCurrency datatype to do our math with, as compared to 
			' a Double in LotusScript. Also, since LCCurrency
			' is a decimal type, fractional currency units can be 
			' represented exactly, whereas 0.10, for instance, can't
			' be represented exactly by a floating point datatype.
			Call cur.Add(addlAmount, cur)
			' write that back into the field.
			Print "new balance: " & cur.Text
			Call fldBalance.SetCurrency(1, cur)
			
			' Get the current transaction count. This may potentially 
			' exceed the precision of a Long,
			' but using the LC we can handle it without a problem.
			Set transCt = fldTransCt.GetNumeric(1)
			' increment the count.
			Print "old transaction count: " & transCt.Value
			Call transCt.Add(one, transCt)
			Print "new transaction count: " & transCt.Value
			' write that back into the field.
			Call fldTransCt.SetNumeric(1, transCt)
			
			' Write a value into the comment field,  translating it from
			' Unicode to the US-ASCII which is used in the database.
			' DB2 doesn't know this is ASCII data; it's just a CLOB. The
			' knowledge about what character set is used is resident in 
			' this application (and others that use the same DB2 table).
			' Since LotusScript doesn't support full pointer operations 
			'(for example mapping a structure onto a string of bytes),
			' you don't have easy access via LotusScript to the binary 
			' data in a Stream object, except if you treat it as a
			' string of characters. But conversions from one character set 
			' to another are quite simple. There are also methods to 
			' extract and replace substrings, merge and truncate string data.
			Set remark = fldRemark.GetStream(1, LCSTREAMFMT_ASCII)
			Print "old remark: " & remark.Text
			Call remark.Convert(newComment, LCCONVERTF_PRESERVE, LCSTREAMFMT_ASCII)
			Print "new remark: " & remark.Text
			Call fldRemark.SetStream(1, remark)
			
			' Write the current date and time into the last 
			' transaction field.
			Call transTime.SetCurrent( )
			Print "transaction time: " & transTime.Text
			Call fldTransTime.SetDatetime( 1, transTime )
			
			Call con.Update(results)
		Wend
	End If
End Sub

Example Output

old balance: 31229.7000
new balance: 31470.6000
old transaction count: 30
new transaction count: 31
old remark: auto-pay AT&T
new remark: Just because we like him...
transaction time: 05/29/2002 02:17:15.14 PM