Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Criteria for TextBox 1

Status
Not open for further replies.

Mollethewizard

IS-IT--Management
Nov 18, 2002
93
SE
I’ve got a user form with a TextBox that I would the user to either leave empty or state a value according to the given mask.

How can I increase the code below so both criteria’s will be accepted?


Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Not TextBox1.Text Like "######-####" Then
MsgBox ("Wrongly stated!")
Cancel = True
End If
End Sub

Christer
 
Will this work, or am I missing something?

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1.Text <> "" Then
    If Not TextBox1.Text Like "######-####" Then
      MsgBox ("Wrongly stated!")
      Cancel = True
    End If
End If
End Sub
 
gh61

Sorry I did not mention that it should apply to a masked edit box with the given mask.

It works perfectly well with an ordinary text box.

Do you have any suggestions for code to a masked edit box?


Christer
 
Not sure if I understand - do you want the masked edit box to display nothing at all (ie no mask clues) if there isn't an otherwise valid string? For example, from the help:

"To clear the Text property when you have a mask defined, you first need to set the Mask property to an empty string, and then the Text property to an empty string:"

MaskedEdit1.Mask = ""
MaskedEdit1.Text = ""

 
gh61

OK I’ll try to clarify.

What I want is that the user puts something in the masked edit box and that something is checked for validation or the masked edit box remains empty when the user tabs along in the user form.

Christer
 
If you're doing your validation in the Exit event code as in your first post, why not use an ordinary text box? What is the masked edit doing for you?
 
Gh61

The masked edit box is intended for Swedish social security numbers. The masked edit box with the right mask set the user sees in what format the entry should be. Furthermore the user doesn’t have to put in a (-) sign when he enters the numbers. And he could for speed use only the numerical keyboard. For e.g. mask ######-#### the user enters 123456 the (-) already is there and he continues with 7890. The exit control checks that he entered all 10 digits. But I also would like him to be able to pass the box without entering anything.

Christer
 
Christer,
Apologies, I misunderstood how you want the masked edit to behave if nothing is entered.
Does this do what you want?

Code:
Private Sub Ed1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    If Ed1.Text = "______-____" Then
        'nothing was entered, carry on
    ElseIf Not Ed1.Text Like "######-####" Then
        MsgBox "Invalid number."
        Cancel = True
    End If
End Sub

Graeme
 
Graeme,

As I’ve said before – why on earth didn’t I think of that solution?

It works like a clock, the atomic kind, and you really deserve the Star for this is indeed very helpful for me.

Thanks

Christer
 
You're very welcome - glad I got there eventually!! And thanks for the star.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top