Tuesday, September 21, 2010

Visual Basic Date and Time Function : DateAdd

Purpose : The DateAdd function returns a variant (date data ) containing a date to which a specified time interval has been added.

Syntax :
DateAdd ( interval, number, date )

interval : The interval of time you want to add that is string expression ( see in blog )
number : the number of intervals you want to add. also use positive and negative value
date : Date to which the interval is added

The interval argument has these setting
Setting Description
yyyy year
q Quarter
m Month
y Day of year
d day
w weekday
h hour
n Minute
s Secound



DateAdd ("m", 1, " 31-Jan-2010")

Return value is 28-Feb-2010

Visual Basic Date and Time Function : Now

Purpose : The Now function provides the current date and time of the computer system's clock/calendar as date data type.

Syntax :
Now ( )

Option Explicit

Private Sub Timer1_Timer()
'set timer interval
Timer1.Interval = 100
'display the date variant
Text1.Text = Str$ (Now)
'display formatted date and time
Text2.Text = Format$ (Now(), "mmmm-dd-yyyy hh:mm:ss")
End Sub

The Now function has no arguments. the date returned by the Now function represents the system date and time at the moment the code runs.

Visual Basic Date and Time Function : DateValue

Purpose : The dateValue function converts a date in the form of a string into a visual basic date data type . This function changes differently formatted dates to a universal form.

Syntax :
DateValue ( datestring$ )

datestring$ : String that represents a date for the dateValue function to convert.

Examples of valid datestring$ Argument values
Valid String
01/01/2010
01/01/10
January 1, 2010
Jan 1, 1994
01-Jan-2010
01 January 2010

Option Explicit

Private Sub cmdCalculate_Click()
Dim dtmFirstDate As Date, dtmEndDate As Date
Dim vmtDays As Variant
On Error GoTo BadDate
If txtDate(0).Text = "" Or txtDate(1).Text = "" Then
ErrMsgBox "Please enter a date in each box."
Exit Sub
End If
'find date variant of entered date
dtmFirstDate = DateValue(txtDate(0).Text)
'find date variant of entered date
dtmEndDate = DateValue(txtDate(1).Text)
'display date variant
txtDate(0).Text = Str(dtmFirstDate)
'display date variant
txtDate(1).Text = Str(dtmEndDate)
'find number of days between dates
vmtDays = Abs((dtmEndDate - dtmFirstDate))
lblDifference.Caption = "Number of days between dates : " & _
Trim(vmtDays) & " Days"
Exit Sub

BadDate:
ErrMsgBox "Please enter a valid date."
Exit Sub

End Sub

Public Sub ErrMsgBox (massage As String)
MsgBox massage
End Sub

Monday, September 20, 2010

Visual Basic Date and Time Function : DateSerial

Purpose : The DateSerial function converts the numeric values of an indicated date to a Visual Basic date data type.

Syntax :
DateSerial ( year%, month%, day% )

year% : A number or expression that evaluates to between 100 and 9999 inclusive
month% : A number or expression that evaluates to between 1 and 12 inclusive
day% : A number or expression that evaluates to between 1 and 31 inclusive

Option Explicit

Dim thisYear%, thisMonth%, thisDay%, monthsToAdd%
Dim newDate As Date


Private Sub Command1_Click()
'how many months to add to current date
monthsToAdd% = Val(Combo1.Text)
thisYear% = Year(Now())
thisMonth% = Month(Now())
thisDay% = Day(Now())
On Error GoTo baddate
newDate = DateSerial(thisYear%, thisMonth% + monthsToAdd%, thisDay)
Text1.Text = Format$(newDate, "dd-mm-yyyy")
Exit Sub

baddate:
MsgBox "The result date is not acceptable"
Exit Sub

End Sub

Private Sub Form_Load()
Dim i%
For i = 1 To 12
Combo1.AddItem i
Next i
Combo1.ListIndex = 0
End Sub

Visual Basic Date and Time Statements : Date, Date$

Purpose : The Date and Date$ statement enable the user to change the system date of a computer within a visual Basic application.

Syntax :
Date = expression$
Date = datestring$

datestring$: String that represents the date in the form of mm-dd-yy or mm-dd-yyyy
expression$: string that represents the date ( more generalized than datesrting$ )

Acceptable contents of the datestring$ argument
datestring$ Setting
mm a number between 01 and 12 inclusive
dd a number between 01 and 31 inclusive
yy a number between 00 and 99 inclusive
yyyy a number between 1980 and 2079 ( on windows NT; 2099 on windows 95 inclusive

Visual Basic Date and Time Function : Date , Date$

Purpose : The Date function returns the current system date as a date data type and the Date$ function returns the current system data as a string.

Syntax :
Date
Date$

Possible returned setting of the Date$ function
Date Setting Range
Month (mm) a number between 01 and 12 inclusive
Day (dd) a number between 01 and 31 inclusive
Year (yyyy) a year between 0100 and 9999 inclusive

Option Explicit

Private Sub Form_Load()
Dim Msg$
'default message: date as string
Msg$ = "Today's date is : " & Date
'compare date literal
If Date = #9/20/2010# Then
'display date as date data type
Msg$ = "Today is then Twenty of September : " & Date
'compare part of date
ElseIf Format(Now, "dd-mm") = "25-12" Then
'display formatted date as string
Msg$ = " Today is Christmas : " & Format(Now, " dd-mm-yyyy")
End If
'display result
MsgBox Msg$
End Sub

Visual Basic Date and Time Function : CDate

Purpose : The CDate function converts a string or numeric expression into a date date type.

Syntax :
CDate ( expression )

expression : String or number that can be evaluated as a legal date or time

CDate input Handling
Input Value Return Value
Numeric, -657434 to 2958468 Date
Numeric, Out of range Error 13 ( Type Mismatch)
String that looks like a date Date
String that looks like a number Date , if number in range; Error 13 if out of range
Other String expression Error 13 ( Type Mismatch )

Option Explicit

Private Sub Form_Load()
Dim InDate As String, OutDate As Date, Msg, TABSTOP
TABSTOP = Chr$(9)
Do
Msg = "Enter any date and time in any format you like."
InDate = InputBox (Msg)
If InDate = "" Then Exit Sub
If Len(Format(InDate, "mm-dd-yy")) <> 8 Or Format(InDate, "mm-dd-yy") = "01-01-00" Or (Len(InDate) = 8 And Val(InDate) = 0) Then
MsgBox " invalid date ! Try again. ", 48
Else
OutDate = CDate(InDate)
Msg = "You entered : " & TABSTOP & InDate & vbCrLf
Msg = Msg & "Long Form : " & TABSTOP & Format(OutDate, "mmmm dd, yyyy") & vbCrLf
Msg = Msg & "Long Time : " & TABSTOP & Format(OutDate, "hh:MM:ss AM/PM") & vbCrLf
Msg = Msg & "Serial Time: " & TABSTOP & CStr(CDbl(OutDate))
MsgBox Msg
End If
Loop
End Sub

Tuesday, September 14, 2010

Visual Basic Financial Function : SYD

Purpose : The SYD function returns the sum-of-years digits depreciation of an asset.

Syntax :
SYD ( dblInitialCost , dblSalvageValue , dblLifeSpan , dblPeriod )

dblInitialCost : Initial cost of the asset.
dblSalvageValue : Salvage value of the asset.
dblLifeSpan : Total life span of the asset.
dblPeriod : Sequential number representing the current period

For i = 1 to 10
Print SYD ( 10000 , 800 10 , i )
Next i

This function returns the depreciation value for a given period using the sum-of-year' digits algorithm. This gives a weighted depreciation schedule . with a greated amount of depreciation in the beginning than that of the SLN depreciation.

Visual Basic Financial Function : SLN

Purpose : Use the SLN function to return the straight - line depreciation of an asset.

Syntax :
SLN ( dblInitialCost , dblSalvageValue , dblLifeSpan )

dblInitialCost : Initial cost of the assent.
dblSalvageValue : Salvage value of the asset.
dblLifeSpan : Total life span of the asset.

dblyearlyDepreciation = SLN ( 50000 , 2500 , 10 )

The SLN function returns the depreciation value for an asset. you provide the initial caost of the item , the eventual salvage value ( what it is worth after its useful life span ) , and it s total life span. SLN returns the average depreciation.

Monday, September 13, 2010

Visual Basic Financial Function : Rate

Purpose : The Rate function returns the interest rate of an annuity , given a constant periodic series of cash payments and an unvarying interest rate.

Syntax :
Rate ( dblTotalPeriods , dblPayment , dblPresentValue , [ vFutureValue , [ vWhenDue , [ vGuess ] ] ] )

dblTotalPeriods : Total number of periods
dblPayment : Periodic payment.
dblPresentValue :Starting value of the annuity.
vFutureValue : Value of the annuity when it is complete.
vWhenDue : Number specifying whether the payment are due at the beginning (1) or end (0) of the period
vGuess : Estimate for the rate used as a starting point in the first iteration.

dblRate = Rate ( 360 , 600 , -100000 , 0 , 0.1 ) * 12

Visual Basic Financial Function : PV

Purpose : This PV function to derive the present value of an annuity, assuming a constant stream of periodic payments and an unvarying interest rate.

Syntax :
PV ( dblInterestRate , intTotalPeriods , dblPayment , [ vFutureValue , [ vWhenDue ] ] )

dblInterestRate : Interest rate
intTotalPeriods : Total number of payment.
dblPayment : Amount of payment.
vFutureValue : Value of the annuity when it is complete.
vWhenDue : number specifyng whether the payments are due at the deginning (1) or end (0) of the period.

dblPresentValue = PV ( dblRate , 360 , dblPayout , 0 , 0 )

Sunday, September 12, 2010

Visual Basic Financial Function : PPmt

Purpose : The PPmt function returns the amount of principal payment for an annuity, assuming constant periodic payments and an unvarying interest tare.

Syntax :
PPmt ( dblInterestRate , intwhichperiod , intTotalPeriods , vPresentValuse [ , vFutureValue , [ vWhenDue ] ] )

dblInterestRate : Interest Rate.
intwhichperiod : Sequential number representing current period.
intTotalPeriods : Total number of periods.
vPresentValuse : Starting value of then annuity.
vFutureValue : Value of the annuity when it is complete.
vWhenDue : Number specifying whether the payments are due at the beginning (1 ) or end (0 ) of the period.

dblPrincipal = PPmt ( dblRate / 12 , intPeriod , 360 , dblMortageAmount , 0 , 0 )

Visual Basic Financial Function : Pmt

Purpose : The Pmt function to return the total payment amount ( principal and interest ) on an annuity , assuming constant periodic payments and a constant interest rate.

Syntax :
Pmt ( dblInterestRate , intNumberOfPayments , [ vPresentValue , [ vFutureValue , [ vWhenDue ] ] ] )

dblInterestRate : Interest rate.
intNumberOfPayments : Number of Payments.
vPresentValue : Starting value of the annuity.
vFutureValue : Value of the annuity when it is complete .
vWhenDue : Number specifying whether the payments are due at the beginning (1 ) or end ( 0 ) of the period.

dblTetalPayment = Pmt ( dblRate / 12 , intNumPayment , -dblMortgage , 0 , 0 )

Friday, September 10, 2010

Visual Basic Financial Function : NPV

Purpose : The NPV function returns the net present value of a varying series of periodic cash flows ( both negative and positive ) at a given discount rate.

Syntax :
NPV ( dblDiscountRate , dblValuesArray ( ) )

dblDiscountRate : Discount rate
dblValuesArray : Array of cash flows

dblMoney(0) = -50000
dblMoney(1) = -25000
dblMoney(2) = 0
dblMoney(3) = 10000
dblMoney(4) = 30000
dblMoney(5) = 50000
dblMoney(6) = 200000
dblDiscountRate = 0.1 ' ten percent APR

dblReturn = NPV ( dblDiscountRate , dblMoney ( ) )

Visual Basic Financial Function : MIRR

Purpose : The MIRR function returns the modified internal rate of return for a varying series of cash flows.

Syntax :
MIRR (dblValuesArray ( ) , dblFinanceInterestRate , dblReinvestmentInternalRate )

dblValuesArray : Array of cash flows
dblFinanceInterestRate : Interest rate of initial investment
dblReinvestmentInternalRate : Interst rate for reinvested monies

dblMoney(0) = -50000
dblMoney(1) = -25000
dblMoney(2) = 0
dblMoney(3) = 10000
dblMoney(4) = 30000
dblMoney(5) = 50000
dblMoney(6) = 200000
dblFinanceRate = 0.1
dblReinvestRate= 0.8

dblReturn = MIRR ( dblMoney ( ) , dblFinanceRate , dblReinvestRate ) * 100

Visual Basic Financial Function : IRR

Purpose : The IRR function to return the internal rate of return for a series of periodic payments and receipts.

Syntax :
IRR ( dblValuesArray ( ) [ , vGuess ] )

dblValuesArray : Array of cash flows.
vGuess : Estimate for the IRR use as a starting point in the first iteration.

dblMoney(0) = 50000
dblMoney(1) = 25000
dblMoney(2) = 0
dblMoney(3) = 10000
dblMoney(4) = 30000
dblMoney(5) = 50000
dblMoney(6) = 200000

trnVal = IRR ( dblMoney () , 0.20 ) * 100

The IRR function can provide the overall tate of return for an investment that has a varying payment and receipt schedule. the dblValuesArray () argument is a one-dimensional array holding the monetary values . it must have at least one negative amount ( the payment ) and one positive amount ( the receipt ). and we can provide a varying cas flow stream for the function to evaluate.

Thursday, September 9, 2010

Visual Basic Financial Function : IPmt

Purpose : The IPmt function returns the interest portion of a payment on an annuity, given constant payments and a constant interest rate.

Syntax :
IPmt ( dblRate , dblCurrentPeriod , dblNumberTotalPeriods , [ vPresentValue , [ vFunctionValue , [ vWhenDue ] ] ] )

dblRate : Effective interest rate of the annuity.
dblCurrentPeriod :Sequential number representing the current period
dblNumberTotalPeriods : Number of Payments
vFunctionValue :Value of the annuity when it is complete.
vWhenDue : Number specifying whether the payments are due at the beginning(1) or end of the period (0)

interest = IPmt ( dblRate / 12 , dblThisMonth , 360 , vLonaAmount , 0 , 0 )

Wednesday, September 8, 2010

Visual Basic Financial Function : FV

Purpose : Use the FV function to determine the huture value of an annuity based on constant payments and a constant interest rate.

Syntax :
FV (dbRate, intNumPeriods, dblpayment, [ vPresentValue , [ vWhenDue ] ] )

dbRate : Effective interest rate of the annuity.
intNumPeriods : Number of Payments.
dblpayment : Payment amount of the annuity.
vPresentValue : starting value of the annuity .
vWhenDue : Number specifying whether the payment are due at the beginning (1) or end of the period (0)

dblAmountSaved = FV ( dblInterstRate /12 , inNumberMonthes , dblAmountInvestedPerMonth, 0 , 0 )

Wednesday, September 1, 2010

Visual Basic Financial Function : DDB

Purpose : The DDB function returns the depreciation of an asset based on the double-declining balance method.

Syntax :
DDB ( dblCost, dblSalvage, dblLife, dblPeriod, [ vFactor ] )

dblCost : Initial cost of the asset.
dblSalvage : Salvage value of the cost
dblLife : totla life span of the asset
dblPeriod : Period for which you want to determine the depreciation amount of the asset.
vFactor : The rate at which the balance beclines . This argument is optional.

dblPeriodDepreciation = DDB ( dblItemValue, 0 , 30, dblWhichPeriod )