Results 1 to 24 of 24

Thread: Math sucks!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Question 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.

  2. #2
    Fanatic Member space_monkey's Avatar
    Join Date
    Apr 2005
    Location
    神と歩くこと
    Posts
    573

    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    Yes that's it! But how do I put that to be calculated for a text box (txtCost)

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

    VB Code:
    1. Val(txtCost.Text)
    and so forth with all your controls.

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Math sucks!

    u are thinking too hard! lol

    VB Code:
    1. Private Sub Command1_Click()
    2. txtCost = Val(txtInitCost) * Val(cmbNights.List(cmbNights.ListIndex))
    3. txtCost = Val(txtCost) - ((Val(txtInitCost) / 2) * Val(cmbShared.List(cmbShared.ListIndex)))
    4. End Sub
    5. ' U dont need to do it all in one shot although Space monkeys' looks good..
    6. 'Just do it in 'parts' so you can 'follow' it easier.. makes figuring out formulas easier
    7. Private Sub Form_Load()
    8. cmbNights.AddItem "1"
    9. cmbNights.AddItem "2"
    10. cmbNights.AddItem "3"
    11. cmbNights.AddItem "4"
    12. cmbNights.AddItem "5"
    13. cmbNights.AddItem "6"
    14. cmbShared.AddItem "1"
    15. cmbShared.AddItem "2"
    16. cmbShared.AddItem "3"
    17. cmbShared.AddItem "4"
    18. cmbShared.AddItem "5"
    19. cmbShared.AddItem "6"
    20. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    VB Code:
    1. Me.txtCost = Val(Me.txtInitialCost) * (Val(Me.cmbNights) / Me.txtShare)
    2. Me.txtCost = ((Val(Me.txtInitialCost) * Val(Me.txtShare)) / 2))

    This doesn't work

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Math sucks!

    Quote Originally Posted by vonoventwin
    VB Code:
    1. Me.txtCost = Val(Me.txtInitialCost) * (Val(Me.cmbNights) / Me.txtShare)
    2. 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.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    VB Code:
    1. Me.txtCost = ((Val(Me.txtInitialCost.Text) * (Val(Me.txtShare.Text) / 2))
    Syntax Error

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    Quote 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

  11. #11

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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 Attached Files

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    I'm hungry be back in 30 minutes

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Math sucks!

    Quote 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.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    Wonderful now I will definitely not get any help...

  15. #15
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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:
    1. Private Sub Text0_KeyPress(KeyAscii As Integer)
    2. With Text0
    3.     Select Case KeyAscii
    4.         Case Asc("0") To Asc("9")
    5.         Case 8 'backspace
    6.         Case Else
    7.             KeyAscii = 0
    8.     End Select
    9. End With
    10. 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

  16. #16
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Math sucks!

    Dude, it's simple....

    VB Code:
    1. Dim lngNumNights As Long
    2. Dim lngNumSharedNights As Long
    3. Dim curInitCost as currency
    4. dim curFullCost as Currency
    5. Dim curSharedCost as Currency
    6. dim curCost as Currency
    7. 'Let's break it down.
    8. 'First get the total number of nights
    9. lngNumNights = val(cmbNights.Text)
    10. 'Now Get the Number of Shared Nights
    11. lngNumSharedNights= Val(txtShare.Text)
    12. 'Now figure how many were not shared (this is the full price nights)
    13. lngNumNights = lngNumNights - lngNumSharedNights
    14. 'Get the initial cost price
    15. curInitCost = ccur(txtInitialCost.Text)
    16. 'Ok, so now let's calc the cost of full price nights
    17. curFullCost = lngNumNights * curInitCost
    18. 'Now calculate the cost of the shared nights
    19. curSharedCost = lngNumSharedNights * (curInitCost / 2)
    20. 'Now we have the cost of shared nights and full nights. Add them together
    21. curCost = curFullCost + curSharedCost
    22. txtCost = format$(curCost, "Currency")

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  17. #17
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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:
    1. Private Sub Command90_Click()
    2. Dim InitCost As Double
    3. Dim TotNights As Integer
    4. Dim FullNights As Integer
    5. Dim HalfNights As Integer
    6. Dim FinalCost As Double
    7.    
    8.     InitCost = Val(Me.txtInitialCost)
    9.     TotNights = Val(Me.cmbNights)
    10.     HalfNights = Val(Me.txtShare)
    11.     FullNights = TotNights - HalfNights
    12.     FinalCost = (FullNights * InitCost) + (HalfNights * InitCost / 2)
    13.     Me.txtCost = FinalCost
    14. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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!

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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?

  20. #20
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    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 Attached Files
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    Re: Math sucks!

    I don't really understand the numeric only option. Is that code just allowing numbers only??

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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:
    1. If Me.txtNights.Text = "anything greater then the number 3" Then
    2. Me.cmb1.Visible = False
    3. Me.cmb2.Visible = False
    4. Me.cmb3.Visible = False
    5. Me.txtFrom.Visible = True
    6. Me.txtTo.Visible = True
    7. Me.lblDays.Visible = True
    8. End If

  23. #23
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Math sucks!

    Change
    VB Code:
    1. If Me.txtNights.Text = "anything greater then the number 3" Then
    to
    VB Code:
    1. 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

  24. #24

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Long Island, NY.
    Posts
    353

    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
  •  



Click Here to Expand Forum to Full Width