Textbox data entry validation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Adam Honek

    #1

    Textbox data entry validation

    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


  • JDMils

    #2
    Re: Textbox data entry validation

    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.Ena bleEvents Or ClassTriggerEna bled = 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 = "tbDateClos ed" 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("Dat a").Range("dDat eClosed").Offse t(frmDataEntry. sbFU.Value +
    1).Value = "" Then
    ' TxtBx.Locked = True
    ' TxtBx.BackStyle = fmBackStyleTran sparent
    ' Else
    ' TxtBx.Locked = False
    ' TxtBx.BackStyle = fmBackStyleOpaq ue
    ' 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(CtrlTa g:=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.Ena bleEvents = False
    ' Format the textbox according to its designated format.
    Select Case varTag
    Case "Curr"
    TxtBx = Format(TxtBx.Va lue, "Currency")
    Case "Dt"
    TxtBx = Format(TxtBx.Va lue, "mm/dd/yyyy")
    Case "Ph"
    TxtBx = Format(TxtBx.Va lue, "(###) ###-####")
    Case "Num"
    TxtBx = Format(TxtBx.Va lue, "#")
    End Select
    ' Re-enable events.
    Application.Ena bleEvents = 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 SetupClassEvent TxtBox()
    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.

    --
    |
    +-- JDMils
    |


    "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> wrote in message
    news:eoI$RXBZGH A.3848@TK2MSFTN GP05.phx.gbl...[color=blue]
    >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
    >[/color]


    Comment

    • Herfried K. Wagner [MVP]

      #3
      Re: Textbox data entry validation

      "Adam Honek" <AdamHonek@Webm aster2001.frees erve.co.uk> schrieb:[color=blue]
      > 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?[/color]


      \\\
      Private Sub TextBox_Validat ing( _
      ByVal sender As Object, _
      ByVal e As CancelEventArgs _
      ) Handles TextBox1.Valida ting, TextBox2.Valida ting, ...
      Dim SourceControl As TextBox = DirectCast(send er, TextBox)
      If SourceControl.T ext.Length = 0 Then
      Me.ErrorProvide r1.SetError( _
      SourceControl, _
      "Value must be at least one character long." _
      )
      Else
      If Me.ErrorProvide r1.GetError(Sou rceControl).Len gth > 0 Then
      Me.ErrorProvide r1.SetError(Sou rceControl, "")
      End If
      End If
      End Sub
      ///

      --
      M S Herfried K. Wagner
      M V P <URL:http://dotnet.mvps.org/>
      V B <URL:http://classicvb.org/petition/>

      Comment

      Working...