Consulting

Results 1 to 7 of 7

Thread: textbox to integer

  1. #1
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location

    textbox to integer

    hey i am trying to calculate the water/ cementious ratio using a user form and for some reason vba is saving the textbox entry to a text so then why i try to add three inputs together say for 1,1,1 it gives me 111 instead of 3. but i have another input that it will subtract and divide just fine.

    please help

    [VBA]

    Dim course, fine, cement, water1 As Integer

    'calculate
    course = w1 - w1 / (1 + m1 / 100)
    fine = w2 - w2 / (1 + m2 / 100)
    cement = course + fine + cement1 + pozz
    water1 = 8.33 * (addedwater + batchwater + meterwater)
    cwratio1 = water1 / cement
    MsgBox course
    MsgBox fine
    MsgBox water1[/VBA]

    it will calculate the course and fine as integers but the water1 it will give me the "111" instead of 3.

  2. #2
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    you can try CInt on your variables in case one is a string
    ------------------------------------------------
    Happy Coding my friends

  3. #3
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    + is also a concatenation operator on strings
    ------------------------------------------------
    Happy Coding my friends

  4. #4
    VBAX Regular
    Joined
    Dec 2011
    Posts
    16
    Location
    oh that makes sense.

    can you post the code for cint, i am not familiar with that.

  5. #5
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    i assume you have something like
    [VBA]w1 = textbox(blahblahblah)[/VBA]

    change it to
    [VBA]w1 = CInt(textbox(blahblahblah))[/VBA]
    ------------------------------------------------
    Happy Coding my friends

  6. #6
    VBAX Tutor
    Joined
    Jun 2012
    Posts
    269
    Location
    Jhnnyboz,
    Just a note, I see you are using integers, but you may want to consider using doubles instead of integers....

    With an integer, any fractions are rounded. If you want this that is fine, but some of your calculations look like they need decimal values. If you were to change all or some of the variables to doubles, it may give you more accurate calculations.

    Of course you could use cdbl() the same way as you use cint() to convert a string or value to a double.

    Good luck.

  7. #7
    VBAX Expert CatDaddy's Avatar
    Joined
    Jun 2011
    Posts
    581
    Location
    ^^true, i did not even consider that
    ------------------------------------------------
    Happy Coding my friends

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •