Textbox data entry validation

  • Thread starter Thread starter Adam Honek
  • Start date Start date
A

Adam Honek

I have a form.

It has serveral text boxes for user data entry.

I could of course write code to check if each is empty before proceeding to
save this data to a file.

Is there any global way to control if any of these text boxes is empty or
are we really forced to do it on a one by one basis doing If....End is so
many times?

Thanks,
Adam
 
This is from an XL VBA project I once did:

Class:

Public WithEvents TxtBx As MSForms.TextBox





Private Sub TxtBx_Change()
' -----------------------------------------------------------------------------
' This is the Class Module that is assigned to each textbox on the
' userform. It will trigger when each textbox is changed and as long
' as certain logic is True.
' -----------------------------------------------------------------------------
If Not Application.EnableEvents Or ClassTriggerEnabled = False Then Exit Sub

' We need to lock the Close Date textbox if there is no value present
' to stop the user from creating a new one. A Close Date can only be
' entered via a Follow-Up.
' If TxtBx.Name = "tbDateClosed" And ClntType = "Edit" Then
' ' If the textbox is blank and the data on the "Data" worksheet is
not,
' ' then the user has blanked the textbox himself, presumably to
enter
' ' a new Close Date, thus we will not lock the textbox.
' If TxtBx.Value = "" And
Worksheets("Data").Range("dDateClosed").Offset(frmDataEntry.sbFU.Value +
1).Value = "" Then
' TxtBx.Locked = True
' TxtBx.BackStyle = fmBackStyleTransparent
' Else
' TxtBx.Locked = False
' TxtBx.BackStyle = fmBackStyleOpaque
' End If
' End If

' If Mode = 1 Then Debug.Print "The control '" & TxtBx.Name & "' is being
changed."
' Extract the "format" property from the Tag property
' of the textbox control.
varTag = CheckTag(CtrlTag:=TxtBx.Tag, Fld:="Fmt")
' If Mode = 1 Then Debug.Print " Type => " & varTag
' Stop the routine from re-triggering when it changes
' a textbox's value.
Application.EnableEvents = False
' Format the textbox according to its designated format.
Select Case varTag
Case "Curr"
TxtBx = Format(TxtBx.Value, "Currency")
Case "Dt"
TxtBx = Format(TxtBx.Value, "mm/dd/yyyy")
Case "Ph"
TxtBx = Format(TxtBx.Value, "(###) ###-####")
Case "Num"
TxtBx = Format(TxtBx.Value, "#")
End Select
' Re-enable events.
Application.EnableEvents = True
End Sub


Then in your main code:

Main:

Dim Coll As Collection
Dim Tbx As CTxtBx

.....
.....

'
' -----------------------------------------------------------------------------
' This routine "groups" all textboxes into one collection
' so that one custom Class Module procedure can be used
' to format each textbox according to its format.
' -----------------------------------------------------------------------------
'
Sub SetupClassEventTxtBox()
Dim Ctrl As MSForms.Control

If ClntType = "ViewOnly" Then
cmbOK.Enabled = False
End If
Set Coll = New Collection
For Each Ctrl In Me.Controls
If TypeOf Ctrl Is MSForms.TextBox Then
Set Tbx = New CTxtBx
Set Tbx.TxtBx = Ctrl
Coll.Add Tbx
End If
Next Ctrl
End Sub

This at least should get you going. As you can see, the class checks the tag
of the textbox to determine it's data format and the Select....Case then
formats the data accordingly. Hope this helps.
 
Adam Honek said:
It has serveral text boxes for user data entry.

I could of course write code to check if each is empty before proceeding
to save this data to a file.

Is there any global way to control if any of these text boxes is empty or
are we really forced to do it on a one by one basis doing If....End is so
many times?


\\\
Private Sub TextBox_Validating( _
ByVal sender As Object, _
ByVal e As CancelEventArgs _
) Handles TextBox1.Validating, TextBox2.Validating, ...
Dim SourceControl As TextBox = DirectCast(sender, TextBox)
If SourceControl.Text.Length = 0 Then
Me.ErrorProvider1.SetError( _
SourceControl, _
"Value must be at least one character long." _
)
Else
If Me.ErrorProvider1.GetError(SourceControl).Length > 0 Then
Me.ErrorProvider1.SetError(SourceControl, "")
End If
End If
End Sub
///
 
Back
Top