Results 1 to 6 of 6

Thread: VB6 Calculation

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    87

    VB6 Calculation

    Hai..I'am using VB6..I want do some calculations..If the weight is 1.2 then X = 2..my code looks like this:

    nilai = $10.00
    If txtWeight.Text > 1 Then x = 2
    If txtWeight.Text > 2 Then x = 3
    If txtWeight.Text > 3 Then x = 4
    If txtWeight.Text > 4 Then x = 5
    If txtWeight.Text > 5 Then x = 6
    If txtWeight.Text > 6 Then x = 7
    .
    .
    .
    .
    If txtWeight.Text > 100 Then x = 1001

    lblPos.Caption = FormatCurrency(nilai * x, 2, vbTrue, vbTrue, vbTrue)

    How can I make the code as simple as possible?

  2. #2
    PowerPoster Nightwalker83's Avatar
    Join Date
    Dec 2001
    Location
    Adelaide, Australia
    Posts
    13,344

    Re: VB6 Calculation

    You could do something along these lines.

    Code:
    Option Explicit
    Dim nilai As Currency, x As Integer
    Private Sub Command1_Click()
    nilai = 10#
    Select Case txtWeight.Text
     Case 1
      x = 2
     Case 2
      x = 3
     Case 3
      x = 4
     Case 4
      x = 5
     Case 5
      x = 6
     Case 6
      x = 7
    End Select
    If txtWeight.Text = x Then x = txtWeight.Text + 1
    End Sub
    
    Private Sub txtWeight_Validate(Cancel As Boolean)
     If Not IsNumeric(txtWeight.Text) Then MsgBox "Please number only numbers!"
    End Sub
    when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    https://get.cryptobrowser.site/30/4111672

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    87

    Re: VB6 Calculation

    if txtWeight.text > 100 so I have to do until case is 100? Wow it's too long again if the value is 1 then X still 1 but if the value is 1.01 then X will become 2.
    Last edited by mdtam; Oct 21st, 2013 at 12:19 AM.

  4. #4
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: VB6 Calculation

    Quote Originally Posted by mdtam View Post
    Code:
    lblPos.Caption = FormatCurrency(nilai * x, 2, vbTrue, vbTrue, vbTrue)
    How can I make the code as simple as possible?
    Code:
    lblPos.Caption = FormatCurrency(nilai * -Int(-CDbl(txtWeight.Text)), 2&, vbTrue, vbTrue, vbTrue)
    See Rounding Up section in How To Implement Custom Rounding Procedures for more details.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: VB6 Calculation

    another way that could work would be to check for the decimal point and if present drop it and add 1
    Code:
    x=instr(txtweight.text)
    If x>0 then
        Weight=Cint(left$(txtweight.text,x-1))+1
    Else
        Weight=Cint(txtWeight.Text)
    End If
    Assuming of course that you are not going to have an entry of x.00

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2013
    Posts
    87

    Re: VB6 Calculation

    Thanks Bonnie West..the code works perfectly..awesome

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