View Full Version : Code help needed on message box please


dxp
04-30-2007, 07:53 AM
Hi All, Is it possible to have just one instance of this message box fire when one or all or any number of the flags are triggered. At the moment if 2 flags are set then 2 messgage boxes appear,if 3 then 3 messages etc. My code at the moment is below to trigger the message box, hope I make my self clear;can it be tidied up?





private Sub Form_Current()
If Me.Flag1 = "Patient Alert. HIGH RISK. Med Hist" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment"
End If
If Me.Flag2 = "Patient Alert. ALLERGY. Med Cond" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment"
End If
If Me.Flag3 = "Patient Alert. Cat Score. LOW. Caution See Notes" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment"
End If
If Me.Flag4 = "Patient Alert. PATHOLOGY. Int/Ext Exam" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment"
End If
End Sub

boblarson
04-30-2007, 08:39 AM
Private Sub Form_Current()
Dim blnAlert As Boolean
Dim strAlert As String

strAlert = "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment"


If Me.Flag1 = "Patient Alert. HIGH RISK. Med Hist" Then
blnAlert = True
End If
If Me.Flag2 = "Patient Alert. ALLERGY. Med Cond" Then
blnAlert = True
End If
If Me.Flag3 = "Patient Alert. Cat Score. LOW. Caution See Notes" Then
blnAlert = True
End If
If Me.Flag4 = "Patient Alert. PATHOLOGY. Int/Ext Exam" Then
blnAlert = True
End If

If blnAlert Then
MsgBox strAlert, vbCritical, "Alerts Detected"
End If
End Sub

CraigDolphin
04-30-2007, 08:49 AM
This should work...more than one way to skin this cat though...


private Sub Form_Current()

If Me.Flag1 = "Patient Alert. HIGH RISK. Med Hist" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment",vbinformation
Else
If Me.Flag2 = "Patient Alert. ALLERGY. Med Cond" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment",vbinformation
Else
If Me.Flag3 = "Patient Alert. Cat Score. LOW. Caution See Notes" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment",vbinformation
Else
If Me.Flag4 = "Patient Alert. PATHOLOGY. Int/Ext Exam" Then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment",vbinformation
End If
End If
End If
End If

End Sub


It's ugly doing it this way. If you used a check box bound to a yes/no field for each flag (instead of a text field) you could do something a little more elegant.

For example:

dim show_message as integer
show_message = ABS(Me.Flag1 + Me.Flag2 + Me.Flag3 + Me.Flag4)
if show_message > 0 then
MsgBox "**WARNING PATIENT ALERTS DETECTED**Read all patient alerts before treatment",vbinformation
End if

Since the value of a yes/no field is -1 for true, and 0 for false, and your message does not change depending on which flag is triggered, you only need to know if the absolute value of the sum of the flags is greater than one and thereby show the message. Using a yes/no field for each flag will also save you several bytes per record compared to storing the text strings.
___________________________________
Ok...Bob beat me to the punch (he posted before I got done with my reply)...again! :)

dxp
04-30-2007, 10:37 AM
Thanks guys, I shall give your examples a go now. Hopefuly I shall have only one message show no matter how many flags are triggered. I will let you know how it goes.

boblarson
04-30-2007, 10:47 AM
Thanks guys, I shall give your examples a go now. Hopefuly I shall have only one message show no matter how many flags are triggered. I will let you know how it goes.

If you use my code, only one message box is guaranteed since it's called only once.

dxp
04-30-2007, 11:04 AM
Guys Thanks a million, I used Bobs code, works treat. Many thanks for your help.:D

dxp
04-30-2007, 11:25 AM
Bob, heres a screen shot of your code in action! is it possible to have to lower case text on a seperate line instead of split like it is. Just for the sake of good design?

dxp
04-30-2007, 11:29 AM
sorry,screen shot here now

dxp
04-30-2007, 12:34 PM
got it now after a little trial and error, & vbCrLf &

boblarson
04-30-2007, 01:37 PM
Good going! Glad to see you got it sorted.