MessageBox opens multiple times

loststudent

New Member
Joined
Jan 25, 2018
Messages
7
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:
Code:
'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 Sub
Code:

Thank you in advance!
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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:
Upvote 0
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
 
Upvote 0
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?
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top