+ Reply to Thread
Results 1 to 7 of 7

Warning msgbox when closing a userform

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    430

    Warning msgbox when closing a userform

    Hi
    I have a userform for inputing a range of vehicle details. It will not let the user add the details unless all fields are complete.

    If the user complets some of the fields and closes the form it will not save any information.

    They must use a command button to close the form (the red X will not show)

    I am looking for a way for a warning msg box to appear when they select the close form button

    thanks
    Last edited by JamesT1; 10-13-2008 at 01:20 PM.

  2. #2
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    re: Warning msgbox when closing a userform

    This is just a copy what can be found in the HELP. Add you functionality like check wether all fields are filled.
    Private Sub UserForm_Terminate()
    
    Dim Msg, Style, Title, Help, Ctxt, Response, MyString
    Msg = "Do you want to continue ?"    ' Define message.
    Style = vbYesNo + vbCritical + vbDefaultButton2    ' Define buttons.
    Title = "MsgBox Demonstration"    ' Define title.
    
    Ctxt = 1000    ' Define topic
            ' context.
            ' Display message.
    Response = MsgBox(Msg, Style, Title, , Ctxt)
    If Response = vbYes Then    ' User chose Yes.
        MyString = "Yes"    ' Perform some action.
    Else    ' User chose No.
        MyString = "No"    ' Perform some action.
    End If
    
    End Sub
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    430
    Hi rwgrietveld

    Thanks for the reply

    The userform has two buttons

    'Add Vehicle', this can only be used when all fields are complete.

    'Close Form' to be used for closing the form when the vehicle details have been entered.

    What I would like is, if the 'Add Vehicle' button has not been used then a message box appears warning the vehicle details have not been saved, when the 'Close Form' button is selected
    Last edited by JamesT1; 10-12-2008 at 03:29 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello JamesT1,

    Welcome to the Forum!

    If you have a workbook with the user form added, you should post it. It will make things clearer for everyone and get you a faster response to your question.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Contributor
    Join Date
    08-04-2008
    Location
    West Calder, Scotland
    MS-Off Ver
    365
    Posts
    430
    Hi Leith Ross

    The userform is attached the to 'Add New Vehicle' button
    Attached Files Attached Files
    Last edited by JamesT1; 10-12-2008 at 04:21 PM.

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello JamesT1,

    Here is the macro I added to the form. It is called in the QueryBeforeClose event. If it is false, the message is displayed and the focus set back to vehicle registration text box. It has already been installed in the attached workbook.
    Function ValidateAllEntries() As Boolean
    
      Dim Ctrl As MSForms.Control
      Dim Status As Boolean
      
        Status = True
        
        For Each Ctrl In Me.Controls
          Select Case TypeName(Ctrl)
            Case Is = "TextBox", "ComboBox"
              If Ctrl.Value = "" Then Status = False
          End Select
        Next Ctrl
        
        ValidateAllEntries = Status
      
    End Function
    Sincerely,
    Leith Ross
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Closing a userform
    By wilro85 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2008, 03:47 PM
  2. Keep Modal UserForm Alive after closing 2nd userform using 2007?
    By ShredDude in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-24-2008, 03:12 PM
  3. Required Fields
    By jmswebdesign in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-27-2007, 07:19 PM
  4. Userform closing error
    By darren101 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-18-2007, 11:07 AM
  5. Adding MsgBox to Userform
    By buckchow in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-31-2007, 12:45 AM

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