473,487 Members | 2,616 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

sum five fields

fnwtech
48 New Member
I have two tasks for the same field. First, I need a field on a form to sum 5 other fields on the form...

The field is named percent. The five fields I want to sum are understanding, quality, communication, completion, preparation, participation.

I keep getting a compilation error. I am trying this on gotfocus. Can someone tell me how I would do this?

The second step is to take the sum and use if else then to convert the sum to a number grade...
Expand|Select|Wrap|Line Numbers
  1. If percent = 30 Then percent =100 elseif percent>=29 then percent=97
this part I have done before, so I think I am set here. I just can't seem to get the sum function to work.
Thanks!
smg
Oct 15 '09 #1
13 1963
ajalwaysus
266 Recognized Expert Contributor
You will need to post the code you are using to do this, for us to help you. It is hard to analyze your issue without seeing the code.

-AJ
Oct 15 '09 #2
fnwtech
48 New Member
Okay, just got it. was thinking I needed to use sum, but because I was using different fields I was getting the error. switched it to percent=[field1]+[field2] etc works. I am set for now... I will see if I get stuck with the rest!
thanks !
Oct 15 '09 #3
fnwtech
48 New Member
Okay, can I then use the else/elseif in the same set of code as the one above or should I create a field for the sum and then a new one for the else if statement? I tried adding it to the same and get an error...
Expand|Select|Wrap|Line Numbers
  1. Percent = [Understanding] + [Quality] + [Communication] + [Completion] + [Preparation] + [Participation]
  2.  
  3. If Percent = 30 Then Percent = 100
  4. ElseIf Percent = 29 Then Percent = 97
  5. ElseIf Percent = 28 Then Percent = 94
  6. ElseIf Percent = 27 Then Percent = 91
  7. ElseIf Percent = 26 Then Percent = 89
  8. ElseIf Percent = 25 Then Percent = 87
  9. ElseIf Percent = 24 Then Percent = 85
  10. ElseIf Percent = 23 Then Percent = 83
  11. ElseIf Percent = 22 Then Percent = 81
  12. etc 
  13. end if
The compile error says Else without if
Oct 15 '09 #4
ajalwaysus
266 Recognized Expert Contributor
OK glad that works, but there is one issue I know of when using the addition sign, if any of the fields you are adding up is null, the entire result will be null, to avoid this I usually write these like this...
Expand|Select|Wrap|Line Numbers
  1. Percent = NZ([field1], 0) + NZ([field2], 0) '...etc
This way nulls are substituted for zeros and the addition should then always work.

-AJ
Oct 15 '09 #5
ajalwaysus
266 Recognized Expert Contributor
Please show us the entire IF code, I can't assume what you have past the "etc", and please put your code in [code] tags.

-AJ
Oct 15 '09 #6
fnwtech
48 New Member
First, thanks for the tip on the null feature. I will fix that.

Here is the whole piece...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Percent_GotFocus()
  2. Percent = [Understanding] + [Quality] + [Communication] + [Completion] + [Preparation] + [Participation]
  3.  
  4. If Percent = 30 Then Percent = 100
  5. ElseIf Percent = 29 Then Percent = 97
  6. ElseIf Percent = 28 Then Percent = 94
  7. ElseIf Percent = 27 Then Percent = 91
  8. ElseIf Percent = 26 Then Percent = 89
  9. ElseIf Percent = 25 Then Percent = 87
  10. ElseIf Percent = 24 Then Percent = 85
  11. ElseIf Percent = 23 Then Percent = 83
  12. ElseIf Percent = 22 Then Percent = 81
  13. ElseIf Percent = 21 Then Percent = 79
  14. ElseIf Percent = 20 Then Percent = 77
  15. ElseIf Percent = 19 Then Percent = 74
  16. ElseIf Percent = 18 Then Percent = 71
  17. ElseIf Percent = 17 Then Percent = 69
  18. ElseIf Percent = 16 Then Percent = 67
  19. ElseIf Percent = 15 Then Percent = 65
  20. ElseIf Percent = 14 Then Percent = 63
  21. ElseIf Percent = 13 Then Percent = 61
  22. ElseIf Percent = 12 Then Percent = 59
  23. ElseIf Percent = 11 Then Percent = 57
  24. ElseIf Percent = 10 Then Percent = 55
  25. ElseIf Percent = 9 Then Percent = 53
  26. ElseIf Percent = 8 Then Percent = 51
  27. ElseIf Percent = 7 Then Percent = 49
  28. ElseIf Percent = 6 Then Percent = 47
  29.  
  30. Else: present = 0
  31.  
  32. End If
  33.  
  34.  
  35. End Sub
Oct 15 '09 #7
ajalwaysus
266 Recognized Expert Contributor
Try This,
Expand|Select|Wrap|Line Numbers
  1. Private Sub Percent_GotFocus()
  2. Percent = [Understanding] + [Quality] + [Communication] + [Completion] + [Preparation] + [Participation]
  3.  
  4. If Percent = 30 Then
  5.     Percent = 100
  6. ElseIf Percent = 29 Then
  7.     Percent = 97
  8. ElseIf Percent = 28 Then
  9.     Percent = 94
  10. ElseIf Percent = 27 Then
  11.     Percent = 91
  12. ElseIf Percent = 26 Then
  13.     Percent = 89
  14. ElseIf Percent = 25 Then
  15.     Percent = 87
  16. ElseIf Percent = 24 Then
  17.     Percent = 85
  18. ElseIf Percent = 23 Then
  19.     Percent = 83
  20. ElseIf Percent = 22 Then
  21.     Percent = 81
  22. ElseIf Percent = 21 Then
  23.     Percent = 79
  24. ElseIf Percent = 20 Then
  25.     Percent = 77
  26. ElseIf Percent = 19 Then
  27.     Percent = 74
  28. ElseIf Percent = 18 Then
  29.     Percent = 71
  30. ElseIf Percent = 17 Then
  31.     Percent = 69
  32. ElseIf Percent = 16 Then
  33.     Percent = 67
  34. ElseIf Percent = 15 Then
  35.     Percent = 65
  36. ElseIf Percent = 14 Then
  37.     Percent = 63
  38. ElseIf Percent = 13 Then
  39.     Percent = 61
  40. ElseIf Percent = 12 Then
  41.     Percent = 59
  42. ElseIf Percent = 11 Then
  43.     Percent = 57
  44. ElseIf Percent = 10 Then
  45.     Percent = 55
  46. ElseIf Percent = 9 Then
  47.     Percent = 53
  48. ElseIf Percent = 8 Then
  49.     Percent = 51
  50. ElseIf Percent = 7 Then
  51.     Percent = 49
  52. ElseIf Percent = 6 Then
  53.     Percent = 47
  54.  
  55. Else: Percent = 0
  56. End If
  57.  
  58. End Sub
Your issue is that you have the assignment of a value after the "Then" on the same line, if I am right this only works if the entire "if then else" was on the same line together, which would be REALLY long and hard to follow.

Also, as a side note, some advice =) When you write this much hard code, you may want to consider storing this in a table or something that way your code won't be so long and won't be so tedious trying to change it if something changes.

Hope this helps,

-AJ
Oct 15 '09 #8
fnwtech
48 New Member
Thanks! that worked. I really need to rebuild the whole database - this was built by someone with even less knowledge then me ;-) and I am just trying to make it work for another year until we have time to tackle it.
Oct 15 '09 #9
NeoPa
32,556 Recognized Expert Moderator MVP
Try the following :
Expand|Select|Wrap|Line Numbers
  1. Private Sub Percent_GotFocus()
  2.   Percent = [Understanding] + [Quality] + [Communication] + _
  3.             [Completion] + [Preparation] + [Participation]
  4.  
  5.   Select Case Percent
  6.   Case 27 To 30
  7.     Percent = 100 - ((30 - Percent) * 3)
  8.   Case 20 To 26
  9.     Percent = 97 - ((30 - Percent) * 2)
  10.   Case 19
  11.     Percent = 74
  12.   Case 6 To 18
  13.     Percent = 95 - ((30 - Percent) * 2)
  14.   Case Else
  15.     Percent = 0
  16.   End Select
  17. End Sub
Failing a method such as AJ suggests (holding the values in a table), the Select Case construct is better for any complicated sets of comparisons.

I'm deeply curious as to why the strange set of values by the way? Why some gaps of 3 but mostly of 2?
Oct 15 '09 #10
NeoPa
32,556 Recognized Expert Moderator MVP
By the way, as a full member now, you should know that we expect your code to be posted in [code] Tags as AJ commented earlier (See How to Ask a Question and indeed the instructions in the posting window itself).

This makes it easier for our Experts to read and understand it. Failing to do so creates extra work for the moderators, thus wasting resources, otherwise available to answer the members' questions.

Please use the tags in future.

Administrator.

PS. Tags are done as matching pairs where the opening one is surrounded by [...] and the closing one by [/...]. A set of buttons is available for ease of use in the Standard Editor (Not the Basic Editor). The one for the [ CODE ] tags has a hash (#) on it. You can choose which editor to use in your Profile Options (Look near the bottom of the page).
Oct 15 '09 #11
fnwtech
48 New Member
@ajalwaysus
After thinking about what you wrote last night, I was curious... if I store the in a table, how would I "call" it into the form? The way this particular database was constructed, they just print off the form instead of using a report. Or is this just more work than it is worth at this point?!
Thanks
Oct 16 '09 #12
ajalwaysus
266 Recognized Expert Contributor
If this is the only place you are writing this, then hard coding would be acceptable, the purpose of storing it in a table is so that you don't have to repeat your code across too many places.
If this code works then I would stick with it, but I would suggest you try NeoPa's code as well, because it is always nice to be able to trim down your code when possible. =)

@fnwtech
To answer your question though, if you ever wanted to do this, a simple way would be to write code like this...

Expand|Select|Wrap|Line Numbers
  1. Private Sub Percent_GotFocus()
  2.   Percent = [Understanding] + [Quality] + [Communication] + _
  3.             [Completion] + [Preparation] + [Participation]
  4.  
  5.   Percent = NZ(DLookup("CalcPercent", "Percent_Comp_Table", "OrigPercent = " & Percent),0)
  6. End Sub
If you had a table named "Percent_Comp_Table" with 2 fields, named "CalcPercent" and "OrigPercent". You would lookup "OrigPercent" based on what you code returns for Percent, and the lookup would then return "CalcPercent" which is the value you wish to assign to Percent.

Let me know if you have any more questions or if this doesn't make sense.

-AJ
Oct 16 '09 #13
fnwtech
48 New Member
I may give the table a try as I don't know the select case coding at all.

Thanks for the assistance.
Oct 16 '09 #14

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

Similar topics

0
1427
by: Rare Book School | last post by:
RARE BOOK SCHOOL (RBS) is pleased to announce its Winter and Early Spring Sessions 2004, a collection of five-day, non-credit courses on topics concerning rare books, manuscripts, the history of...
58
4609
by: Jeff_Relf | last post by:
Hi Tom, You showed: << private const string PHONE_LIST = "495.1000__424.1111___(206)564-5555_1.800.325.3333"; static void Main( string args ) { foreach (string phoneNumber in Regex.Split...
3
25536
by: Kangan | last post by:
Hi, Is there a way to display just the first five rows of the query stt result in DB2. I know that there is something called select Top 5 * from tablename in SQL Server 2000. Is there a wayaround,...
1
1936
by: Emilio | last post by:
(MS Access 2002) Hello, I'm working with some big Census (PUMS) files, and I run into a peculiar problem once the data field exceeds five integers. I'll explain every step, since I am doing it in...
2
2962
by: pierrelap | last post by:
Hello, I need to code a query that: 1-counts the number of time two companies have been in a deal together 2-in the five years that preceded the deal Lead Participant DealDate AAA BBB ...
0
1030
by: Richard Szlachta | last post by:
Readers of python mailing list of all ages and systems, I'd like to introduce you program called ' Piskworky '. It is written in python, uses tkinter gui and is made for playing game known as...
10
14465
by: suhdominic | last post by:
Can anyone help me how to program in C++ which determines the median of five input numbers?
2
2846
by: Saray | last post by:
Hi iam using iis 5. My system is not in domain. Also i enabled my integrated windows authentication. Once i enter into my site it prompt for authentication its fine but while i uploading a big file...
4
2244
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
0
7106
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
7181
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6846
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7349
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
5442
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,...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3071
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
267
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.