Form Code for List My Bookings

' ============================================================
' 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 Get Booking Appointments 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:
'   txtBusinessID         Text box — Bookings calendar SMTP address
'   txtAppointmentList    Text box — displays returned appointments
'   lblAppointmentCount   Label   — displays appointment count
'   cmdGetAppointments    Command button — triggers retrieval
'
' DEPENDENCIES:
'   modGetBookingAppointments.bas  (GetBookingAppointments)
'   modVBAuthGlobalConstants.bas
'   JSONConverter.bas              (Tim Hall VBA-JSON)
'
' PREREQUISITE:
'   lclVBAuthAdmin must contain a correctly configured record
'   for this feature. See README_GetBookingAppointments.md
'   for required values.
'
' MICROSOFT GRAPH API LIMITATIONS:
'   The Bookings appointments endpoint does not support server-
'   side filtering. All filtering (e.g. by customer name) is
'   performed client-side inside GetBookingAppointments after
'   the full API response is received. This is a Microsoft
'   Graph API constraint, not a VBAuth limitation.
'   See README_GetBookingAppointments.md for full details.
' ============================================================

Option Compare Database
Option Explicit

' ============================================================
' cmdGetAppointments_Click
' Validates the Bookings calendar SMTP address entered in
' txtBusinessID, then calls GetBookingAppointments to retrieve
' appointments via Graph API.
' Displays the appointment list and count in the output controls.
'
' Note: the business ID is the SMTP address of the Bookings
' calendar (e.g. mybusiness@contoso.com), not the user's
' own email address.
' ============================================================
Private Sub cmdGetAppointments_Click()

Dim strResult       As String
Dim lngCount        As Long
Dim strBusinessID   As String
Dim strErrors       As String

    strBusinessID = Trim(Nz(Me.txtBusinessID, ""))
    strErrors = ""

    If strBusinessID = "" Then
        strErrors = strErrors & "- Bookings calendar address cannot be empty" & vbCrLf
    Else
        If Not IsValidEmail(strBusinessID) Then
            strErrors = strErrors & "- Bookings calendar address is not a valid email address" & vbCrLf
        End If
    End If

    If strErrors <> "" Then
        MsgBox "Please correct the following:" & vbCrLf & vbCrLf & strErrors, _
               vbExclamation, "Get Appointments"
        Me.txtBusinessID.SetFocus
        Exit Sub
    End If

    Me.txtAppointmentList = "Retrieving appointments..."
    Me.lblAppointmentCount.Caption = "Appointment count: -"
    Me.Repaint

    If GetBookingAppointments(strResult, lngCount, strBusinessID) Then
        Me.txtAppointmentList = strResult
        Me.lblAppointmentCount.Caption = "Appointment count: " & lngCount
    Else
        Me.txtAppointmentList = ""
        Me.lblAppointmentCount.Caption = "Appointment count: -"
        MsgBox "Failed to retrieve appointments." & vbCrLf & vbCrLf & strResult, _
               vbCritical, "Get Appointments"
    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 FunctionCode language: PHP (php)
Updated on April 4, 2026