Results 1 to 6 of 6

Thread: MessageBox opens multiple times
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Jan 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default MessageBox opens multiple times

    Hello everyone!

    I have a MessageBox in my application that asks whether the user wants to add a new product to the order or not. If I press 'No', it closes. When I press 'Yes', it does what it's supposed to do, lets me add another product, and so on. The problem is that once I press no, the amount of MessageBoxes I get is the number of times I pressed 'Yes' before that. How could I solve this?

    The code is:
    'Show MessageBox asking user if they want to add more products to the order
    Private Sub Addmoreproducts()
    'Declare the msg (message) and ans (answer) variables
    Dim msg, ans


    Do
    Unload UFAddStock
    Unload UFUpdateStock
    msg = "Add more products to the order?"
    ans = MsgBox(msg, vbYesNo)


    'If answer is Yes, then close this UserForm and open the UserForm for adding more products
    If ans = vbYes Then
    Unload UFAddStock
    UFUpdateStock.Show

    End If

    'If answer is No, then close all open UserForms and return to the sheet
    If ans = vbNo Then Exit Do
    Unload UFAddStock
    Unload UFUpdateStock


    'Loop for MessageBox until answer is No
    Loop Until ans = vbNo


    End SubThank you in advance!

  2. #2
    Board Regular
    Join Date
    Sep 2016
    Posts
    2,582
    Post Thanks / Like
    Mentioned
    36 Post(s)
    Tagged
    1 Thread(s)

    Default Re: MessageBox opens multiple times

    If ans = vbYes Then
    Unload UFAddStock
    UFUpdateStock.Show

    'If answer is No, then close all open UserForms and return to the sheet
    ElseIf ans = vbNo Then
    Unload UFAddStock
    Unload UFUpdateStock
    Exit Do
    End If

    'Loop for MessageBox until answer is No
    Loop
    Last edited by footoo; Jan 28th, 2018 at 10:04 PM.

  3. #3
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,603
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: MessageBox opens multiple times

    This should work. There were many duplicate, unneeded UnLoadings in the OP code.

    How is the sub AddMoreProducts triggered? If it is by a button on UFAddStock or UFUpdateStock, there might be recursion issues. If it is called by the user from the Macro dialog, this should work.

    Code:
    Private Sub AddMoreProducts()
        Dim msg As String, ans As Long
        Dim msg, ans
    
        msg = "Add more products to the order?"
        Do
            Unload UFAddStock
            Unload UFUpdateStock
        
            ans = MsgBox(msg, vbYesNo)
    
    
           'If answer is Yes, then close this UserForm and open the UserForm for adding more products
            If ans = vbYes Then
                UFUpdateStock.Show
            End If
        Loop Until ans = vbNo
    End Sub

  4. #4
    New Member
    Join Date
    Jan 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: MessageBox opens multiple times

    Hi, thank you for the code. Unfortunately, it didn't change anything. The sub is called from another sub.

  5. #5
    New Member
    Join Date
    Jan 2018
    Posts
    7
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default Re: MessageBox opens multiple times

    Hi, thank you, but it didn't work

  6. #6
    MrExcel MVP mikerickson's Avatar
    Join Date
    Jan 2007
    Location
    Davis CA
    Posts
    22,603
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    15 Thread(s)

    Default Re: MessageBox opens multiple times

    Do frmUpdateStock and frmAddStock invoke each other?
    Does the other sub have any looping that might be where the problem lies?

    What happens when you step through the code?

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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