472,145 Members | 1,504 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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 976
Killer42
8,435 Expert 8TB
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

Post your reply

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

Similar topics

2 posts views Thread by wenmang | last post: by
3 posts views Thread by ooze | last post: by
reply views Thread by Saiars | last post: by

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.