Oct 21st, 2005, 09:31 AM
#1
Thread Starter
Hyperactive Member
Math sucks!
I have a textbox (txtInitialCost), textbox (txtCost), combo box (cmbNights), and a textbox (txtShared).
This is like someone staying at a hotel. There is an initial cost, the number of nights they stay, then number of nights shared. They only pay half price for each night shared (b/c the other person pays the other half).
I want txtCost to calculate a number for me from the values inside the other boxes. Let's say initial cost is $100, number of nights stayed is 2, and number of nights shared is 1.
It would calculate 100X2=200-(100/2) and would give me $150
This is initial value times number of nights = # - (initial cost divided by nights shared)
More help: Nights shared is Initial Cost/2 for each night shared. So if inital cost is 100 for 5 nights and 3 were shared. They pay half cost for 3 nights and full for 2. Which would equal $350 total cost.
100X2=200+(300/2) = $350
This is all screwed up in my head. But do you get what I am trying to do. If not I will try to right more clearly b/c these formulas aren't right.
Oct 21st, 2005, 09:43 AM
#2
Fanatic Member
Re: Math sucks!
I think you are asking for a formula. This should work
(Initial_Cost * (Total_Nights - Nights_Shared)) + ((Initial_Cost/2) * Nights_Shared) = Total_Cost
so for your example
(100 * (5 - 3)) + ((100/2) * 3) = 350
Last edited by space_monkey; Oct 21st, 2005 at 09:47 AM .
Reason: hopefully made it easier to read
Using VB6 or VB.net 2008 with .net 3.5
"Life... death... either way I'll be confined to a small cubicle!" - Hermes Conrad
Oct 21st, 2005, 09:44 AM
#3
Thread Starter
Hyperactive Member
Re: Math sucks!
Maybe this will help:
txtInitalCost = a
cmbNights = b
txtShared = c
x=b-c (this gets number of full price nights)
(a times x) + [(a times c) divided by 2)]
I think this is right
a=100
b=5
c=3
Then
x=2
(100 times 2) + [(100 times 3) divided by 2]
(200) + [(300) divided by 2]
(200) + (150) = $350
Oct 21st, 2005, 09:46 AM
#4
Thread Starter
Hyperactive Member
Re: Math sucks!
Yes that's it! But how do I put that to be calculated for a text box (txtCost)
Oct 21st, 2005, 09:47 AM
#5
Re: Math sucks!
space_monkey's suggestion looks promising. Give that a whirl. Don't forget, however, the data in textboxes and combo boxes are stored as strings.
These need to be converted to numerics before you can perform math on them.
Edit: I didn't see your second post.
You can convert the strings to numbers using Val
and so forth with all your controls.
Oct 21st, 2005, 09:56 AM
#6
Re: Math sucks!
u are thinking too hard! lol
VB Code:
Private Sub Command1_Click()
txtCost = Val(txtInitCost) * Val(cmbNights.List(cmbNights.ListIndex))
txtCost = Val(txtCost) - ((Val(txtInitCost) / 2) * Val(cmbShared.List(cmbShared.ListIndex)))
End Sub
' U dont need to do it all in one shot although Space monkeys' looks good..
'Just do it in 'parts' so you can 'follow' it easier.. makes figuring out formulas easier
Private Sub Form_Load()
cmbNights.AddItem "1"
cmbNights.AddItem "2"
cmbNights.AddItem "3"
cmbNights.AddItem "4"
cmbNights.AddItem "5"
cmbNights.AddItem "6"
cmbShared.AddItem "1"
cmbShared.AddItem "2"
cmbShared.AddItem "3"
cmbShared.AddItem "4"
cmbShared.AddItem "5"
cmbShared.AddItem "6"
End Sub
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
Oct 21st, 2005, 10:14 AM
#7
Thread Starter
Hyperactive Member
Re: Math sucks!
VB Code:
Me.txtCost = Val(Me.txtInitialCost) * (Val(Me.cmbNights) / Me.txtShare)
Me.txtCost = ((Val(Me.txtInitialCost) * Val(Me.txtShare)) / 2))
This doesn't work
Oct 21st, 2005, 10:16 AM
#8
Re: Math sucks!
Originally Posted by
vonoventwin
VB Code:
Me.txtCost = Val(Me.txtInitialCost) * (Val(Me.cmbNights) / Me.txtShare)
Me.txtCost = ((Val(Me.txtInitialCost) * Val(Me.txtShare)) / 2))
This doesn't work
What happens?
Errors?
Incorrect totals?
PS: You don't need the Me if the control you are referencing is on the form. It doesn't do any harm, but it really isn't necessary.
Oct 21st, 2005, 10:19 AM
#9
Thread Starter
Hyperactive Member
Re: Math sucks!
VB Code:
Me.txtCost = ((Val(Me.txtInitialCost.Text) * (Val(Me.txtShare.Text) / 2))
Syntax Error
Oct 21st, 2005, 10:20 AM
#10
Thread Starter
Hyperactive Member
Re: Math sucks!
Originally Posted by
Hack
PS: You don't need the Me if the control you are referencing is on the form. It doesn't do any harm, but it really isn't necessary.
I'm using VBA
Oct 21st, 2005, 10:25 AM
#11
Thread Starter
Hyperactive Member
Re: Math sucks!
It needs work, plus I have no idea how Im gonna figure out the number of nights when there is 3+, but anyway take a look. I's all on Form tblInput
Attached Files
Oct 21st, 2005, 10:29 AM
#12
Thread Starter
Hyperactive Member
Re: Math sucks!
I'm hungry be back in 30 minutes
Oct 21st, 2005, 10:47 AM
#13
Re: Math sucks!
Originally Posted by
vonoventwin
I'm using VBA
I really wished you had said this right from the beginning. There are features and functions in VB that won't work in VBA. Moved To Office Development.
Oct 21st, 2005, 10:55 AM
#14
Thread Starter
Hyperactive Member
Re: Math sucks!
Wonderful now I will definitely not get any help...
Oct 21st, 2005, 11:11 AM
#15
Re: Math sucks!
Von
Don't be so negative...
On the nights greater than 3. Can I suggest that you convert this control to a textbox, i.e. allow the user to type the number of nights?
Don't worry that its a textbox, as you can restrict the value in the text box to numerics only using the attached code for the KeyPress event of the textbox.
VB Code:
Private Sub Text0_KeyPress(KeyAscii As Integer)
With Text0
Select Case KeyAscii
Case Asc("0") To Asc("9")
Case 8 'backspace
Case Else
KeyAscii = 0
End Select
End With
End Sub
Last edited by DKenny; Oct 21st, 2005 at 11:17 AM .
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Oct 21st, 2005, 11:34 AM
#16
Re: Math sucks!
Dude, it's simple....
VB Code:
Dim lngNumNights As Long
Dim lngNumSharedNights As Long
Dim curInitCost as currency
dim curFullCost as Currency
Dim curSharedCost as Currency
dim curCost as Currency
'Let's break it down.
'First get the total number of nights
lngNumNights = val(cmbNights.Text)
'Now Get the Number of Shared Nights
lngNumSharedNights= Val(txtShare.Text)
'Now figure how many were not shared (this is the full price nights)
lngNumNights = lngNumNights - lngNumSharedNights
'Get the initial cost price
curInitCost = ccur(txtInitialCost.Text)
'Ok, so now let's calc the cost of full price nights
curFullCost = lngNumNights * curInitCost
'Now calculate the cost of the shared nights
curSharedCost = lngNumSharedNights * (curInitCost / 2)
'Now we have the cost of shared nights and full nights. Add them together
curCost = curFullCost + curSharedCost
txtCost = format$(curCost, "Currency")
-tg
Oct 21st, 2005, 11:41 AM
#17
Re: Math sucks!
Von
The following code will work,except when the user selects "3+" from the nights, but see my last post on that one.
VB Code:
Private Sub Command90_Click()
Dim InitCost As Double
Dim TotNights As Integer
Dim FullNights As Integer
Dim HalfNights As Integer
Dim FinalCost As Double
InitCost = Val(Me.txtInitialCost)
TotNights = Val(Me.cmbNights)
HalfNights = Val(Me.txtShare)
FullNights = TotNights - HalfNights
FinalCost = (FullNights * InitCost) + (HalfNights * InitCost / 2)
Me.txtCost = FinalCost
End Sub
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Oct 21st, 2005, 11:44 AM
#18
Thread Starter
Hyperactive Member
Re: Math sucks!
So I tried it using the Command 90 button, for a test and I keep getting You can't reference a property or method for a control unless the control has the focus. Your right though, it looks so easy when you make strings out of everything. Thanks!
Oct 21st, 2005, 12:03 PM
#19
Thread Starter
Hyperactive Member
Re: Math sucks!
Ok you got me up and running now. But I really don't understand what to do with the 3+. Just change it to a textbox, then do what?
Oct 21st, 2005, 12:10 PM
#20
Re: Math sucks!
Von
here's you Db with 2 extra forms, one show the textbox with the numeric only option I outlined above. The second uses a spinner contol to change the value in the textbox.
Attached Files
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Oct 21st, 2005, 12:27 PM
#21
Thread Starter
Hyperactive Member
Re: Math sucks!
I don't really understand the numeric only option. Is that code just allowing numbers only??
Oct 21st, 2005, 12:31 PM
#22
Thread Starter
Hyperactive Member
Re: Math sucks!
Ok I'm going to change it to a numeric option only. I think I'm following you. How would I write the code to say:
VB Code:
If Me.txtNights.Text = "anything greater then the number 3" Then
Me.cmb1.Visible = False
Me.cmb2.Visible = False
Me.cmb3.Visible = False
Me.txtFrom.Visible = True
Me.txtTo.Visible = True
Me.lblDays.Visible = True
End If
Oct 21st, 2005, 01:16 PM
#23
Re: Math sucks!
Change
VB Code:
If Me.txtNights.Text = "anything greater then the number 3" Then
to
VB Code:
If Val(Me.txtNights.Text)>3 Then
Declan
Don't forget to mark your Thread as resolved.
Take a moment to rate posts that you think are helpful
Oct 21st, 2005, 01:22 PM
#24
Thread Starter
Hyperactive Member
Re: Math sucks!
TY Aweseom now I'm done until Tuesday, CYA then!
Posting Permissions
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
Forum Rules
Click Here to Expand Forum to Full Width