+ Reply to Thread
Results 1 to 4 of 4

msgBox problem

Hybrid View

  1. #1
    Registered User
    Join Date
    05-13-2008
    Posts
    14

    msgBox problem

    I am trying to have a message box show when a user fails to fill in a required text box in a user form, however the box does not pop up and I am getting no error messages as to an incorrect code. Here is what I'm using:

    Private Sub TextBox2_LostFocus()
        If TextBox2.Value = vbNullString Then
             'TextBox2.Value is empty then show msgbox
             MsgBox "TextBox2 value is required.", vbCritical, "Required Value"
             TextBox2.Activate
           Else
            TextBox2.Value = TextBox2.Text
    
        End If
    End Sub
    Thanks for any feedback!
    Bernadette

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    It would help if you told us what line of code you are getting the error on

    Instead of the following command (which I am assuming is causing your problem)
    TextBox2.Activate
    You are probally wanting
    TextBox2.SetFocus

    What is the purpose of this command
    TextBox2.Value = TextBox2.Text
    TextBox2.Value is already the same as TextBox2.Text
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525
    Quote Originally Posted by bernie1961
    I am trying to have a message box show when a user fails to fill in a required text box in a user form, however the box does not pop up and I am getting no error messages as to an incorrect code. Here is what I'm using:

    Private Sub TextBox2_LostFocus()
        If TextBox2.Value = vbNullString Then
             'TextBox2.Value is empty then show msgbox
             MsgBox "TextBox2 value is required.", vbCritical, "Required Value"
             TextBox2.Activate
           Else
            TextBox2.Value = TextBox2.Text
    
        End If
    End Sub
    Thanks for any feedback!
    Bernadette
    Hi Bernadette,
    The msgbox works if you use the exit
    Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)

  4. #4
    Registered User
    Join Date
    05-13-2008
    Posts
    14
    Thanks Dave and Mudraker....

    Dave - That certainly did fix the problem!! Thanks so much!

    Mudraker - SetFocus did make more sense to use as well as deleting the "TextBox2.Value = TextBox2.Text" line of code. (I actually was not getting an error message when I ran my code, so I had nothing to tell me where my error may be....) Thank you for your help!

    Bernadette

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1