473,706 Members | 2,429 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

is there an easier way to write the following If Statement?

1 New Member
Expand|Select|Wrap|Line Numbers
  1. If Me.PD = 0 And Me.GA = 0 And Me.BO = 0 And Me.EL = 0 And Me.CH = 0 Then
  2.     Me.Cover = ""
  3.     Me.CPPrice = 0
  4. ElseIf Me.PD = 0 And Me.GA = 0 And Me.BO = 0 And Me.EL = -1 And Me.CH = 0 Then
  5.     Me.Cover = ""
  6. ElseIf Me.PD = 0 And Me.GA = -1 And Me.BO = 0 And Me.EL = -1 And Me.CH = 0 Then
  7.     Me.Cover = ""
  8.     Me.CPPrice = 0
  9. ElseIf Me.PD = -1 And Me.GA = 0 And Me.BO = 0 And Me.EL = 0 And Me.CH = -1 Then
  10.     Me.Cover = ""
  11.     Me.CPPrice = 0
  12. ElseIf StrPD = -1 And StrGA = 0 And StrBO = 0 And StrEL = 0 And StrCH = 0 Then
  13.     Me.Cover = "PD"
  14.     Me.CPPrice = 119.88
  15. ElseIf StrPD = -1 And StrGA = -1 And StrBO = 0 And StrEL = 0 And StrCH = 0 Then
  16.     Me.Cover = "PDG"
  17.     Me.CPPrice = 143.88
  18. ElseIf StrPD = -1 And StrGA = 0 And StrBO = 0 And StrEL = -1 And StrCH = 0 Then
  19.     Me.Cover = "PDE"
  20.     Me.CPPrice = 167.88
  21. ElseIf StrPD = -1 And StrGA = -1 And StrBO = 0 And StrEL = -1 And StrCH = 0 Then
  22.     Me.Cover = "PDEG"
  23.     Me.CPPrice = 179.88
  24. ElseIf StrPD = -1 And StrGA = 0 And StrBO = -1 And StrEL = 0 And StrCH = 0 Then
  25.     Me.Cover = "PDB"
  26.     Me.CPPrice = 227.88
  27. ElseIf StrPD = -1 And StrGA = -1 And StrBO = -1 And StrEL = 0 And StrCH = 0 Then
  28.     Me.Cover = "PDBG"
  29.     Me.CPPrice = 239.88
  30. ElseIf StrPD = -1 And StrGA = 0 And StrBO = -1 And StrEL = 0 And StrCH = -1 Then
  31.     Me.Cover = "PDBCH"
  32.     Me.CPPrice = 251.88
  33. ElseIf StrPD = -1 And StrGA = 0 And StrBO = -1 And StrEL = -1 And StrCH = 0 Then
  34.     Me.Cover = "PDBE"
  35.     Me.CPPrice = 263.88
  36. ElseIf StrPD = -1 And StrGA = -1 And StrBO = -1 And StrEL = 0 And StrCH = -1 Then
  37.     Me.Cover = "PDBCHG"
  38.     Me.CPPrice = 275.88
  39. ElseIf StrPD = -1 And StrGA = -1 And StrBO = -1 And StrEL = -1 And StrCH = 0 Then
  40.     Me.Cover = "PDBEG"
  41.     Me.CPPrice = 275.88
  42. ElseIf StrPD = -1 And StrGA = 0 And StrBO = -1 And StrEL = -1 And StrCH = -1 Then
  43.     Me.Cover = "PDBCHE"
  44.     Me.CPPrice = 287.88
  45. ElseIf StrPD = -1 And StrGA = -1 And StrBO = -1 And StrEL = -1 And StrCH = -1 Then
  46.     Me.Cover = "PDBCHEG"
  47.     Me.CPPrice = 299.88
  48.  
  49. ElseIf StrPD = -0 And StrGA = 0 And StrBO = -1 And StrEL = 0 And StrCH = 0 Then
  50.     Me.Cover = "B"
  51.     Me.CPPrice = 119.28
  52. ElseIf StrPD = -0 And StrGA = -1 And StrBO = -1 And StrEL = 0 And StrCH = 0 Then
  53.     Me.Cover = "BG"
  54.     Me.CPPrice = 143.28
  55. ElseIf StrPD = -0 And StrGA = 0 And StrBO = -1 And StrEL = 0 And StrCH = -1 Then
  56.     Me.Cover = "BCH"
  57.     Me.CPPrice = 167.16
  58. ElseIf StrPD = -0 And StrGA = 0 And StrBO = -1 And StrEL = -1 And StrCH = 0 Then
  59.     Me.Cover = "BE"
  60.     Me.CPPrice = 167.28
  61. ElseIf StrPD = -0 And StrGA = -1 And StrBO = -1 And StrEL = 0 And StrCH = -1 Then
  62.     Me.Cover = "BCHG"
  63.     Me.CPPrice = 191.16
  64. ElseIf StrPD = -0 And StrGA = -1 And StrBO = -1 And StrEL = -1 And StrCH = 0 Then
  65.     Me.Cover = "BEG"
  66.     Me.CPPrice = 191.28
  67. ElseIf StrPD = -0 And StrGA = 0 And StrBO = -1 And StrEL = -1 And StrCH = -1 Then
  68.     Me.Cover = "BCHE"
  69.     Me.CPPrice = 215.16
  70. ElseIf StrPD = -0 And StrGA = -1 And StrBO = -1 And StrEL = -1 And StrCH = -1 Then
  71.     Me.Cover = "BCHEG"
  72.     Me.CPPrice = 239.16
  73.  
  74.    Else
  75. End If
Jul 9 '12 #1
1 1011
Killer42
8,435 Recognized Expert Expert
One possibility here might be to set up multi-dimensional arrays holding the values to be placed in various things for each combination.

Let's say you create an array x with 5 dimensions. Load the values you want into it. Then instead of checking the combinations, just use array entry x(Me.PD, Me.GA, Me.BO, Me.EL, Me.CH). People tend not to consider arrays with more than 2 or 3 dimensions, simply because 2 and 3 are easily thought of (and drawn) as a square or cube.

Hm...

Not sure this could be made to work easily, since you're not just using different values depending on the combination of inputs, but placing them in different fields.



Another approach might be something like this:
Expand|Select|Wrap|Line Numbers
  1. Dim s As String
  2. s = Format$(Me.PD + 1,"0") _
  3.   & Format$(Me.GA + 1,"0") _
  4.   & Format$(Me.BO + 1,"0") _
  5.   & Format$(Me.EL + 1,"0") _
  6.   & Format$(Me.CH + 1,"0")
  7.  
  8. Select Case s
  9.   Case "11111"
  10.     Me.Cover = ""
  11.     Me.CPPrice = 0
  12.   Case "11101"
  13.     Me.Cover = ""
  14.   Case "10101"
  15.     Me.Cover = ""
  16.     Me.CPPrice = 0
  17.   Case "01110"
  18.     Me.Cover = ""
  19.     Me.CPPrice = 0
  20.   ...
I stopped here because I'd assumed it was the same five controls being checked each time, but they changed at that point. But hopefully you get the general idea. (I just added 1 to them all at the start because negative numbers made my string look messy).

A third alternative woukld be to look into nested IF statements, along the lines of:
Expand|Select|Wrap|Line Numbers
  1. If Me.PD = 0 Then
  2.   If Me.GA = 0 Then
  3.     If Me.BO = 0 Then
  4.       If Me.EL = 0 Then
  5.         If Me.CH = 0 Then
  6.           Me.Cover = ""
  7.           Me.CPPrice = 0
  8.         End If
  9.       ElseIf Me.EL = -1 Then
  10.         Me.Cover = ""
  11.       End If
  12.     ElseIf Me.BO = -1 Then
  13.       If Me.EL = 0 Then
  14.         If Me.CH = 0 Then
  15.           Me.Cover = "B"
  16.           Me.CPPrice = 119.28
  17.         ElseIf Me.CH = -1 Then
  18.           Me.Cover = "BCH"
  19.           Me.CPPrice = 167.16
  20. ...
  21.  
However, as you can see this rapidly becomes confusing.

In the end, you might find that your original ElseIf code is actually about the easiest to understand.
Jul 10 '12 #2

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

Similar topics

2
1481
by: wenmang | last post by:
//a header file #define API_ABC //a CPP file class API_ABC Myclass; class Myclass{
0
1148
by: Relaxin | last post by:
UPDATE BALANCE set TOTAL = TOTAL + ? WHERE ID = ? The "TOTAL = TOTAL + ?" fails with an error, but didn't fail using 7.2 (Peer Direct Windows port). If I change the statement to this: UPDATE BALANCE set TOTAL = TOTAL + 5 WHERE ID = ? it works. Is this by design or is this a bug.
3
1320
by: ooze | last post by:
I have read the Programing language C 6.10.3.2 The # operator Constraints 1 Each # preprocessing token in the replacement list for a function-like macro shall be followed by a parameter as the next preprocessing token in the replacement list. Semantics 2 If, in the replacement list, a parameter is immediately preceded by
5
8613
by: td0g03 | last post by:
Here is the question....Write a statement that declares a prototype for a function divide that takes four arguments and returns no value. The first two arguments are of type int . The last two arguments arguments are pointers to int that are set by the function to the quotient and remainder of dividing the first argument by the second argument. The function does not return a value. Here is my code void divide(int , int, pInt* quotient,...
1
937
by: hmartinez | last post by:
=Trim("1" & Right(Str(),2) & "0" & Left(Str(),1) & "0" & Mid(Str(),3,1)) don't know why it is returning an error
3
5590
by: sivadhanekula | last post by:
Hi...every one... I have a problem with below statement here the underlined fullname is the item from combo box If I have the same statement without LIMIT then the program is working...if it is with LIMIT I have some problem...is the statement I have given correct?... if not can you plzz kindly give me suggestions of how to write an SQL statement with LIMIT in Visual Basic...
2
1448
by: Jim Rockett | last post by:
The curveGR is a column of numbers The numbers in the statement are values to match against the curve to give the name of the mineral in this case dolomite. Dolomite is to be shown in a column beside the CurveGR with heading Mineral The statement is-- if curveGR=>11.5 and curveGR <17.5 then print "dolomite"
0
8781
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9156
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8997
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7911
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5939
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4453
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4709
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2502
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.