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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word - message box - prompt for text 1

Status
Not open for further replies.

Mollethewizard

IS-IT--Management
Nov 18, 2002
93
SE
I’ve got a problem. In a form I have textboxes that requires the user to insert text. Fix a message box that tells the user that he or she have not entered any text while leaving the textbox by pressing enter or tab is relatively easy to fix in the textbox exit or change event. But how on earth can I get the prompt to return to the textbox if it’s empty?
Any suggestions how to solve the problem?

Greetings from a rainy city called Karlstad in Sweden
 
Mollethewizard,

After your prompt, set Cancel = True to cancel the exit from the box. I just did this in Word but I'm sure Excel is pretty similar.

Code:
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If TextBox1 = "" Then
    MsgBox "empty"
    Cancel = True
End If
End Sub

Depending on what you want you may have to trap form closure as well.

Enjoy,
Tony
 
This returns the focus on the textbox if its empty.

Private Sub CommandButton1_Click()
'my macro
If TextBox1.Text = "" Then
MsgBox "textbox is empty"
TextBox1.SetFocus
'my macro
End If
End Sub
 
Tony understood that I wanted the control to be made while the form was still open and before the user hit the OK button.

Edric’s code works fine if the control should be made while closing the form.

Thanks both.

Mollethewizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top