Urgent help on VB structure

lscheer

Registered User.
Local time
Today, 18:20
Joined
Jan 20, 2000
Messages
185
I am currently trying to learn how to program in VB and I'm stuck. Here is the code I am trying to work with, but my calculation doesn't work, when I click Calculate, I just get $0.00.

Any help would be great!!! Thanks!
Code:
Option Explicit

    Const mcurRate As Currency = 12.25
    Dim mcurTotalAmount As Currency
    Dim mintCount As Integer
    Dim msngAverage As Single

Private Sub cmdCalculate_Click()
 
 Dim curAmount As Currency
 Dim intHours As Integer
  
    'Convert Text to Value
    curAmount = Val(txtAmount.Text)
    
    'Calculate Amount
    curAmount = intHours * mcurRate
  
    'Format calculation values
   
    txtAmount.Text = FormatCurrency(curAmount)
    txtRate.Text = FormatCurrency(mcurRate)
    

End Sub

Private Sub cmdClear_Click()
    txtHours = ""
    txtRate = ""
    txtAmount = ""
    txtTotalAmount = ""
    txtCount = ""
    txtAverage = ""
    txtHours.SetFocus

End Sub

Private Sub cmdEnd_Click()
    End
End Sub

Private Sub cmdTotals_Click()
    
    'Calculate summary values
    mcurTotalAmount = mcurTotalAmount + mcurAmount
    mintCount = mintCount + 1
    msngAverage = mcurTotalAmount / mintCount
    
    'Format summary values
    mcurTotalAmount = Val(txtTotalAmount.Text)
    txtTotalAmount.Text = FormatCurrency(mcurTotalAmount)
    msngAverage = Val(txtAverage.Text)
    txtAverage.Text = FormatCurrency(msngAverage, 2)
    
    
End Sub
 
Last edited by a moderator:
In the click event of cmdTotals you seem to have a variable which is undeclared - mcurAmount. This variable is always going to be empty, since you don't assign it a value anywhere in the code.
Are you having problems with the cmdCalculate or cmdTotals click event, because the calculate button returns a value in txtAmount field?

Rich
 
Ah, yes, I had forgotten to change mcurAmount to curAmount and redefine it for that subprocedure. However, I am (so far) having trouble with the calculation in the Calculate_Click procedure (I haven't made it past there, and the Totals_Click procedure relies on results from the Calculate_Click so I don't know yet if that part of the code is having problems, too).
 
I've tried executing your code for the calculate button. I put 5 in txthours and 20 for txtAmount. The calculation results in £61.25 in txtAmount and £12.25 in rate (obviously as it is constant). Is this what you want to happen?
curAmount = intHours * mcurRate
at the moment you are storing the result of the above in txtAmount is this where you want it?

Rich
 
Yes, the value I'm calculating (curAmount) is supposed to be like a total wage, where Rate is the hourly wage, thus intHours x curRate = curAmount.
 
I've gotten the first calculation to work! I think it was just a matter of not having converted all my text values with the Val() function. Now, onto the totals calculations...
 
In the code below I think you've got it back to front. txtTotal amount is null, so you're assigning the null value to you're current total.

mcurTotalAmount = Val(txtTotalAmount.Text)

change to:

txtTotalAmount.Text = mcurTotalAmount

I think you're doing the same with the average, assigning it back to zero in the penultimate line

HTH

Rich

[This message has been edited by Howlsta (edited 06-07-2002).]

[This message has been edited by Howlsta (edited 06-07-2002).]
 
Thanks for pointing that out, I think you're right. But somehow, the Totals calculations still aren't working.

Here is my current (revised) code:

Private Sub cmdTotals_Click()

Dim curAmount As Currency

txtTotalAmount.Text = mcurTotalAmount
txtAverage.Text = mcurAverage

'Convert Text to Value
curAmount = Val(txtAmount.Text)

'Calculate summary values
mcurTotalAmount = mcurTotalAmount + curAmount
mintCount = mintCount + 1
mcurAverage = mcurTotalAmount / mintCount

'Format summary values
txtTotalAmount.Text = FormatCurrency(mcurTotalAmount)
txtAverage.Text = FormatCurrency(mcurAverage, 2)


End Sub
 
Thanks for all your help. I had to turn in my assingment already...but I think I worked out most of the kinks.

Just a side note to Rich's last post: I forgot to say that this code is actually in VB, not in Access, so the txt boxes in VB need to be converted to accommodate numerics.
 

Users who are viewing this thread

Back
Top Bottom