Visual Basic

Started by
9 comments, last by jedi2832 20 years, 6 months ago
Private Sub cmdExit_Click() End End Sub Private Sub cmdPurchase_Click() Dim x As Integer y = 0 x = Val(InputBox("Enter Your Age: ")) If x < 12 And x >= 1 Then MsgBox ("$ ") & 5.5 y = y + 1 ElseIf x > 55 And x <= 200 Then MsgBox ("$ ") & 6.5 y = y + 1 ElseIf x >= 12 And x <= 55 Then MsgBox ("$ ") & 7.5 y = y + 1 Else MsgBox "Error!", vbCritical End If MsgBox y End Sub Private Sub Form_Activate() Dim y As Integer End Sub in this code i try to get the y to add up, so every time a pop window comes up to ask your age, u enter ur age, and it converts how much money it cost, but i cant seem to add up the money, and tell the user, how much money total it gonna cost. plz help
Advertisement
You need to define y as a public variable.
At teh moment, you get a new y every time you click the Purchase Button.
remove the Dim y as integer from teh activate sub, and then add Public y as integer at teh top of your form code OUTSIDE ALL SUBS

look up variable scope is any good programming textbook/intenet site etc....



-----------

Does it matter? Even if it does matter, does it matter that it matters?
---------Does it matter? Even if it does matter, does it matter that it matters?
i did what u said, but still i didnt add them up
Whats the new code?

Does it matter? Even if it does matter, does it matter that it matters?
---------Does it matter? Even if it does matter, does it matter that it matters?
Alternatively, just below
Dim x As Integer

you could declare:
Static y as integer

either way, get rid of "y=0"

ld
No Excuses
from that code y will always =
a msgbox will appear telling you how much!!

what is the point of Y?

do you want y to hold this info all the time?

if so declare it like this

static y as integer

then everytime the code is excuted y will hold the value of the time before.

posted the same time as above

[edited by - themonkster on October 13, 2003 6:40:40 PM]
Dim y As Integer
Private Sub cmdExit_Click()
End
End Sub

Private Sub cmdPurchase_Click()
Dim x As Integer
x = Val(InputBox("Enter Your Age: "))
If x < 12 And x >= 1 Then
MsgBox ("$ ") & 5.5
y = y + 5.5
ElseIf x > 55 And x <= 200 Then
MsgBox ("$ ") & 6.5
y = y + 6.5
ElseIf x >= 12 And x <= 55 Then
MsgBox ("$ ") & 7.5
y = y + 7.5
Else
MsgBox "Error!", vbCritical
End If
MsgBox y
End Sub

Private Sub Form_Activate()
Static y As Integer
End Sub

this time it worked, thx alot guys
you realize that you aren''t adding up the money. All you are doing is adding amount of customers.

If it''s still not working post your new code.
one more thing, every time 3 of those number added, the computer always rounded off, not leaving a .5, is there anyway i can have it leave a .5 when odd numbers are added, thx for replying
instead of "dim as integer"

try "dim as double"

I think that is what you mean.

This topic is closed to new replies.

Advertisement