I'm new to vba and need assistance with problem...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vbanewbie2
    New Member
    • Jul 2010
    • 35

    I'm new to vba and need assistance with problem...

    I have some code already in place but my problem lies with trying to add 4 textboxs (currency) to another textbox5. when i try to add them all together 1 thru 4 it works. but when I try to add 1 thru 3 leaving off #4 it does not work. same thing happens if i leave off textboxs 2 and 3 . can anybody assist me please.

    Code:
    Private Sub cmdenter_click()
    Dim STRTOTAL As Currency
    Dim INTNUM1  As Currency
    Dim INTNUM2 As Currency
    Dim intNUM3 As Currency
    Dim INTANSWER As Currency
    
    
    STRTOTAL = Me.TextBox5.Value
    INTNUM1 = TextBox1.Value
    INTNUM2 = TextBox2.Value
    intNUM3 = TextBox3.Value
    intNUM4 = TextBox4.Value
    
    If INTNUM2 = "" And intNUM3 = "" And intNUM4 = "" Then
    INTANSWER = INTNUM1
    Me.TextBox5.Value = " SUM; " & INTANSWER
    Else
      
    If intNUM3 = "" And intNUM4 = "" Then
    INTANSWER = (INTNUM1 + INTNUM2)
    Me.TextBox5.Value = " SUM; " & INTANSWER
     Else
     
    If intNUM4 = "" Then
    INTANSWER = INTNUM1 + INTNUM2 + intNUM3
    Me.TextBox5.Value = " SUM; " & INTANSWER
    Else
      
     'If STRTOTAL = "" Then
     INTANSWER = INTNUM1 + INTNUM2 + intNUM3 + intNUM4
     Me.TextBox5.Value = " SUM; " & INTANSWER
       End If
       End If
       End If
    End Sub
  • Guido Geurs
    Recognized Expert Contributor
    • Oct 2009
    • 767

    #2
    There is no DIM for num4 !

    Code:
    Dim intNUM4 As Currency
    There is no .value for a Textbox !

    This is working in VB6=

    Code:
    Private Sub cmdenter_click()
    Dim STRTOTAL As Currency
    Dim INTNUM1  As Currency
    Dim INTNUM2 As Currency
    Dim intNUM3 As Currency
    Dim intNUM4 As Currency
    Dim INTANSWER As Currency
       INTNUM1 = Val(Textbox1.Text)
       INTNUM2 = Val(Textbox2.Text)
       intNUM3 = Val(Textbox3.Text)
       intNUM4 = Val(Textbox4.Text)
       If INTNUM2 = 0 And intNUM3 = 0 And intNUM4 = 0 Then
          INTANSWER = INTNUM1
       ElseIf intNUM3 = 0 And intNUM4 = 0 Then
          INTANSWER = (INTNUM1 + INTNUM2)
       ElseIf intNUM4 = 0 Then
          INTANSWER = INTNUM1 + INTNUM2 + intNUM3
       Else
          INTANSWER = INTNUM1 + INTNUM2 + intNUM3 + intNUM4
       End If
       Textbox5.Text = " SUM; " & INTANSWER
       STRTOTAL = INTANSWER
    End Sub
    But also this:

    Code:
    Private Sub cmdenter_click()
       Textbox5.Text = " SUM; " & (Val(Textbox1.Text) + Val(Textbox2.Text) + _
             Val(Textbox3.Text) + Val(Textbox4.Text))
    End Sub

    Comment

    • vb5prgrmr
      Recognized Expert Contributor
      • Oct 2009
      • 305

      #3
      ggeu, in VBA, which the OP states that they are using in the title of this post, there is a .Value property for text boxes as those lovely Form 2.0 controls are what the office products use. Beyond that, good catch on the missing declaration, however, using Val puts a double into the currency variable...
      Val Function


      Returns the numbers contained in a string as a numeric value of appropriate type.

      Syntax

      Val(string)

      The required stringargument is any validstring expression.

      Remarks

      The Val function stops reading the string at the first character it can't recognize as part of a number. Symbols and characters that are often considered parts of numeric values, such as dollar signs and commas, are not recognized. However, the function recognizes the radix prefixes &O (for octal) and &H (for hexadecimal). Blanks, tabs, and linefeed characters are stripped from the argument.

      The following returns the value 1615198:

      Val(" 1615 198th Street N.E.")

      In the code below, Val returns the decimal value -1 for the hexadecimal value shown:

      Val("&HFFFF")

      Note The Val function recognizes only the period (.) as a valid decimal separator. When different decimal separators are used, as in international applications, use CDbl instead to convert a string to a number.
      So, if the textbox contains $1,034.34, Val will return zero (0) while ccur, which is the data type one is putting the values into, and cdbl will return 1,034.34...



      Good Luck

      Comment

      • Guido Geurs
        Recognized Expert Contributor
        • Oct 2009
        • 767

        #4
        Yes; sorry, My mistake.
        I hope this will help (see also attachment)=

        Code:
        Private Sub cmdenter2_Click()
        Dim TEMPTXT As String
        Dim INTNUM1  As Currency
        Dim INTNUM2 As Currency
        Dim intNUM3 As Currency
        Dim intNUM4 As Currency
           TEMPTXT = Replace(TextBox1.Value, "$", "")
           INTNUM1 = CCur(Replace(TEMPTXT, " ", ""))
           
           TEMPTXT = Replace(TextBox2.Value, "$", "")
           INTNUM2 = CCur(Replace(TEMPTXT, " ", ""))
           
           TEMPTXT = Replace(TextBox3.Value, "$", "")
           intNUM3 = CCur(Replace(TEMPTXT, " ", ""))
           
           TEMPTXT = Replace(TextBox4.Value, "$", "")
           intNUM4 = CCur(Replace(TEMPTXT, " ", ""))
           
           TextBox5.Value = " SUM; " & INTNUM1 + INTNUM2 + intNUM3 + intNUM4
        
        End Sub
        Attached Files

        Comment

        Working...