473,408 Members | 2,888 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter_click()
  2. Dim STRTOTAL As Currency
  3. Dim INTNUM1  As Currency
  4. Dim INTNUM2 As Currency
  5. Dim intNUM3 As Currency
  6. Dim INTANSWER As Currency
  7.  
  8.  
  9. STRTOTAL = Me.TextBox5.Value
  10. INTNUM1 = TextBox1.Value
  11. INTNUM2 = TextBox2.Value
  12. intNUM3 = TextBox3.Value
  13. intNUM4 = TextBox4.Value
  14.  
  15. If INTNUM2 = "" And intNUM3 = "" And intNUM4 = "" Then
  16. INTANSWER = INTNUM1
  17. Me.TextBox5.Value = " SUM; " & INTANSWER
  18. Else
  19.  
  20. If intNUM3 = "" And intNUM4 = "" Then
  21. INTANSWER = (INTNUM1 + INTNUM2)
  22. Me.TextBox5.Value = " SUM; " & INTANSWER
  23.  Else
  24.  
  25. If intNUM4 = "" Then
  26. INTANSWER = INTNUM1 + INTNUM2 + intNUM3
  27. Me.TextBox5.Value = " SUM; " & INTANSWER
  28. Else
  29.  
  30.  'If STRTOTAL = "" Then
  31.  INTANSWER = INTNUM1 + INTNUM2 + intNUM3 + intNUM4
  32.  Me.TextBox5.Value = " SUM; " & INTANSWER
  33.    End If
  34.    End If
  35.    End If
  36. End Sub
  37.  
  38.  
Jul 18 '10 #1

✓ answered by Guido Geurs

There is no DIM for num4 !

Expand|Select|Wrap|Line Numbers
  1. Dim intNUM4 As Currency 
There is no .value for a Textbox !

This is working in VB6=

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter_click()
  2. Dim STRTOTAL As Currency
  3. Dim INTNUM1  As Currency
  4. Dim INTNUM2 As Currency
  5. Dim intNUM3 As Currency
  6. Dim intNUM4 As Currency
  7. Dim INTANSWER As Currency
  8.    INTNUM1 = Val(Textbox1.Text)
  9.    INTNUM2 = Val(Textbox2.Text)
  10.    intNUM3 = Val(Textbox3.Text)
  11.    intNUM4 = Val(Textbox4.Text)
  12.    If INTNUM2 = 0 And intNUM3 = 0 And intNUM4 = 0 Then
  13.       INTANSWER = INTNUM1
  14.    ElseIf intNUM3 = 0 And intNUM4 = 0 Then
  15.       INTANSWER = (INTNUM1 + INTNUM2)
  16.    ElseIf intNUM4 = 0 Then
  17.       INTANSWER = INTNUM1 + INTNUM2 + intNUM3
  18.    Else
  19.       INTANSWER = INTNUM1 + INTNUM2 + intNUM3 + intNUM4
  20.    End If
  21.    Textbox5.Text = " SUM; " & INTANSWER
  22.    STRTOTAL = INTANSWER
  23. End Sub
But also this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter_click()
  2.    Textbox5.Text = " SUM; " & (Val(Textbox1.Text) + Val(Textbox2.Text) + _
  3.          Val(Textbox3.Text) + Val(Textbox4.Text))
  4. End Sub

3 1316
Guido Geurs
767 Expert 512MB
There is no DIM for num4 !

Expand|Select|Wrap|Line Numbers
  1. Dim intNUM4 As Currency 
There is no .value for a Textbox !

This is working in VB6=

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter_click()
  2. Dim STRTOTAL As Currency
  3. Dim INTNUM1  As Currency
  4. Dim INTNUM2 As Currency
  5. Dim intNUM3 As Currency
  6. Dim intNUM4 As Currency
  7. Dim INTANSWER As Currency
  8.    INTNUM1 = Val(Textbox1.Text)
  9.    INTNUM2 = Val(Textbox2.Text)
  10.    intNUM3 = Val(Textbox3.Text)
  11.    intNUM4 = Val(Textbox4.Text)
  12.    If INTNUM2 = 0 And intNUM3 = 0 And intNUM4 = 0 Then
  13.       INTANSWER = INTNUM1
  14.    ElseIf intNUM3 = 0 And intNUM4 = 0 Then
  15.       INTANSWER = (INTNUM1 + INTNUM2)
  16.    ElseIf intNUM4 = 0 Then
  17.       INTANSWER = INTNUM1 + INTNUM2 + intNUM3
  18.    Else
  19.       INTANSWER = INTNUM1 + INTNUM2 + intNUM3 + intNUM4
  20.    End If
  21.    Textbox5.Text = " SUM; " & INTANSWER
  22.    STRTOTAL = INTANSWER
  23. End Sub
But also this:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter_click()
  2.    Textbox5.Text = " SUM; " & (Val(Textbox1.Text) + Val(Textbox2.Text) + _
  3.          Val(Textbox3.Text) + Val(Textbox4.Text))
  4. End Sub
Jul 19 '10 #2
vb5prgrmr
305 Expert 100+
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
Jul 22 '10 #3
Guido Geurs
767 Expert 512MB
Yes; sorry, My mistake.
I hope this will help (see also attachment)=

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdenter2_Click()
  2. Dim TEMPTXT As String
  3. Dim INTNUM1  As Currency
  4. Dim INTNUM2 As Currency
  5. Dim intNUM3 As Currency
  6. Dim intNUM4 As Currency
  7.    TEMPTXT = Replace(TextBox1.Value, "$", "")
  8.    INTNUM1 = CCur(Replace(TEMPTXT, " ", ""))
  9.  
  10.    TEMPTXT = Replace(TextBox2.Value, "$", "")
  11.    INTNUM2 = CCur(Replace(TEMPTXT, " ", ""))
  12.  
  13.    TEMPTXT = Replace(TextBox3.Value, "$", "")
  14.    intNUM3 = CCur(Replace(TEMPTXT, " ", ""))
  15.  
  16.    TEMPTXT = Replace(TextBox4.Value, "$", "")
  17.    intNUM4 = CCur(Replace(TEMPTXT, " ", ""))
  18.  
  19.    TextBox5.Value = " SUM; " & INTNUM1 + INTNUM2 + intNUM3 + intNUM4
  20.  
  21. End Sub
Attached Files
File Type: zip I'm new to vba and need assistance with problem_v1.zip (13.2 KB, 43 views)
Jul 22 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: AMT2K5 | last post by:
Hello folks, I seem to have recieved a segfault within my function but am unsure on how to resolve it. I understand that it means that somewhere something is trying to access memory that is not...
1
by: Dalan | last post by:
I have tried both methods of using DSum and creating a Function to address summing some number columns, but to no avail. Since this has been a popular topic over the years, I'm sure I'll receive...
1
by: Dalan | last post by:
I'm experiencing a Query Syntax Error with an Access 97 Db. Actually, the query performs as expected when adding any new records or editing existing ones and even deleting records, EXCEPT when the...
2
by: CSDunn | last post by:
Hello, I need some assistance with error handling in an Access 2003 Project form. The project is has a data source connection to a SQL Server 2000 database. The main form is named...
0
by: Luis Esteban Valencia | last post by:
I've never worked with LDAP before, and I'm having issues connecting to our AD for user authentication. I am trying to use the code found at: ...
0
by: Joe Ross | last post by:
(Apologies in advance if there is a better forum for asking advice on this topic). Our ASP.NET application occasionally starts spitting out OutOfMemory exceptions. When this happens, the memory...
0
by: mwalk66 | last post by:
I have been able to create a web based company phone book by retrieving the last name, first name, email address and telephone # from active directory. However I need serious assistance in...
0
by: serpius | last post by:
Hello Everyone, I am a beginner in VB.Net 2003 and am taking classes in beginning VB.net. To make a long story short, I am looking for real samples of coding in Console Apps. I am the type of...
1
by: Scubadude | last post by:
I am looking for some assistance in 'fine-tuning' my preferences as I set-up my system to learn PHP. I am running Komodo professional, version 3.5.3, build 262321, platform win32-x86. Under...
2
by: quack | last post by:
In SQL Server 2005 I have database mail, I need experienced assistance with a project- when a new record is created send an html message to the new records column "Authorize" , for example...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.