Click to See Complete Forum and Search --> : Do While Loop in VB 6
ojones
04-22-2002, 07:10 PM
I have yet another VB 6 question. I have created the code below to calculate an accumulator of income vs expenses with resultant profit or loss (in case of negative number.) I keep getting a runtime error '52', bad file name or number. How do I correct this? I get the error at the Do While Not EOF(1). Thanks everybody. Ken(ojones)
Private Sub cmdCalc_Click()
'declare variables
Dim sngIncome As Single
Dim sngExpense As Single 'accumulator
Dim strIncome As String
Dim strExpense As String 'accumulator
Dim sngProfit As Single
Dim x As Integer
x = x + 1 'counter
'initiate values to 0
sngProfit = 0
sngIncome = 0
sngExpense = 0
strIncome = InputBox("Enter income. Click cancel when done.", "Input")
strExpense = InputBox("Enter expenses. Click cancel when done.", "Input")
Do While Not EOF(1)
x = x <= 3 'accumulator
'create inputboxes to enter values
strIncome = InputBox("Enter income. Click cancel when done.", "Input")
strExpense = InputBox("Enter expenses. Click cancel when done.", "Input")
sngExpense = sngExpense + Val(strExpense) 'accumulator
sngIncome = sngIncome + Val(strIncome) 'accumulator
sngIncome = sngIncome + 1
sngExpense = sngExpense + 1
x = x + 1
Loop
'calculate profits
sngProfit = sngIncome - sngExpense
'display values
txtIncome.Text = Val(sngIncome)
txtExpense.Text = Val(sngExpense)
lblProfit.Caption = Val(sngProfit)
'format as currency
txtIncome.Text = Format(sngIncome, "currency")
txtExpense.Text = Format(sngExpense, "currency")
lblProfit.Caption = Format(sngProfit, "currency")
End Sub
Rhino302
04-22-2002, 08:04 PM
They way I like to do code is with comments all on separate lines from code.
What are you trying to do here:
x = x <= 3 'accumulator
ojones
04-22-2002, 10:47 PM
That code was meaningless. I need a counter + a accumulator. I added x=<=3 by mistake. This is what I have changed my code so far since my last post. It almost works but not quite. I do not know what happens to make my calc go wrong. Your comments in regards to comments is noted!
Thanks, Rhino302
Private Sub cmdCalc_Click()
'declare variables
Dim sngIncome As Single
Dim sngExpense As Single 'accumulator
Dim strIncome As String
Dim strExpense As String 'accumulator
Dim sngProfit As Single
Dim intX As Integer
'initiate values to 0
sngProfit = 0
sngIncome = 0
sngExpense = 0
'create inputboxes for calc values
strIncome = InputBox("Enter income. Click cancel when done.", "Input")
strExpense = InputBox("Enter expenses. Click cancel when done.", "Input")
Do While strIncome <> "" And strExpense <> ""
intX = intX + 1 'update counter
sngIncome = sngIncome + Val(strIncome) 'update accumulator
'obtain value for strincome with inputbox
strIncome = InputBox("Enter income. Click cancel when done.", "Input")
intX = intX + 1 'update counter
sngExpense = sngExpense + Val(strExpense) 'update accumulator
'obtain value for strexpense with inputbox
strExpense = InputBox("Enter expenses. Click cancel when done.", "Input")
Loop
'calculate profits
sngProfit = sngIncome - sngExpense
'format as currency
txtIncome.Text = Format(sngIncome, "currency")
txtExpense.Text = Format(sngExpense, "currency")
lblProfit.Caption = Format(sngProfit, "currency")
End Sub
Rhino302
04-23-2002, 02:00 AM
I would get rid of the loop. Make a command button to trigger the income box. That way, the user has more control on entering figures. One button for income and one for expense.
Have 2 variables each for income and expense. Curreny income and expense, and variables for totals. Assign the inputbox value to the income and expense variables, then add them to the totals variables.
[KMAC] STAN
04-23-2002, 11:20 AM
I dont get it, I have entered your code and created the necessary buttons / text boxes / labels, and as far as I can see it is calculating fine. What is the problem (give me an example of a bad calculation)
I probably would not have gone about it the way you have, I would probably have a couple of list boxes, that display the values entered, one for income, and one for expense, so you can keep adding to either, and have a calculate button to add the totals etc after. This way you can see all the figures you previously entered, so there is no danger of duplication. (Just a thought)
ojones
04-23-2002, 01:34 PM
Thanks guys for your help. I will try both suggestions.
Ken:)
Rhino302
04-23-2002, 10:35 PM
I dont think I understood you and didn't explain right. I was tired when I wrote that reply. Anyways, here's the jist of what I think:
Looping an input box would not be preferred IMO. You've got about 3 choices
1.) Input box like you have. This will reload a new list box every iteration.
2.) Embedd the text boxes in your main form, and let the user type in them. As soon as the user clicks the "Enter" button--this will add the value in the text box to the total figures.
3.) Creat your own list boxes. Make mini-forms, with text boxes, that on "Enter" button click event adds the values to the toals. Cancel button unloads the mini-form. This will, like a list box, allow you to keep entering until you "Cancel out."
I also didn't see really where your code wouldn't work, although I didn't try it. Also, the reason why I suggested 2 variables for Expenses and 2 for Income is because I like to include 'undo' features in my programs. For example:
Private Sub cmdIncomeEnter_Click()
'reference variables on main form
frmMain.sngCurrIncome = txtIncome.text
frmMain.sngTotalIncome = frmMain.sngTotalIncome + frmMain.sngCurrIncome
frmMain.lblTotalIncome = sngTotalIncome
'clear text box
txtIncome.text = ""
End Sub
]Private Sub cmdIncomeUndo_Click()
'reference variables on main form
'deletes last figure entered for income
frmMain.sngTotalIncome = frmMain.sngTotalIncome - frmMain.sngCurrIncome
frmMain.lblTotalIncome = sngTotalIncome
'clear text box
txtIncome.text = ""
End Sub
ojones
04-24-2002, 11:52 AM
Rhino302, thanks for your input. My specs for this project are pretty narrow and the use of inputboxes is required, however, I have yet another dumb question! When does one need to use the Val function and when not, ie does it need to be coded when using inputboxes as well as for caption or text? My boss is driving me crazy with this! Thanks
Rhino302
04-24-2002, 01:36 PM
Val is for making a string into a number. Whener a user types in any kind of text box, it is a string by default. And if the user type '3500', in order to treat that as an integer, you'd have to use val.
ojones
04-24-2002, 07:23 PM
I am truly a dunce, NO! Thanks Rhino302. I looked it up and saw what I should have been doing. i.e. Val(InputBox("Enter income", "Input")).
SysOpt.com
Copyright Internet.com Inc. All Rights Reserved.