Consulting

Results 1 to 7 of 7

Thread: Solved: Validating entry on a userform (Excel '97)

  1. #1
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location

    Solved: Validating entry on a userform (Excel '97)

    Hi,

    I'm currently using the following to validate entry on a userfrom :
    [VBA]If Trim(Me.txtTwo.Value) = "" Then
    Me.txtTwo.SetFocus
    MsgBox "Please enter the Customers Name!"
    Exit Sub
    End If[/VBA]

    Instead of using this code, is there anyway that I can validate it based on the amount of characters entered? For example, the field must contain at least 3 characters?

    Thanks,
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  2. #2
    VBAX Tutor nst1107's Avatar
    Joined
    Nov 2008
    Location
    Monticello
    Posts
    245
    Location
    Try[VBA]
    If Len(Trim(Me.txtTwo.Value)) < 3 Then
    Me.txtTwo.SetFocus
    MsgBox "Please enter the Customers Name!"
    Exit Sub
    End If
    [/VBA]

  3. #3
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Thank you, that works a treat.
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can also "check as you go"
    [VBA]Private Sub txtTwo_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Trim(txtTwo.Value)) < 3 Then
    Cancel = True
    MsgBox "Please enter the Customers Name!"
    txtTwo.Text = ""
    End If
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by mdmackillop
    You can also "check as you go"
    [vba]Private Sub txtTwo_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
    If Len(Trim(txtTwo.Value)) < 3 Then
    Cancel = True
    MsgBox "Please enter the Customers Name!"
    txtTwo.Text = ""
    End If
    End Sub
    [/vba]
    Interesting idea, I'll bear this one in mind for use later. I prefer to let people enter everything and then do the check at the very end as if a popup appears mid-way through the data entry it could cuase other problems if they don't notice and just keep typing
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    No problem. Of course you can't keep typing with a message box on the screen!
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  7. #7
    VBAX Tutor phendrena's Avatar
    Joined
    Oct 2008
    Location
    Huddersfield, UK
    Posts
    285
    Location
    Quote Originally Posted by mdmackillop
    No problem. Of course you can't keep typing with a message box on the screen!
    Somewhere in the dark and nasty regions where nobody goes, stands an ancient castle.
    Deep within this dank and uninviting place lives Berk, overworked servant of The Thing Upstairs.
    But thats nothing compared to the horrors that lurk beneath The Trap Door.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •