Text entry validator


This module provides an example of the Multiplier program TextBox_Change event number validator code logic, plus some additional features.


In this module:


0. The interface


The animation in figure 1 is produced by the code in this module.


xlf-txt-validator
Fig 1: xlfAnimation - txtBox number validator with [X] close button feature

1. the VBA code


1.1 txtEntry change event


txtEntry is written with series of VBA OR functions. The OR function returns TRUE if any of the conditions are TRUE.



Code 1: Sub txtEntry_Change filters the InString variable. TextBox_Change event
Private Sub txtEntry_Change()
Dim InString As String
Dim Allowable As Boolean

    InString = txtEntry.Value
    ' check for numeric, blank (backspace clear), or minus
    If IsNumeric(InString) _
        Or InString = "" _
        Or InString = "-" Then
        Allowable = True
    Else
        Allowable = False
    End If

    If Allowable = True Then
        Number1 = Val(InString)
        Image1.Visible = True
    Else
        Image1.Visible = False
    End If

End Sub
'

1.2 Other UserForm code



Code 2: maintenance code for UserForm
' ================================
' 1. Module declarations
Option Explicit
' module scope declaration
Dim Number1 As Double
Dim ExitDemo As Boolean
'
' ================================
' 2. Initialize event
Private Sub UserForm_Initialize()
    Me.BackColor = RGB(214, 232, 243)
    Label1.BackColor = RGB(214, 232, 243)
    Label2.BackColor = RGB(214, 232, 243)
    Label3.BackColor = RGB(214, 232, 243)

    cmdClose.BackColor = RGB(55, 96, 146)
    cmdClose.ForeColor = RGB(252, 252, 252)

    txtEntry.BackColor = RGB(255, 255, 255)

    With cmdDemo
        .Caption = "Demo"
        .BackColor = RGB(55, 96, 146)
        .ForeColor = RGB(252, 252, 252)
    End With
End Sub


' Command buttons
' ================================
' 3.1. Button Click event
Private Sub cmdDemo_Click()
Dim txt() As Variant
Dim i As Long
txt = Array("a", "", "-", "+", "123", "$", "End of demo", "End of demo", "")
    txtEntry.BackColor = RGB(204, 253, 204)

    For i = LBound(txt) To UBound(txt)
        If ExitDemo Then Exit For                       ' check for cmdClose event
        Me.txtEntry.Value = txt(i)                      ' write value from txt variant array by index number
        Application.Wait (Now + TimeValue("0:00:01"))
        cmdDemo.Caption = UBound(txt) - i - 1           ' cmdDemo is in count down mode
        DoEvents                                        ' write txtEntry.Value immediately
    Next i

    cmdDemo.Caption = "Demo"                            ' cmdDem is Reset to default caption
    txtEntry.BackColor = RGB(255, 255, 255)

End Sub


' ================================
' 3.2. Button Click event
Private Sub cmdClose_Click()
    ExitDemo = True                                     ' ExitDemo needed by cmdDemo If...Then construct
    Unload Me
End Sub


' ================================
' 4. Form [X] cancel button
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
    MsgBox "Sorry you must use Close Button", , ""      ' force use of cmdClose
    Cancel = True
End If
End Sub
'
'