Results 1 to 12 of 12
  1. #1
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Feedback from dialog boxes

    Ok, I bet that this is a really easy question but, hey, I couldn't find the answer in the help system (or by trial and error) ... so here it is ...

    I have a simple dialog box with an 'ok' and a 'cancel' button. If the user clicks the 'cancel' button, I want to skip the rest of the code. Its easy with a input box (if inputbox() = "" then skip) but I cannot see how to do it with a custom dialog box.

    I did create a way - but it an't pretty.

    Any body with an answer for this one.

    Cheers,

    Tim
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    Hollywood (sorta), California, USA
    Posts
    2,759
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    You need to define what you mean by dialog box.

    Do you mean you used the msgbox statement? Or do you mean you created a userform with buttons on it?
    Kevin <IMG SRC=http://www.wopr.com/w3tuserpics/Kevin_sig.gif alt="Keep the change, ya filthy animal...">
    <img src=/w3timages/blackline.gif width=33% height=2><img src=/w3timages/redline.gif width=33% height=2><img src=/w3timages/blackline.gif width=33% height=2>

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Feedback from dialog boxes

    If MsgBox("Destroy hard drive?", vbQuestion + vbOKCancel) = vbCancel Then _
    Exit Sub

  4. #4
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    OK, maybe a bit more info will help. I'm fine with inputbox and msgbox ...

    x = inputbox()
    if x = "" then goto TheEnd

    if msgbox("blah blah blah", vbyesno) = vbno then goto TheEnd

    ... but it is USERFORMs that cause me the worry.

    Sorry about the confusion.
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Feedback from dialog boxes

    Assuming a command button named "cmdCancel":

    Private Sub cmdCancel_Click()
    Unload Me
    End Sub

  6. #6
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    Ok, now I am going to be really detailed. I have a user form that contains a list box and two command buttons (one cmdOK and one cmdCancel).

    This form gets displayed right at the beginning of a macro to give the user a choice of paths (the list box contains a list of several paths). The user can select a path, click Ok (the path form gets hidden) and then the macro saves a file to the selected path. If the user clicks Cancel, the form still gets hidden but how do I know that cancel has been clicked.

    What I have done is create another label (lblResult) that is invisible. If the user clicks Ok, the .enable property is set to TRUE, if the user clicks Cancel, the .enable property is set to FALSE.

    In this way I can do the following:

    ...

    frmPath.lblResult.enable = TRUE
    frmPath.show

    if not frmPath.lblResult.enable then goto TheEnd

    ...

    My original question (with maybe not enough details) was is there a better way?

    Cheers,

    Tim
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  7. #7
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    >the user clicks Cancel, the
    .enable property is set to FALSE.

    I like this.

    I had two "cancel" problems this morning; the second was how to communicate back to the calling procedure that "cancel" was clicked. I took the cheap way out and used a public Boolean, but I much prefer to use a value that is local to the form.

    Thanks

  8. #8
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: Feedback from dialog boxes

    Tim,

    Maybe it's just my poor caffeine-depleted brain cells<g>, but I'm still not following what you're trying to do (i.e. what the problem is):

    The usual practice on a userform would be to have two commandbuttons: cmdOK and cmdCancel. Clicking on cmdOK sets in motion whatever processing the userform is designed to make available (followed by the userform unloading). The code for this is housed in the cmdClick_OK event procedure, and whatever calls are necessary to procedures that do the processing, are placed in the cmdClick_OK procedure.

    Usually, the Cancel button is intended just to let the user exit (unload) the userform - so that is all the code that's usually found in cmdCancel_Click.

    If the above is the case with your userform, then isn't the cmdCancel_Click procedure sufficient?

    Another less common scenario is that you want the userform to make available a series of procedures, and the userform needs to stay open to enable the user to keep making choices after each procedure has completed. In this case you'd use additional buttons to enable the user to run or cancel these procedures. But in any case, you can put code into any of the buttons' Click event procedure, to handle the event of the user clicking that button.

    Hope the above is any use, sorry if this is repeating what you already know and I'm missing the point!

    Gary

  9. #9
    5 Star Lounger Ruff_Hi's Avatar
    Join Date
    Feb 2001
    Location
    New York, New York, USA
    Posts
    768
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    Thanks all for your replies.

    I really wanted a form that would only return the selected path - not process any code. I could have put all my processing code in the cmdOK button but that would make the form hard to use for other purposes.

    So I kept all my processing code in a sub and wanted to know if the user had selected to cancel so I could skip to the end of the sub. Sounds like the solution I used is ok (it works).

    Again, thx for everyone's replies.

    Cheers Tim
    (Location Australia, then UK, but now USA. Heart, outlook, attitude, etc always Australian)
    Quote: "All Happiness is the release of internal pressure"

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Feedback from dialog boxes

    I think storing the value with the form is great. Like Chris, I usually changed the value of a Public variable (sometimes a byte, since it could have 3 or 4 values). Another way to do what you're doing is to change the UserForm.Tag value (a string), and then you don't need to create a hidden label.

  11. #11
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Feedback from dialog boxes

    Actually, if you use automation and the WithEvents keyword to open the form, you can respond to the OnClick event of the button directly, without using label, tags or anything else. This only works from a class module, of course, but the modules behind a form, document or worksheet is a class module, so it should work most of the time. I use this all the time in Access, but it works in VB or in other Office apps just as well.
    Charlotte

  12. #12
    Star Lounger
    Join Date
    Jan 2001
    Location
    L.A., California
    Posts
    77
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Feedback from dialog boxes

    If I understand your question, you want to put up the form and have the user choose a path, which will be used not in the form, but in the code which called the form. If the user clicks Cancel on the form, the routine which called the form will terminate...right?
    Here's one way to do what you want:

    Have a global or module level variable to use in your subroutine:

    Dim PathToUse As String

    Have a function whose only purpose is to call the form, like this:

    Function GetPath() as String
    Load ufrmGetPath
    ufrmGetPath.Show
    GetPath = ufrmGetPath.MyPath
    Unload ufrmGetPath
    Set ufrmGetPath = Nothing
    End Function

    Note that the first line inside the function is optional, as the Show method will implicitly Load it also. You would need it, however, if you wanted to set any of the form's public variables before showing it.

    In the form, have a form-level public variable (define it at the top, before any subroutines):

    Public MyPath As String

    In the Ok button click event, set MyPath to the one the user chose in the list.

    In the Cancel button click, set MyPath to "".

    In both events, don't unload the form, as the calling function will do that. Simply Hide the form instead:

    Me.Hide

    In the main routine of your program, use the following:

    PathToUse = GetPath

    If PathToUse = "" Then
    ...your exit code here...
    End If

    ...your other code here....

    This should work ok. Any questions, feel free to e-mail.

Posting Permissions

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