Skip to main content

Dialog box

How can I create a dialog box that prompts me for four dates (Date1, Date2, Date3, and Date4) and validates them to ensure the user doesn't enter incorrect dates? Once the user enters the correct dates, I'll call an external import script. The idea is to automate this process. Any ideas on how to build this dialog box would be greatly appreciated. Thanks.

Option Explicit

Sub main

Call menu()

End Sub

Function menu()
Dim dlg As Ndialogo
Dim button As Integer
button = Dialog(dlg)
End Function
This is as far as I can get; I don't know how to validate the four dates captured in the TextBox of each one.

scotchy33 Mon, 11/24/2025 - 14:33

This should get you started

Begin Dialog NDialog 0,0,322,217,"Enter Four Dates", .DateValidationHandler
Text 20,10,80,13, "Date 1:"
TextBox 110,8,180,14, .txtDate1
Text 20,30,80,13, "Date 2:"
TextBox 110,28,180,14, .txtDate2
Text 20,50,80,13, "Date 3:"
TextBox 110,48,180,14, .txtDate3
Text 20,70,80,13, "Date 4:"
TextBox 110,68,180,14, .txtDate4
Text 20,95,280,40, "Enter dates in a format like: MM/DD/YYYY, YYYY-MM-DD, or any other valid date format."
OKButton 110,165,90,21, .OK
CancelButton 210,165,90,21, .Cancel
End Dialog
Option Explicit

' Global variables to store dialog state and date values
Dim g_userOkayed As Boolean
Dim g_userCancelled As Boolean
Dim g_date1 As String
Dim g_date2 As String
Dim g_date3 As String
Dim g_date4 As String

Sub main
Call menu()
End Sub

Function menu()
Dim dlg As NDialog
Dim button As Integer
Dim continueLoop As Boolean

' Initialize global state
g_userOkayed = False
g_userCancelled = False
g_date1 = ""
g_date2 = ""
g_date3 = ""
g_date4 = ""

' Dialog loop pattern - allows validation without closing
continueLoop = True
Do While continueLoop
' Show dialog
button = Dialog(dlg)

' Check what happened
If g_userCancelled Then
' User clicked Cancel or ESC
MsgBox "Operation cancelled by user.", MB_ICONINFORMATION, "Cancelled"
Exit Function
ElseIf g_userOkayed Then
' User clicked OK and validation passed
continueLoop = False
End If
' If neither flag is set, validation failed - loop continues
Loop

' At this point, all dates are validated and stored in global variables
MsgBox "Dates validated successfully!" & Chr(13) & Chr(10) & _
"Date 1: " & g_date1 & Chr(13) & Chr(10) & _
"Date 2: " & g_date2 & Chr(13) & Chr(10) & _
"Date 3: " & g_date3 & Chr(13) & Chr(10) & _
"Date 4: " & g_date4, MB_ICONINFORMATION, "Success"

' Now call your external import script with the validated dates
Call YourImportScript(g_date1, g_date2, g_date3, g_date4)

End Function

'****************************************************************************************************
' Name: DateValidationHandler
' Description: Dialog handler that validates all four date inputs
' Returns 1 to keep dialog open, 0 to close it
'****************************************************************************************************
Function DateValidationHandler(ControlID As String, Action As Integer, SuppValue As Integer) As Integer
Dim bKeepDialogOpen As Boolean
Dim errorMsg As String
Dim tempDate1 As String
Dim tempDate2 As String
Dim tempDate3 As String
Dim tempDate4 As String

bKeepDialogOpen = True ' Default: keep dialog open

Select Case Action
Case 1 ' Control initialization
' Reset flags on dialog open
g_userOkayed = False
g_userCancelled = False
bKeepDialogOpen = True

Case 2 ' Button pressed
Select Case ControlID
Case "OK"
' Capture current values from dialog
tempDate1 = Trim(DlgText("txtDate1"))
tempDate2 = Trim(DlgText("txtDate2"))
tempDate3 = Trim(DlgText("txtDate3"))
tempDate4 = Trim(DlgText("txtDate4"))

' Validate all four dates
errorMsg = ""

If tempDate1 = "" Then
errorMsg = errorMsg & "Date 1 is required." & Chr(13) & Chr(10)
ElseIf Not IsDate(tempDate1) Then
errorMsg = errorMsg & "Date 1 is not a valid date." & Chr(13) & Chr(10)
End If

If tempDate2 = "" Then
errorMsg = errorMsg & "Date 2 is required." & Chr(13) & Chr(10)
ElseIf Not IsDate(tempDate2) Then
errorMsg = errorMsg & "Date 2 is not a valid date." & Chr(13) & Chr(10)
End If

If tempDate3 = "" Then
errorMsg = errorMsg & "Date 3 is required." & Chr(13) & Chr(10)
ElseIf Not IsDate(tempDate3) Then
errorMsg = errorMsg & "Date 3 is not a valid date." & Chr(13) & Chr(10)
End If

If tempDate4 = "" Then
errorMsg = errorMsg & "Date 4 is required." & Chr(13) & Chr(10)
ElseIf Not IsDate(tempDate4) Then
errorMsg = errorMsg & "Date 4 is not a valid date." & Chr(13) & Chr(10)
End If

' If any validation errors exist, show them and keep dialog open
If errorMsg <> "" Then
MsgBox errorMsg, MB_ICONEXCLAMATION, "Validation Error"
g_userOkayed = False
bKeepDialogOpen = False ' Close so loop can restart
Else
' All dates are valid - store them and signal success
g_date1 = tempDate1
g_date2 = tempDate2
g_date3 = tempDate3
g_date4 = tempDate4
g_userOkayed = True
bKeepDialogOpen = False ' Close dialog
End If

Case "Cancel"
g_userCancelled = True
g_userOkayed = False
bKeepDialogOpen = False ' Close dialog

End Select

Case 3 ' Text entry or ESC pressed
If ControlID = "" Then
' ESC key pressed
g_userCancelled = True
bKeepDialogOpen = False
Else
' Text changed in one of the TextBoxes
bKeepDialogOpen = True
End If
End Select

' CRITICAL: Return numeric value (1 = keep open, 0 = close)
If bKeepDialogOpen Then
DateValidationHandler = 1
Else
DateValidationHandler = 0
End If
End Function

'****************************************************************************************************
' Name: YourImportScript
' Description: Placeholder for your external import script
' Replace this with your actual import logic
'****************************************************************************************************
Sub YourImportScript(date1 As String, date2 As String, date3 As String, date4 As String)
' Your import script code here
' You can convert the strings to Date type if needed:
' Dim actualDate1 As Date
' actualDate1 = CDate(date1)

MsgBox "Import script would be called here with validated dates.", MB_ICONINFORMATION, "Import"
End Sub