• Feedback from dialog boxes

    • This topic has 11 replies, 7 voices, and was last updated 24 years ago.
    Author
    Topic
    #355453

    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

    Viewing 5 reply threads
    Author
    Replies
    • #524309

      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?

    • #524336

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

    • #524372

      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.

      • #524387

        Assuming a command button named “cmdCancel”:

        Private Sub cmdCancel_Click()
        Unload Me
        End Sub

    • #524410

      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

      • #524463

        >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

      • #524475

        Tim,

        Maybe it’s just my poor caffeine-depleted brain cells, 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

    • #524478

      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

      • #524484

        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.

        • #524493

          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.

    • #524609

      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.

    Viewing 5 reply threads
    Reply To: Feedback from dialog boxes

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: