Form Code for Validate Contacts

This code is intended to be on the form/tab from where the validation of the SMTP aliases will be performed.


' ============================================================
' Copyright (c) 2025-2026 Peter Bryant (Corylus Business Systems)
'                    and  Andrew Richards (The IT Service Ltd)
' Part of VBAuth - https://vbauth.com
' Licensed under the MIT Licence - see LICENCE.txt
' This copyright notice must not be removed or altered.
' ============================================================

' ============================================================
' Form code examples for the Validate Contacts feature.
' These procedures belong in your Access form's code-behind.
'
' They cannot be imported directly as a module — copy each
' procedure into your form module manually.
'
' FORM CONTROLS REQUIRED:
'   txtContactEmail   Text box — user enters address(es) here
'   cmdCheckContacts  Command button — triggers the check
'
' DEPENDENCIES:
'   modValidateContacts.bas       (ValidateContactAddresses)
'   modVBAuthGlobalConstants.bas
'   JSONConverter.bas             (Tim Hall VBA-JSON)
'
' PREREQUISITE:
'   lclVBAuthAdmin must contain a correctly configured record
'   for this feature. See README.md for required values.
' ============================================================

Option Compare Database
Option Explicit

' ============================================================
' cmdCheckContacts_Click
' Validates a single email address (or comma- or
' semicolon-separated list) entered in txtContactEmail against
' the authenticated user's Exchange contacts via Graph API.
' Format-validates addresses before calling Graph.
' Displays a MsgBox with found/not-found results.
' ============================================================
Private Sub cmdCheckContacts_Click()

Dim strAddresses    As String
Dim strMessage      As String
Dim strFormatErrors As String

    strAddresses = Trim(Nz(Me.txtContactEmail, ""))

    If strAddresses = "" Then
        MsgBox "Please enter at least one email address to check.", _
               vbExclamation, "Check Addresses in Contacts"
        Exit Sub
    End If

    ' Format-validate first
    strFormatErrors = ""
    ValidateEmailList strAddresses, "Addresses", strFormatErrors

    If strFormatErrors <> "" Then
        MsgBox "Please correct invalid addresses before checking:" & _
               vbCrLf & vbCrLf & strFormatErrors, _
               vbExclamation, "Check Addresses in Contacts"
        Exit Sub
    End If

    If ValidateContactAddresses(strAddresses, "", "", strMessage) Then
        MsgBox strMessage, vbInformation, "Check Addresses in Contacts"
    Else
        MsgBox "Could not complete contact check:" & vbCrLf & vbCrLf & _
               strMessage, vbExclamation, "Check Addresses in Contacts"
    End If

End Sub

' ============================================================
' IsValidEmail
' Basic format validation — checks for a single @ with a
' local part, domain, and TLD of at least 2 characters.
' Does not verify the address exists.
' ============================================================
Private Function IsValidEmail(strAddress As String) As Boolean

Dim intAtPos    As Integer
Dim strLocal    As String
Dim strDomain   As String
Dim intDotPos   As Integer

    IsValidEmail = False
    strAddress = Trim(strAddress)
    If Len(strAddress) < 6 Then Exit Function

    intAtPos = InStr(strAddress, "@")
    If intAtPos < 2 Then Exit Function
    If InStr(intAtPos + 1, strAddress, "@") > 0 Then Exit Function

    strLocal = Left(strAddress, intAtPos - 1)
    strDomain = Mid(strAddress, intAtPos + 1)

    If Len(strLocal) = 0 Then Exit Function

    intDotPos = InStrRev(strDomain, ".")
    If intDotPos < 2 Then Exit Function
    If intDotPos >= Len(strDomain) Then Exit Function
    If Len(strDomain) - intDotPos < 2 Then Exit Function

    IsValidEmail = True

End Function

' ============================================================
' ValidateEmailList
' Validates a comma- or semicolon-separated list of email
' addresses using IsValidEmail. Appends any invalid addresses
' to strErrors prefixed with the field name.
' Returns False if any addresses are invalid.
' ============================================================
Private Function ValidateEmailList(strList As String, strFieldName As String, _
                                   ByRef strErrors As String) As Boolean

Dim arrAddresses()  As String
Dim strAddress      As String
Dim strBadList      As String
Dim i               As Integer

    ValidateEmailList = True
    If Trim(strList) = "" Then Exit Function

    arrAddresses = Split(Replace(strList, ";", ","), ",")
    For i = 0 To UBound(arrAddresses)
        strAddress = Trim(arrAddresses(i))
        If strAddress <> "" Then
            If Not IsValidEmail(strAddress) Then
                strBadList = strBadList & strAddress & ", "
            End If
        End If
    Next i

    If strBadList <> "" Then
        strBadList = Left(strBadList, Len(strBadList) - 2)
        strErrors = strErrors & "- " & strFieldName & ": invalid address(es): " & _
                    strBadList & vbCrLf
        ValidateEmailList = False
    End If

End Function
Code language: PHP (php)
Updated on March 14, 2026