I face two issues with the code below for a Loan Calc application, my first, and they relate to:
1. I would like all textboxes to display with proper format, however the Rate Function applied here will halt the operation after the initial calcuation if I then decide to clear any of the 1 to 6 variables, and re-enter a new factor.
THe appl. will then zero out the Other Costs box, &/or halt due to an invalid procedure call.
Below is described the changes made to get the operation to work, but I would nevertheless like to learn how to format the Price and Other Costs textboxes as finally displayed in the form.
2. I also have remmed out the Keystroke sections as these are not permiting me to use the Debug feature to test the application.
THe form has other uncoded as yet features that I will add later.
I have resolved the conflicit in the latest Loan Calc VB6 application. I was formatting the Price Textbox which is the kingpin in the calculations, and so when I clear any of the 6 variables numbered 1 through 6 on the left of each variable either the Rate function formula would halt the operation as an invalid porcedure call, or the Other COsts textbox would be zeroed out.
I now changed the formatting in the code for the Price, and the Other Costs textboxes and all is well again.
Option Explicit
Private Sub CmdCalculate_Click()
Dim Price As Currency
Dim DwnPmt As Double
Dim principal As Currency
Dim fRate As Double
Dim Term As Long
Dim payment As Currency
Dim Points As Double
Dim Costs As Currency
Dim PmtwCosts As Currency
Dim APR As Single
Dim Loanwcosts As Currency
Dim FutureValue As Currency
Dim FVal As Currency
Dim PayType As Integer
Dim Guess As Double
If Trim(TxtPrice.Text) = "" Then
MsgBox "Enter a Price"
TxtPrice.SetFocus
Exit Sub
End If
If Trim(TxtDwnPmt.Text) = "" Then
MsgBox "Enter a Down Payment"
TxtDwnPmt.SetFocus
Exit Sub
End If
If Trim(TxtfRate.Text) = "" Then
MsgBox "Enter an Interest Rate"
TxtfRate.SetFocus
Exit Sub
End If
If Trim(TxtTerm.Text) = "" Then
MsgBox "Enter a Term"
TxtTerm.SetFocus
Exit Sub
End If
If Trim(TxtPoints.Text) = "" Then
MsgBox "Enter the Points"
TxtPoints.SetFocus
Exit Sub
End If
If Trim(TxtCosts.Text) = "" Then
MsgBox "Enter the additional Costs"
TxtCosts.SetFocus
Exit Sub
End If
Price = Val(TxtPrice.Text) 'this is the purchase price
'TxtPrice = FormatCurrency(Price)
DwnPmt = Val(TxtDwnPmt.Text) / 100 'converts the dwon payment to value based on the purchase price
TxtDwnPmt = Format(DwnPmt, "#0.00000%")
principal = (Price * (1 - DwnPmt)) 'determines the loan amount after the down payment
TxtPrincipal = FormatCurrency(principal)
fRate = Val(TxtfRate.Text) / 100
fRate = fRate / 12 'Converts the annual interest rate to a monthly rate
TxtfRate.Text = Format(fRate * 12, "#0.00000%")
Term = Val(TxtTerm.Text) * 12 'Converts the life of the loan from years to months
payment = principal * (fRate / (1 - (1 + fRate) ^ -Term)) 'Computes the Monthly Payment & interest
TxtPayment.Text = Format(payment, "$###,000.00")
Points = Val(TxtPoints.Text) / 100 'Points are often paid to get favorable Interest Rates
TxtPoints = FormatPercent(Points)
Costs = Val(TxtCosts.Text) 'This covers all types of closing loan costs & other concepts
'TxtCosts = FormatCurrency(Costs)
PmtwCosts = principal + (principal * Points) + Costs ' This includes Intial Pmt + all other costs
Loanwcosts = PmtwCosts * (fRate / (1 - (1 + fRate) ^ -Term)) ' This calculates the Pmt with all other costs
TxtPmtWCosts = FormatCurrency(Loanwcosts)
FVal = 0 'Normally for this exercise the Future Value is zero
PayType = 0 ' 0 = Payment at end of Month; 1 = Payment at the beginning of the Month
Guess = InputBox("What is your best guess-estimate of the Interest Rate expressed as a decimal {For example 10% would be .10}?") 'Prompts for Guess Rate
APR = (Rate(Term, -Loanwcosts, principal, FVal, PayType, Guess) * 12) * 100 'Computes APR
TxtAPR.Text = Format(APR / 100, "#0.00000%")
Private Sub CmdClearDwnPmt_Click()
TxtDwnPmt.Text = vbNullString
End Sub
Private Sub CmdClearOCosts_Click()
TxtCosts.Text = vbNullString
End Sub
Private Sub CMdClearPoints_Click()
TxtPoints.Text = vbNullString
End Sub
Private Sub CmdClearPrice_Click()
TxtPrice.Text = vbNullString
End Sub
Private Sub CmdClearRate_Click()
TxtfRate.Text = vbNullString
End Sub
Private Sub CMdClearTerm_Click()
TxtTerm.Text = vbNullString
End Sub
Private Sub CmdExit_Click()
Unload Me
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
If MsgBox("Are you sure that you'd like to exit?", vbInformation + vbYesNo, App.Title) = vbYes Then
End
Else
Cancel = 1
End If
End Sub
'Private Sub TxtRate_KeyPress(KeyAscii As Integer)
'Select Case Chr(KeyAscii)
' Case "0" To "9" 'Allow numbers
' Case "." 'Allow "."
' Case "," 'Allow ","
' Case vbBack 'allows backspace to function
' Case Else
' KeyAscii = 0
'End Select
'End Sub
'Private Sub TxtPrincipal_KeyPress(KeyAscii As Integer)
'Select Case Chr(KeyAscii)
' Case "0" To "9" 'Allow numbers
' Case "." 'Allow "."
' Case "," 'Allow ","
' Case vbBack 'allows backspace to function
' Case Else
' KeyAscii = 0
'End Select
'End Sub
'Private Sub TxtTerm_KeyPress(KeyAscii As Integer)
'Select Case Chr(KeyAscii)
' Case "0" To "9" 'Allow numbers
' Case "." 'Allow "."
' Case "," 'Allow ","
' Case vbBack 'allows backspace to function
' Case Else
' KeyAscii = 0
'End Select
'End Sub
'Private Sub Txtmax_Change()
'LenMax = Val(Txtmax.Text)
'End Sub
it is not clear from the code you posted what is going on. When you run the app in the debugger, because you do not have error handling the debugger should stop at the offending line of code, and from there you can peek the variables (just hover the mouse on top of them, or use the Locals dialog). You should be able to see which variable has a wrong value, and is causing you trouble. It is possible that because you are formatting the values in the textBoxes, the Val functions does not return what you are expecting. So maybe it is a formatting problem. When you format data you must be sure that 1) you can read it again or 2) you never read it again
Let us know,
Marco
PS there is no need for a End statement in the QueryUnload event. End can be very bad when the app is allocating resources, or connecting to other machines, or opening files, because it just kills the app without giving it the time to clear the resources. This is not your case, but still it is a good programming practice
"There are two ways to write error-free programs. Only the third one works."
Unknown
If you open the zip file that I attached to my inquiry, and run the loan calc application you will note my problem with the formatting. The Price and the Other Costs texboxes on the completed form, once you enter all the 6 variables, and click on the Calculate Choice Command button, and provide the guess rate of .10., appear to be not properly formatted.
Both fields appears with without the $ sign, and the decimals of (.00).
This is what I want to cure since I strongly believe all values should be properly formatted.
Now to cover your topic of using the debug feature:
If you remove the rems from any of the section, for example this one:
'Select Case Chr(KeyAscii)
' Case "0" To "9" 'Allow numbers
' Case "." 'Allow "."
' Case "," 'Allow ","
' Case vbBack 'allows backspace to function
' Case Else
' KeyAscii = 0
'End Select
'End Sub
IF you then try to step through the process using the Debug the application will knock you out to the code section and insert whether you may have typed as the first variable's value.
In-other-words this coding will not allow you to Debug the process.
This is my 2nd problem.
I would like to solve for both. The application as coded works well with any set of variables &/or changes to these variables.
I simply want to learn how to format for the two variables as displayed in the form when the process is completed. If I introduce the formatting for the Price as 'TxtPrice = FormatCurrency(Price) Please notice that both variables have their formatting remmed out.
If this rem is removed the application will initially work correctly, but the moment you change any of the variables the value of Price becomes zero in the Rate Function and an invalid procedure call halts the process. IF the Formatting that is for the Other Costs has its rem removed, then the results after one of the variables is changed becomes zero.
debugging key events is tricky, because as soon as the IDE step in the app loses its focus. I usually put a breakpoint in the lines that I want to stop, and then let the app runs. When it stops, I go through step by step, but I hit F5 before the end of the current procedure, because I do not want to step in key events...
In your case, I'd put a single breakpoint at the beginning of the CmdCalculate_Click method, without 'remming' the events (I had to dig deep in my memory to remember what 'rem' means
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
actually I did not load you code, I said that what I "would do is...". I do enough debugging in my own programs to debug someone else code... Any line at the beginning of your CmdCalculate_Click method will do.
Have fun
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
Think a little about it. The app is running, a TextBox has the focus, you hit a key, and the key event is called. if the debugger is set 'step-by-step', the debugger will stop and grab the focus. At this point any other key stroke will end up in the editor window.
To avoid that, put a breakpoint NOT in a key event, and just hit Ctrl+F5 (run with full compile). At this point the app will start and stop ONLY at the breakpoint, and NOT in the event callbacks.
Now, and this is interesting, if you are in the breakpoint and you hit F8 you step line-by-line. If you hit F8 at the end of the method, the debugger is STILL in step-bystep mode, so if you hit a key the debugger will stop in the key event... to avoid this press F5 (continue) BEFORE you are at the end of the method where the breakpoint is. And if by mistake end up in a key event, just hit F5 (continue)
If you think that this is complicated, be grateful: the VB debugger is one of the best (not the best, but for sure one of the best)
Marco
"There are two ways to write error-free programs. Only the third one works."
Unknown
It is this section of the coding that I am talking about:
'Private Sub TxtRate_KeyPress(KeyAscii As Integer)
'Select Case Chr(KeyAscii)
' Case "0" To "9" 'Allow numbers
' Case "." 'Allow "."
' Case "," 'Allow ","
' Case vbBack 'allows backspace to function
' Case Else
' KeyAscii = 0
'End Select
'End Sub
There is a section of this coding for each textbox to insure that the proper info is keystroked into the form.
I can't get the Debugger (which I admit is very good, as it has permited me to find all the errors todate in this application)
If these section are not remmed out, and I place the Debug Step through into action I am immediately kicked out, and find that whether i entered in the Price textbox in the form will be prefixed to the first line of code in these keystroke sections.
I will nevertheless place as you suggest a breakpoint after the first input called for namely the Price, and see what it tell me.
Thanks again. I don't find the Debugger complicated at all.
Private Sub Command1_Click()
Dim dblResult As Double
' Do your calculation using the value of the variable(dblTmp)
' and not the content of the textbox(text1.text)
' Ex.
dblResult = dblTmp * (50 / 100)
' Return your result to the textbox(text1.text)
' Remembering to update your variable(dblTmp)
dblTmp = dblResult
Text1.Text = Format(dblTmp, "$#,##0.00")
End Sub
Private Sub Form_Load()
dblTmp = 100
Text1.Text = Format(dblTmp, "$#,##0.00")
End Sub
Private Sub Text1_GotFocus()
Text1.Text = Format(dblTmp, "0.00")
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If ((KeyAscii >= vbKey0) And (KeyAscii <= vbKey9)) Or (KeyAscii = vbKeyBack) Then
' Allow
ElseIf (Chr(KeyAscii) = ".") Then
If (InStr(Text1.Text, ".") > 0) Then
KeyAscii = 0
End If
Else
KeyAscii = 0
End If
End Sub
Private Sub Text1_LostFocus()
If (Text1.Text = "") Then
dblTmp = 0
Else
dblTmp = Text1.Text
End If
Text1.Text = Format(dblTmp, "$#,##0.00")
End Sub
principal = (dblResult * (1 - DwnPmt)) 'determines the loan amount after the down payment dblCosts = dblCTmp
dblCTmp = dblCosts
TxtCosts.Text = Format(dblCTmp, "$#,##0.00")
Costs = Val(TxtCosts.Text) 'This covers all types of closing loan costs & other concepts
'TxtCosts = FormatCurrency(Costs)
PmtwCosts = principal + (principal * Points) + dblCosts ' This includes Intial Pmt + all other costs
Private Sub Form_Load() dblTmp = InputBox("What is the Price to be Paid?") 'Prompts for Price
TxtPrice.Text = Format(dblTmp, "$#,##0.00") dblCTmp = InputBox("What are the Other Costs to be incurred in this loan?") 'Prompts for Costs
TxtCosts.Text = Format(dblCTmp, "$#,##0.00")
End Sub
Private Sub TxtPrice_GotFocus()
TxtPrice.Text = Format(dblTmp, "0.00")
End Sub
'Private Sub Form_Load()
'dblCTmp = InputBox("What are the Other Costs to be incurred in this loan?") 'Prompts for Costs
'TxtCosts.Text = Format(dblCTmp, "$#,##0.00")
'End Sub
Private Sub TxtCosts_GotFocus()
TxtCosts.Text = Format(dblCTmp, "0.00")
End Sub
Bookmarks