Saturday, December 26, 2009

Conversion Functions


Function Name: Str
Function Description: Returns a String representation of the numeric value passed to it. 
By default it will place a single space in front of the first numeric character.
Syntax: String = Str(Numeric Value)
Examples:
'Proper conversion
Dim strDestination As String
Dim intSource As Integer
intSource = 1
strDestination = Str(intSource)
Function Name: Val
Function Description: Returns a numeric representation of the String value passed to it. 
Val will convert a String to a numeric until it reaches a character that is not a numeric value, a decimal point, or a white-space character. 
Once an unrecognizable character is read, conversion stops at that point.
Common Uses: Val is used much in the same manner as Str, except in the opposite direction.
Syntax: Numeric Value = Val(String)
Examples:

Val(“199.11”) retruns 199.11
Val(“ 199.11 “) retruns 199.11


Function Name: CDate

Function Description: Returns a Date representation of the String value passed to it. 
Common Uses: CDate is used when a Date representation is needed. 
Often a date can be stored in a String when it is gathered from a fixed-width or comma-delimited file. 
If proper operations are going to be performed on the date, then it is necessary to store it in its native format.
Syntax: Date = CDate(String)
Examples:
Dim dteToday As Date
Dim strToday As String
Dim strTomorrow As String
Dim dteTomorrow As Date
strToday = "September 30, 2001"
dteToday = CDate(strToday)
dteTomorrow = DateAdd(DateInterval.Day, 1, dteToday)
strTomorrow = CStr(dteTomorrow)
MsgBox(strTomorrow)

No comments:

Post a Comment