Results 1 to 4 of 4
  1. #1
    dref is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Jan 2010
    Posts
    55

    Code Issue

    Can anyone help me with this code. Am not an expert but am trying to manipulate this code to compute a loan.
    I have a field on the form called NumberofPayments which is not taken care of in this code. Originally it was designed to use years but i prefer to use months. Please help because when i use forexample numberofpayment=8, it schedules giving me 7 months in the schedule.


    If MsgBox("Are you SURE? This will erase all payment data and create a new schedule?", vbYesNoCancel) <> vbYes Then Exit Sub

    If IsNull(Forms!LoanF!PaymentAmount) Then
    MsgBox "You need to calculate the payment amount first"
    Exit Sub
    End If

    DoCmd.SetWarnings False

    DoCmd.RunSQL ("DELETE * FROM ScheduleT WHERE LoanID=" & Forms!LoanF!LoanID)
    Me.Requery
    DoCmd.GoToControl "PaymentNumber"

    Dim BBal As Currency
    Dim Counter As Long
    Dim CurDate As Date
    Dim X As Integer


    Dim TotalPrin As Currency, Correction As Currency

    BBal = Forms!LoanF!LoanAmount
    Counter = 1

    CurDate = Forms!LoanF![txtOneMonth]
    While BBal > 0
    DoCmd.GoToRecord , , acNewRec
    PaymentNumber = Counter
    Counter = Counter + 1
    DueDate = CurDate
    CurDate = DateAdd("m", 1, CurDate)
    AmountPaid = 0
    RegularPayment = 0
    ExtraPayment = 0
    BeginningBalance = BBal
    'AmountDue = Forms!LoanF!PaymentAmount
    AmountDue = Forms!LoanF!PaymentAmount + (Forms!LoanF!Interest)
    Interest = Round(BeginningBalance * (Forms!LoanF!InterestRate), 2)

    Principal = AmountDue - Interest
    If BBal < Forms!LoanF!PaymentAmount Then
    EndingBalance = 0
    TotalPrin = DSum("Principal", "ScheduleT", "LoanID=" & Forms!LoanF!LoanID) + Principal
    Correction = Forms!LoanF!LoanAmount - TotalPrin
    AmountDue = AmountDue + Correction
    'Principal = Principal + Correction
    Else
    EndingBalance = BeginningBalance - Principal
    End If
    BBal = EndingBalance
    Wend

    Forms!LoanF.Requery

    DoCmd.SetWarnings True

    End Sub

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Please post code between CODE tags to retain indentation and readability.

    Have you step debugged?

    How should NumberOfPayments be utilized?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    It's also curious that NumberOfPayments doesn't even appear in any of the posted code!

    Linq ;0)>
    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 10 Access 2013 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Here's what the code would have looked like with code tags and indenting:
    Code:
    Public Sub Foo()
    
       If MsgBox("Are you SURE? This will erase all payment data and create a new schedule?", vbYesNoCancel) <> vbYes Then Exit Sub
    
       If IsNull(Forms!LoanF!PaymentAmount) Then
          MsgBox "You need to calculate the payment amount first"
          Exit Sub
       End If
    
       DoCmd.SetWarnings False
    
       DoCmd.RunSQL ("DELETE * FROM ScheduleT WHERE LoanID=" & Forms!LoanF!LoanID)
       Me.Requery
       DoCmd.GoToControl "PaymentNumber"
    
       Dim BBal As Currency
       Dim Counter As Long
       Dim CurDate As Date
       Dim X As Integer
       Dim TotalPrin As Currency, Correction As Currency
    
       BBal = Forms!LoanF!LoanAmount
       Counter = 1
    
       CurDate = Forms!LoanF![txtOneMonth]
       While BBal > 0
          DoCmd.GoToRecord , , acNewRec
          PaymentNumber = Counter
          Counter = Counter + 1
          DueDate = CurDate
          CurDate = DateAdd("m", 1, CurDate)
          AmountPaid = 0
          RegularPayment = 0
          ExtraPayment = 0
          BeginningBalance = BBal
          'AmountDue = Forms!LoanF!PaymentAmount
          AmountDue = Forms!LoanF!PaymentAmount + (Forms!LoanF!Interest)
          Interest = Round(BeginningBalance * (Forms!LoanF!InterestRate), 2)
    
          Principal = AmountDue - Interest
          If BBal < Forms!LoanF!PaymentAmount Then
             EndingBalance = 0
             TotalPrin = DSum("Principal", "ScheduleT", "LoanID=" & Forms!LoanF!LoanID) + Principal
             Correction = Forms!LoanF!LoanAmount - TotalPrin
             AmountDue = AmountDue + Correction
             'Principal = Principal + Correction
          Else
             EndingBalance = BeginningBalance - Principal
          End If
          BBal = EndingBalance
       Wend
    
       Forms!LoanF.Requery
    
       DoCmd.SetWarnings True
    
    End Sub

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

Similar Threads

  1. Insert Into code issue
    By Damo10 in forum Macros
    Replies: 2
    Last Post: 07-30-2015, 07:18 PM
  2. Issue with code for DB Connecting
    By techieanalyst in forum SQL Server
    Replies: 24
    Last Post: 08-06-2013, 07:41 AM
  3. Code Issue
    By pbaker in forum Programming
    Replies: 9
    Last Post: 08-07-2012, 07:57 AM
  4. Intermittant VBA Code Issue
    By eking002 in forum Forms
    Replies: 9
    Last Post: 07-10-2012, 02:20 PM
  5. Form Code issue
    By Gavroche in forum Forms
    Replies: 2
    Last Post: 09-10-2011, 09:19 PM

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