Normal Topic Write the current record to a CSV file - formatted (Read 3189 times)
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2480
Joined: Aug 20th, 2003
Write the current record to a CSV file - formatted
Jul 6th, 2005 at 3:39pm
Print Post Print Post  
The Code below writes the current record out to a CSV file.  A CSV file is a comma separated values file, just like the ones Sesame writes when you export data.  The values that are written out are formatted depending on what field type(Money, String, Date...) they are bound to. This means that a date will show up as 4/21/2004 instead of 2004/04/21, Money will show up as $416.88 instead of 416.880000, etc, etc.

Code
Select All
** PROGRAMMING SECTION: [Command_Button] [On Element Entry] **
Function FormatNumber(vValue as Double) as String
Var vOut as String
Var vNum as String
Var vLen as Int

	vNum = @Str(VValue)
	While @Right(vNum, 1) = "0"
	{
		vLen = @Len(vNum) - 1
		vNum = @Left(vNum, vLen)
	}
	If @Right(VNum, 1) = "." Then
	{
		vLen = @Len(vNum) - 1
		vNum = @Left(vNum, vLen)
	}
	vOut = vNum

Return vOut
End Function

Function FormatMoney(vValue as Double) as String
Var vOut as String

	vValue = @Rnd(vValue, 2)
	vOut = "$" + @Str(@Decimals(vValue, 2))

Return VOut
End Function

Function FormatYesNo(vValue as Int) as String
Var vOut as String

	If vValue = 0 Then
	{
		vOut = "No"
	}
	Else
	{
		vOut = "Yes"
	}

Return VOut
End Function

Function FormatDate(vValue as Date) as String
Var vOut as String

	vOut = @Str(@Month(vValue)) + "/" + @Str(@Dom(vValue)) + "/" + @Str(@Year(vValue))

Return VOut
End Function

Function FormatTime(vValue as Time) as String
Var vOut as String
Var vTime as Int

	vTime = @ToNumber(vValue)
	If vTime < 720 Then
	{
		If vTime < 60 Then
		{
			vValue = vValue + 720
		}
		vOut = vValue + " AM"
	}
	Else
	{
		If vTime > 779 Then
		{
			vValue = vValue - 720
		}
		vOut = vValue + " PM"
	}

Return VOut
End Function

Var vHandle as Int
Var vData as String
Var vNames as String
Var vElement as String
Var vCnt as Int
Var vLoop as Int
Var vInfo as String

vNames = @StringArrayElementList()
vCnt = @CountStringArray(vNames)
VLoop = 1
vHandle = -1
vData = ""

If FileExists("C:\Sesame\Data\Customer_Out.txt") Then
{
	FileDelete("C:\Sesame\Data\Customer_Out.txt")
}
//Open the file for writing
vHandle = FileOpen("C:\Sesame\Data\Customer_Out.txt")

If vHandle >= 0 Then
{
	While vLoop <= vCnt
	{
		//Get the list of all elements on the form.
		vElement = @AccessStringArray(vNames, vLoop)
		SetThisElement(vElement)
		If ((@ElementType(ThisElement) >= 1000) And (@ElementType(ThisElement) <= 1002)) or ((@ElementType(ThisElement)  >= 1004) And (@ElementType(ThisElement) <= 1008)) Then
		{
			//Element is a Single Line text Box, a MultiLine text box, a combo box, a radio button group, a choice list, a check button or a image box.
			If @Len(vData) > 0 Then
			{
				vData += ","
			}
			If @ElementBoundToType(ThisElement) = 1 Then
			{
				//Bound to a Number field
				vInfo = FormatNumber(ThisElement)
			}
			Else If @ElementBoundToType(ThisElement) = 3 Then
			{
				//Bound to a Time field
				vInfo = FormatTime(ThisElement)
			}
			Else If @ElementBoundToType(ThisElement) = 4 Then
			{
				//Bound to a Date field
				vInfo = FormatDate(ThisElement)
			}
			Else If @ElementBoundToType(ThisElement) = 5 Then
			{
				//Bound to a Money field
				vInfo = FormatMoney(ThisElement)
			}
			Else If @ElementBoundToType(ThisElement) = 9 Then
			{
				//Bound to a Boolean field
				vInfo = FormatYesNo(ThisElement)
			}
			Else
			{
				//No Formatting. String, Images and Keywords.
				vInfo = ThisElement
			}
			vData += @Chr(34) + vInfo + @Chr(34)
		}
		UnSetThisElement()
		vLoop = vLoop + 1
	}
	FileWriteLn(vHandle, vData)
	FileClose(vHandle)
} 



-Ray
« Last Edit: Aug 10th, 2005 at 4:29pm by Ray the Reaper »  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged