CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2012
    Posts
    2

    Post How to use Msgbox with Escapekey in VB6

    I have tried to end my program with following procedure:-
    1. When i press Escapekey in keyboard it should display messagebox stating 'Do you want to Quit?" with Yes or No Option
    Remark: its work up to this.

    2. But When i click on Yes button option, It wont work for me. i have used following and it display "compile error: Argument not optional"
    Please correct me...

    Private Sub Form_KeyPress(KeyAscii As Integer)

    If KeyAscii = vbKeyEscape Then
    MsgBox ("Do you want to quit"), vbYesNo, "You are clossing your program"
    If MsgBox = vbYes Then
    End
    Else
    Me.Show
    End If
    End If
    End Sub

    Thank you in advance....

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to use Msgbox with Escapekey in VB6

    Always call a separate Subroutine, and unload the TOP-MOST form, LAST:
    Code:
    Option Explicit
    
    Private Sub cmdExit_Click()
     Dim frm As Form
     For Each frm In Forms
       If frm.Name <> Me.Name Then ' Unload this form LAST
         Unload frm
         Set frm = Nothing
       End If
     Next
     Unload Me
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: How to use Msgbox with Escapekey in VB6

    The error is due to improper use of MsgBox

    Code:
    Private Sub Form_KeyPress(KeyAscii As Integer)
    
       If KeyAscii = vbKeyEscape Then
          Dim Result As Integer
          Result=MsgBox ("Do you want to quit", vbYesNo, "You are clossing your program")
          If Result = vbYes Then
             End
          Else
             Me.Show
          End If
       End If
    End Sub
    That said you should not use End to stop your program but should unload all forms as shown in the post above as well as release any objects that may be active in your program. If you need to use End to stop your program then you have done something wrong.
    Last edited by DataMiser; July 23rd, 2012 at 12:11 AM.
    Always use [code][/code] tags when posting code.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured