3

I'm building a little program to help out with some data imports and for our techs when out on sites.

The bit I'm struggling with is the bp check, see code below:

Private Sub bphg_afterupdate()

'blood pressure values
'below 100/60 - low
'120/70 - normal
'140/90 - high, gp review
'180/100 - high, cut off for fitness for driving
'200/100 - high, cut off for driving/spiro
'230/120 - urgent review required


    If bpmm <= 100 Or bphg <= 60 Then
        bpcomment.Value = "LOW! - Seek Advice"
    ElseIf bpmm < 140 Or bphg < 90 Then
        bpcomment.Value = "Normal BP"
    ElseIf bpmm < 180 Or bphg < 100 Then
        bpcomment.Value = "High! - GP Review"
    ElseIf bpmm < 200 Then
        bpcomment.Value = "High! - Temp restriction to driving MPE/FLT"
    ElseIf bpmm < 230 Or bphg < 120 Then
        bpcomment.Value = "High! - To high for Spiro & Temp Driving Resitricion MPE/FLT"
    Else
        bpcomment.Value = "URGENT! - Review required"
    End If

End Sub

What it's doing is finding the first value that fits in either the values specified and then stopping. It should be continuing to check other criteria.

So basically with blood pressure, out of the 2 figures your doctor gives you, either can determine if your bp is ok or not. So when we enter a bp into the form say 200/80 (you would probably never get this but I'm being through), it would find that the first figure is high and the second is normal. My script however is finding the second figure being normal first without checking the first figure, so it just displays "normal" when in fact it's "high".

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
  • 2
    You would have cleaner code if you used a `Select Case` instead. – braX Feb 14 '18 at 15:43
  • what are bpmm and bphg for the test? Ideally, you would be passing those values in as parameters to your method. The code looks fine at first glance. If the logic is stopping, it's due to the values of these global vars. – gdbj Feb 14 '18 at 15:43
  • bpmm and bphg are text boxes the user completes – James Fenwick Feb 14 '18 at 15:44
  • Code works fine for me. What about the code you're not showing us? IMO `If...ElseIf...` is fine – CallumDA Feb 14 '18 at 15:45
  • that was the whole sub other than the form. – James Fenwick Feb 14 '18 at 15:51
  • the form? so where is `bpcomment` declared and where are you getting the values of `bpmm` and `bphg` from? – CallumDA Feb 14 '18 at 15:53
  • ok so, bpcomment is a text box that will show the outcome if you like. bpmm and bphg are the blood pressure values the tech enters and after tabbing off bphg the bpcomment updates. i hope that make sense. – James Fenwick Feb 14 '18 at 15:56
  • when the code runs, the if statement would go with the first right answer it found and then display the result and finish, without checking the rest of the options. – James Fenwick Feb 14 '18 at 16:01
  • so basically with blood pressure, out of the 2 figures your doctor gives you, either can determine if your bp is ok or not. so when we enter a bp into the form say 200/80(you would prob never get this but im being through) it would find that the first figure is high and the second is normal.my script however is finding the second figure being normal first without checking the rest of the option for the first figure, so it just displays "normal" when in fact its high – James Fenwick Feb 14 '18 at 16:04
  • Use AND instead of OR. With AND both criteria must be met. With OR only one needs to be true for the IF to evaluate as true. – Cindy Meister Feb 14 '18 at 16:13
  • the "and" instead of "or" seems to have fixed it.Thank you. – James Fenwick Feb 14 '18 at 16:19
  • thank you all for your comments and help. i come here a lot looking for advise and you all seem very helpful to everybody. thank you all again. – James Fenwick Feb 14 '18 at 16:20

2 Answers2

0

Select Case would be a better way to deal with the blood pressure issues:

Option Explicit

Public Sub TestMe()

    Dim bpmm        As Long
    Dim bphg        As Long

    bpmm = 100   'assign these two somehow. E.g.-> bpmm = ActiveSheet.Range("A1")
    bphg = 100

    Select Case True
        Case bpmm <= 100 Or bphg <= 60
            Debug.Print "LOW! - Seek Advice"
        Case bpmm < 140 Or bphg < 90
            Debug.Print "Normal BP"
        Case bpmm < 180 Or bphg < 100
            Debug.Print "High!"
        Case Else
            Debug.Print "URGENT! - Review required"
    End Select

End Sub

Instead of Debug.Print, you may put your business logic there. Just make sure that you order the conditions correctly - if the first condition is evaluated to TRUE, the check does not go further.

There is a tiny performance advantage of Select Case - Which way is faster? If elseif or select case

Edit: If your logic is, that all the criteria should be checked separately and independently, then take a look at this:

Public Sub TestMe()

    Dim bpmm        As Long
    Dim bphg        As Long

    bpmm = 100   'assign these two somehow. E.g.-> bpmm = ActiveSheet.Range("A1")
    bphg = 100

    If bpmm <= 100 Or bphg <= 60 Then
        Debug.Print "LOW! - Seek Advice"
    End If
    If bpmm < 140 Or bphg < 90 Then
        Debug.Print "Normal BP"
    End If
    If bpmm < 180 Or bphg < 100 Then
        Debug.Print "High!"
    End If

End Sub
Vityata
  • 42,633
  • 8
  • 55
  • 100
  • @JamesFenwick - just make sure that you order the conditions correctly - if the first condition is TRUE, the check does not go further. – Vityata Feb 14 '18 at 15:57
  • so i have tried this out and added the rest in and its still giving me the same issue. test values are bpmm = 240 and bphg = 80. its finds the 80 displays the answer and then ends. – James Fenwick Feb 14 '18 at 16:11
0

From your description, it looks like you want to return the result of the highest one of the two criteria. In this case, you will need to reverse the order of the checks:

If bpmm >= 230 Or bphg >= 120 Then
    bpcomment.Value = "URGENT! - Review required"
ElseIf bpmm >= 200 Then
    bpcomment.Value = "High! - To high for Spiro & Temp Driving Resitricion MPE/FLT"
ElseIf bpmm >= 180 Or bphg >= 100 Then
    bpcomment.Value = "High! - Temp restriction to driving MPE/FLT"
ElseIf bpmm >= 140 Or bphg >= 90 Then
    bpcomment.Value = "High! - GP Review"
ElseIf bpmm >= 120 Or bphg >= 70 Then
    bpcomment.Value = "Normal BP"
Else
    bpcomment.Value = "LOW! - Seek Advice"
End If

However, it's not clear from the instructions (that you have at the top of your function) what to do in the case it is between 100/60 and 120/70. The code above considers it LOW. If you want to consider it NORMAL instead, then change the last ElseIf from:

ElseIf bpmm >= 120 Or bphg >= 70 Then

to:

ElseIf bpmm >= 100 Or bphg >= 60 Then
Racil Hilan
  • 24,690
  • 13
  • 50
  • 55