473,503 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to Customize an IF Function in vba using a form

14 New Member
hallo experts!
i would like to customize an if function in vba,
that is allow the user to change the condition, value if true and value if false all in a form.
how would i achieve this please!!!

Expand|Select|Wrap|Line Numbers
  1. Grade: IIf([Marks]>=80, "A", _
  2.    IIf([Marks]>=75,"A-", _
  3.       IIf([Marks]>=70,"B+", _
  4.          IIf([Marks]>=65,"B", _
  5.             IIf([Marks]>=60,"B-", _
  6.                IIf([Marks]>=55,"C+", _
  7.                   IIf([Marks]>=50,"C", _
  8.                      IIf([Marks]>=45,"C-", _
  9.                         IIf([Marks]>=40,"D+", _
  10.                            IIf([Marks]>=35,"D", _
  11.                               IIf([Marks]>=30,"D-", _
  12.                                  IIf([Marks]<30,"E")
  13. )))))))))))
Aug 30 '13 #1
6 1187
Seth Schrock
2,965 Recognized Expert Specialist
Are you using VBA or SQL? Your question says VBA, but your code is SQL.

If VBA, then I would recommend setting up a table that would hold a high value and a low value plus the value if true. For example
Expand|Select|Wrap|Line Numbers
  1. HighV   LowV   Grade
  2. 80      76     A
  3. 75      71     A-
  4. 70      66     B+
Then when you enter in a grade value, you just query this table for the record where HighV >= Given Value AND LowV <= Given Value. You can then base a form on this table so that you can change your values and grades.

Just to let you know, your code as written wouldn't work like you expect. As soon as the code gets returned as true, then it doesn't go through the rest of the If statements. So say your grade was 40. Since 40 is less than or equal to 80, it would give you a grade of A and then skip the rest of the ifs. You would have to reverse the order of the numbers (low grades first and high grades last) to be able to do it your way.
Aug 30 '13 #2
zmbd
5,501 Recognized Expert Moderator Expert
sashasandy:
Seth is correct. What you have is an SQL version of this situation.

I personally hate nested IIF(). I would consider using the Switch Function if you going to do this as a calculated field. Becarefull here as the function is evaluated from left to right. The first true statement from left to right will be returned so your logic will have to reflect this.

In VBA would be considering either looking at the Select...Case Statement
OR
If using Seth's method the table and DLookup() Function
The choice would be based upon design. With the table approach, if the grade ranges change, then there is no need to re-code the application and would be the approach I would use if building a grade-book for a teacher.


@Seth:
Just to let you know, your code as written wouldn't work like you expect. As soon as the code gets returned as true, then it doesn't go through the rest of the If statements.
This is not a correct statement. IIF() functions evaluate all of the IIF() conditionals. So in this case, as written by OP, an 85 will give you an A, 64=B-, and so forth. Try it as a calculated field, remember to remove the underscores that I used to step the code in OP.
Sep 2 '13 #3
Seth Schrock
2,965 Recognized Expert Specialist
Oops. As evidenced by my wording I mis-read it to say less than or equal too when it is actually greater than or equal to. So the code would work in the order written.

I am really confused by the fact that the IIF() function evaluates both the true and the false parts as a grade of 90 would return true for each of the conditions so how does it decide to give it an A instead of a D since it evaluated if 90>=35. I really wish I could step through the code like you can in VBA to see that work as that doesn't make sense that any language would use that functionality.

I just googled stepping through SQL code and evidently you can in SQL Server so I'm going to give that a try.
Sep 3 '13 #4
zmbd
5,501 Recognized Expert Moderator Expert
Seth, because as written, it returns the first true condition in the branch. So for 90 (>=80) == "A", even though it evaluated the remaining, only the first level is the most correct. For 56 (>=55) nothing until this level is true and it is the first level returning true, thus the "C+"

It would make more sense if you charted it. :)
Sep 3 '13 #5
sashasandy
14 New Member
Seth! Am so sorry i dint see this thread, i thought no one bothered.But thank you very much....am going to try your method right away.. although i posted it here also: http://goo.gl/spthFy.
I also got a solution using a complex subquery. quite similar with your idea of making a table first.
Well, The challenge with the method i found is: I can only compare one value at a time. that is in this case "Marks". I would also like to get grades for individual Subjects marks.Note: the "Marks" used in this query is the total of all the individual subject scores.

SQL.Satement:
Expand|Select|Wrap|Line Numbers
  1.  SELECT SM.Student_ID
  2.    , SM.Students
  3.    , SM.Marks
  4.    , GD.Grade
  5. FROM tblStudentMarks 
  6.    AS SM 
  7.       INNER JOIN tblGradeDenormalized 
  8.          AS GD 
  9.        ON (SM.Marks>=GD.MinMarks) 
  10.             AND (SM.Marks<=GD.MaxMarks);
ZMBD:-) Thanks for leading me here from the other thread, Actually i think this is the best Method if i get it to work as i want completely.
Sep 5 '13 #6
zmbd
5,501 Recognized Expert Moderator Expert
sashasandy:
Glad this is working for you; however, I'm still not quite sure about your underlying database structure.

Well, The challenge with the method i found is: I can only compare one value at a time. that is in this case "Marks". I would also like to get grades for individual Subjects marks.
Mad Hatter: Clean cup, clean cup. Move down. You'll need a new thread.
If your database is properly normalized you will have no problems with this (^.^)


Alice's Adventures in Wonderland one of my favorite books by LC, would love to find the Screwtape Letters one of these days.
Sep 5 '13 #7

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

Similar topics

7
7121
by: Abhi | last post by:
Hi all, I am using .net for C++ and I would like to write some variable values to some files. I will be using that file in many member functions of the class. So I declared the file variable...
5
4933
by: M Wells | last post by:
Hi All, Is it possible to use form criteria in a query / view in an Access 2003 ADP with SQL Server as the backend? ie something like: select * from mytable where recid = forms!! I'm...
4
1556
by: Samie | last post by:
Hi I have declared a structure that contains pointer to a function. The code goes like this: typedef struct lcd_funs lcd_funs; struct lcd_funs { void (*decode_image)(cyg_uint32 imageWidth,...
0
2065
by: ambikadevi | last post by:
What is the function to call the Postgres stored procedures function using ADODB? PrepareSP() function is used to call the Oracle Stored Procedure. For PHP application. Waiting for the response!
2
2701
by: =?Utf-8?B?VmljdG9yIExhaQ==?= | last post by:
Hi, I wrote an simple application using VS2005 C#. I am trying to call some functions in my other dll file. This dll is written in EVC for running on Windows CE 5.0. This dll file has the...
3
4276
by: 1965 | last post by:
Hi, All. I want to pass a value to asp file on server side but NOT using form post/get. For example: <input id="regionbox" type="hidden" name="region" value="abc"> function dothing ......
3
6448
by: ravitunk | last post by:
hi..i have a javascript function which uses window.open() function to open another window... as below... <script language ="JavaScript"> function openWin(url) { ...
3
1289
nirmalsingh
by: nirmalsingh | last post by:
hai all, How to call a function in Form, from a Class? using c#.net. thanx in advance Nirmal.
8
3588
by: pankaj17 | last post by:
hello, Is it possible to communicate with another domain javascript function using ajax. Suppose i have a iframe and domain is different and in the iframe i have some links. If user clicks on...
0
7202
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
7086
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
7280
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
7332
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...
0
7462
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
5578
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
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
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
1512
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 ...

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.