Bypass Textbox Validation

  • Thread starter Myrna Rodriguez
  • Start date
M

Myrna Rodriguez

Hi VB Bugs...

I created a userform with textboxes to facilitate data entry on Excel
spreadsheet. I validated the textboxes to display a message box if
empty.
Userform automatically appears when file is open.
However, if I decide not to input data on userform & wish to
CANCEL(unload userform), a message box still appears.
How can I bypass validation for textboxes and proceed to CANCEL form??

Thanks and will validate your sugguestions!!
Myrna

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
J

Jim Rech

I assume you're running your validation routines from Sub
UserForm_QueryClose. If so, check the value of the CloseMode argument that
is passed to it. If it's 0 then the "x" was used to close the form. If
Unload caused it to close the value is 1.

--
Jim Rech
Excel MVP
| Hi VB Bugs...
|
| I created a userform with textboxes to facilitate data entry on Excel
| spreadsheet. I validated the textboxes to display a message box if
| empty.
| Userform automatically appears when file is open.
| However, if I decide not to input data on userform & wish to
| CANCEL(unload userform), a message box still appears.
| How can I bypass validation for textboxes and proceed to CANCEL form??
|
| Thanks and will validate your sugguestions!!
| Myrna
|
| *** Sent via Devdex http://www.devdex.com ***
| Don't just participate in USENET...get rewarded for it!
 
M

Myrna Rodriguez

Hi...

This is the code I am currently using to validate textbox.

Private Sub txtfast_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'display msgbox if textbox fast is empty
If txtfast.Text = "" Then
MsgBox "Enter FAST File"
Cancel = True
End If
End Sub

**The message box appears successfully when empty, but when I close
userform by click on "X" the message box pops up. I would like to Cancel
userform even if textbox is empty.

Thanks,
Myrna





*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
C

Charles

Myrna Rodriguez,

Try using a close command button on your userform.


Private Sub CommandButton1_Click()
Unload UserForm1
End Sub

HTH

Charle
 
J

Jim Rech

This is the code I am currently using to validate textbox

You might defer all your validating until the user tries to close the form.

Or you could consider this approach:

Dim ExitMode As Boolean

Private Sub UserForm_Activate()
ExitMode = False
End Sub

Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ExitMode = True
End Sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If ExitMode = False Then
If TextBox1.Text = "" Then MsgBox "Warning"
End If
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top