Option Explicit
Public Enum verType
vtNumber = 0
vtIntegerNumber = 1
vtBoolean = 2
vtDateTime = 3
vtAlphabeticOnly = 4
End Enum
Public Sub testInputBoxVerified()
Dim vInput As Variant
vInput = InputBoxVerified("Enter a DateValue value", "Enter date", vtDateTime, "Fix Input", "Invalid Input")
MsgBox vInput, vbInformation
End Sub
Public Function InputBoxVerified(prompt As String, Optional InputTitle As String = "", Optional verifyType As verType = vtNumber, Optional verifyPrompt As String = "", Optional verifyTitle As String = "") As Variant
Do
InputBoxVerified = InputBox(prompt, InputTitle)
If Trim(InputBoxVerified & " ") = "" Then Exit Do
Loop Until verifyInput(InputBoxVerified, verifyType, verifyPrompt, verifyTitle)
End Function
Public Function verifyInput(vInput As Variant, verifyType As verType, verifyPrompt As String, verifyTitle As String) As Boolean
Dim strPrompt As String
Select Case verifyType
Case vtNumber
strPrompt = "Number Required." & vbCrLf
verifyInput = myIsNumeric(vInput)
Case vtIntegerNumber
strPrompt = "Integer Number Required." & vbCrLf
verifyInput = myIsIntegerNumeric(vInput)
Case vtBoolean
strPrompt = "Boolean Value Required." & vbCrLf
verifyInput = myIsBoolean(vInput)
Case vtDateTime
strPrompt = "Date Required." & vbCrLf
verifyInput = IsDate(vInput)
If verifyInput Then vInput = CDate(vInput)
Case vtAlphabeticOnly
strPrompt = "Alphabetic Only Value Required." & vbCrLf
verifyInput = myIsAlphabetic(vInput)
Case Else
verifyInput = True
End Select
strPrompt = strPrompt & "Invalid input value: " & vInput & vbCrLf & vbCrLf & verifyPrompt
If Not verifyInput Then
MsgBox strPrompt, vbInformation, verifyTitle
End If
End Function
Public Function myIsNumeric(vInput As Variant) As Boolean
If Not Trim(vInput & " ") = "" Then
vInput = Trim(vInput)
myIsNumeric = (Not vInput Like "*[!0,1,2,3,4,5,6,7,8,9,.,-]*") And IsNumeric(vInput)
End If
End Function
Public Function myIsBoolean(vInput As Variant) As Boolean
If Not Trim(vInput & " ") = "" Then
vInput = Trim(vInput)
myIsBoolean = (vInput = "Yes" Or vInput = "No" Or vInput = "True" Or vInput = "False" Or vInput = "-1" Or vInput = "0")
End If
End Function
Public Function myIsIntegerNumeric(vInput As Variant) As Boolean
If Not Trim(vInput & " ") = "" Then
vInput = Trim(vInput)
myIsIntegerNumeric = (Not vInput Like "*[!0,1,2,3,4,5,6,7,8,9,-]*") And IsNumeric(vInput)
End If
End Function
Public Function myIsAlphabetic(vInput As Variant) As Boolean
If Not Trim(vInput & " ") = "" Then
vInput = Trim(vInput)
myIsAlphabetic = Not vInput Like "*[!A-Z]*"
End If
End Function