Search code examples
vbams-accessmsgbox

Apply font changes to Enhanced Message Box


Renaud Bompuis pulled together some great code. I want to put in font changes (e.g. bold) on a field result within the Dialog.RichText format.

I'm trying to get the result of CaptureEmail to be bold. The box just makes CaptureEmail bold. The value doesn't show up. If I remove the <strong> from the code, the email shows up, but not bold.

Private Sub Other_Email_BeforeUpdate(Cancel As Integer)
  ' https://www.devhut.net/2016/08/18/validate-e-mail-addresses/

Dim ClickResult As VbMsgBoxResultEx
Dim CaptureEmail
Dim html
CaptureEmail = Me.Other_Email

If CaptureEmail = 0 Then
    Exit Sub
ElseIf CaptureEmail > 0 Then
    If ValidateEmail(Me.Other_Email) = False Then

        ClickResult = Dialog.RichBox("The inputted e-mail " & _
          <strong>[CaptureEmail]</strong> & _
          " does not appear to be a valid Email_Address. " & "<p/>" & _
          "Would you like to recheck it before adding it?", _
          vbYesNo + vbCritical, "Invalid Entry", , , 0, False, False, False)
        If (ClickResult = vbYes) Then
            Cancel = True
        ElseIf (ClickResult = vbNo) Then
            Exit Sub
        End If
    ElseIf ValidateEmail(Me.Other_Email) = True Then
        Exit Sub
    End If
End If
End Sub

Solution

  • The main issue with your code is the concatenation of the first argument supplied to the RichBox method:

    "The inputted e-mail " & <strong>[CaptureEmail]</strong> & " does not appear to be a valid Email_Address. " & "<p/>" & _
    "Would you like to recheck it before adding it?"
    

    Since <strong> & </strong> are literal strings, they should be included as part of the content of the strings that are surrounded by double-quotes, e.g.:

    "The inputted e-mail <strong>" & CaptureEmail & "</strong> does not appear to be a valid Email_Address. "
    

    You also have some malformed HTML here:

    "<p/>"
    

    Presumably, this should be:

    "<p>The inputted e-mail <strong>" & CaptureEmail & "</strong> does not appear to be a valid Email_Address.</p>" & _
    "<p>Would you like to recheck it before adding it?</p>"
    

    You also seem to be mixing data types -

    You initially assign the value of the form control Other_Email to your variable CaptureEmail:

    CaptureEmail = Me.Other_Email
    

    And you then treat CaptureEmail as an integer:

    If CaptureEmail = 0 Then
        Exit Sub
    ElseIf CaptureEmail > 0 Then
    

    However, based on the content of the message box, it would imply that CaptureEmail actually contains a string:

    The inputted e-mail " & CaptureEmail & " does not appear to be a valid Email_Address.


    A few other observations about your code:

    If CaptureEmail = 0 Then
        Exit Sub
    ElseIf CaptureEmail > 0 Then
    

    Given that you only wish to proceed when CaptureEmail is greater than zero, only test is required:

    If CaptureEmail > 0 Then
        ...
    End If
    

    Similarly, later in the code, you have the following:

    If ValidateEmail(Me.Other_Email) = False Then
        ...
    ElseIf ValidateEmail(Me.Other_Email) = True Then
        Exit Sub
    End If
    

    Since ValidateEmail returns a boolean value with only two possible values, it is not necessary to test both since if the return is not False, then it must be True and vice-versa.

    As such, the code may become:

    If Not ValidateEmail(Me.Other_Email) Then
        ...
    End If
    

    The same logic could be applied here:

    If (ClickResult = vbYes) Then
        Cancel = True
    ElseIf (ClickResult = vbNo) Then
        Exit Sub
    End If
    

    Since, for any result other than vbYes, you aren't performing any action.