Results 1 to 3 of 3
  1. #1
    jgelpi16 is offline Expert
    Windows XP Access 2007
    Join Date
    Mar 2010
    Location
    Charlotte, NC
    Posts
    544

    Question Calculating Interest with a Module


    I have a public function...
    Code:
    Public Function cmdcalculate(txtFrom, txtTo)
     
        Dim dbs As Database
        Dim rst As Recordset
     
        Dim recStartDate As Date
        Dim recEndDate As Date
        Dim recIntRate As Double
     
        Dim intDays As Integer, testInt As Double, dblInterest As Double, aaa As Date
     
        Set dbs = CurrentDb
     
        Set rst = dbs.OpenRecordset("TblInterest", DB_OPEN_DYNASET)
     
        aaa = txtFrom
        If txtFrom > #12/31/1997# Then
            aaa = DateSerial(Year(txtFrom) + 1, 4, 15)
        End If
        If txtFrom < #12/1/1997# Then
            aaa = DateSerial(Year(txtFrom), Month(txtFrom) + 1, 15)
        End If
        If txtFrom >= #12/1/1997# And txtFrom <= #12/31/1997# Then
            aaa = #1/15/1998#
        End If
        txtFrom = aaa
     
     
        rst.MoveFirst
     
        While Not rst.EOF
            recStartDate = rst!intbdate
            recEndDate = rst!intedate
            recIntRate = rst!Rate / 100
     
            If txtFrom < recStartDate Then
                If txtTo >= recStartDate Then
                    If txtTo > recEndDate Then
                        intDays = DateDiff("d", recStartDate, recEndDate)
                        testInt = (recIntRate * (intDays / 365.25))
                        dblInterest = dblInterest + testInt
                    Else
                        intDays = DateDiff("d", recStartDate, txtTo)
                        testInt = (recIntRate * (intDays / 365.25))
                        dblInterest = dblInterest + testInt
                    End If
                End If
            ElseIf txtFrom >= recStartDate Then
                If txtFrom <= recEndDate Then
                    If txtTo > recEndDate Then
                        intDays = DateDiff("d", txtFrom, recEndDate)
                        testInt = (recIntRate * (intDays / 365.25))
                        dblInterest = dblInterest + testInt
                    Else
                        intDays = DateDiff("d", txtFrom, txtTo)
                        testInt = (recIntRate * (intDays / 365.25))
                        dblInterest = dblInterest + testInt
                    End If
                End If
            End If
            rst.MoveNext
        Wend
        cmdcalculate = dblInterest
    End Function
    ...to calculate interest. However, I have not referenced public functions before and so I need some help. What I would like to accomplish is for the form to calculate the proper interest when the record in the form is selected. Possibly by using the "After Update" even procedure on the form?

  2. #2
    SoftwareMatters is offline Access VBA Developers
    Windows XP Access 2003
    Join Date
    Mar 2009
    Location
    Dorset
    Posts
    274
    If you wanted to run the functuion on the afterupdate then you literally just type it there. Something like this:

    Private Sub Form_Afterupdate
    Me!InterestField= cmdcalculate(txtFrom, txtTo)
    end sub

    I would question where you are going to put this function though. Maybe on a button would be better than the afterupdate of the form.

  3. #3
    SACJO is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Sep 2012
    Posts
    1
    hi everyone,
    please help me , the report i have done was only showing in preview mode the first page, other records were not shown .How to show other records in next page?
    thank a lot
    I'm using access 2007. windows 7

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Calculate Interest
    By jgelpi16 in forum Queries
    Replies: 3
    Last Post: 08-19-2016, 08:01 PM
  2. How do I use a MODULE in a Cross-Tab Query?
    By timo1999 in forum Modules
    Replies: 2
    Last Post: 12-13-2014, 04:51 PM
  3. Calculating Dates
    By TC0126 in forum Queries
    Replies: 1
    Last Post: 02-23-2010, 08:13 PM
  4. Module not found
    By nooby in forum Modules
    Replies: 1
    Last Post: 12-02-2009, 02:20 PM
  5. Calculating the column value
    By access in forum Queries
    Replies: 6
    Last Post: 08-20-2009, 11:51 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums