Solved Do Until Loop with MsgBox

pooldead

Registered User.
Local time
Today, 05:01
Joined
Sep 4, 2019
Messages
136
I'm toying with an idea and wanted to see if there is any validity to it. If I had a function I wanted to run multiple times based on a user's MsgBox choice (yes or no), could I use a do until loop instead of hard-coding the function to run a hundred times over?

Basically:
Do Until MsgBox = 1 (1 being No in this ex)
Run function
Loop

It's a simple example, but again I would just like to know if this is possible? I have some databases that accept user input and can have 1 input or 10, and the goal here would be to keep the code as slim and clean as possible.
 
Hi. Not sure I follow; and if you're really talking about using a MsgBox, then I'm not sure what you mean, because a MsgBox is a dialog window, which means no code will run after you close it (by making a selection when you hit the buttons). So, I'm not sure you can do a loop with a single MsgBox.
 
You can accomplish this but not with a message box. You can customize a message box to return a value (https://support.microsoft.com/en-us/office/msgbox-function-e23511c8-4157-4e09-93a6-ba9f1749f4c0), but when you launch the message box all other code stops until that message box is delt with, so your function will run just once for each response from the user.

If you want a function to continously run until user interaction you should build a custom form. The form has 2 buttons--Start and Stop. The code behind it has a global boolean (True/False) variable. When Start is clicked the global is set to True and it enters a While loop that tests that global variable, as long as true it runs whats inside the While. When the user clicks Stop it simply sets that variable to false and the While function will stop running.
 
You can do it, as a yes/no message box can return a value. I might set up an infinite loop with this inside:

Code:
response = MsgBox(msg, button, title)
If response = vbYes Then
  'run function
Else
  'exit loop
End If
 
or exit a loop conditionally

Code:
do until 1=2
    if msgbox("Are you tired of this yet?",vbyesno,"  ")=vbyes then
        exit do
    end if
loop
 
There's some information about message boxes on my website which may be of interest:-

 
Input boxes prompt for input values. Messages boxes display messages. They do not collect input. But most people simply use unbound controls on a form to collect input arguments.
 
Messages boxes display messages. They do not collect input.

Generally I would agree, but a yes/no message box does return a value. In essence it can be considered input: the user's answer to the yes/no question. I use them all the time to confirm a user's desire to take an action, etc.
 
That's a technicality. Confirmation isn't "data" entry in the sense the OP requires.
 
not really clear what you are trying to achieve but this should work

while MsgBox("run function?",vbyesno)=vbyes
Run function
wend
 
I'm toying with an idea and wanted to see if there is any validity to it. If I had a function I wanted to run multiple times based on a user's MsgBox choice (yes or no), could I use a do until loop instead of hard-coding the function to run a hundred times over?
What are you expecting to change? CJ just suggested a loop that just keeps asking you to run a function. The MsgBox will have the focus and will not allow you to do anything except click yes or no. This is an endless loop. It will run over and over again until you say no. You won't be able to determine what the function did unless perhaps the function puts up a form that says "stop already" to tell you to end the loop.
 
That's a technicality. Confirmation isn't "data" entry in the sense the OP requires.

It is exactly what was asked for: "If I had a function I wanted to run multiple times based on a user's MsgBox choice (yes or no)".
 
If only there was someone attached to this thread who knew exactly what the initial poster wanted and could settle this.

pooldead please return, your thread tearing this forum apart and not in an entertaining manner.
 
or exit a loop conditionally

Code:
do until 1=2
    if msgbox("Are you tired of this yet?",vbyesno,"  ")=vbyes then
        exit do
    end if
loop
Sorry for being late to the game, but thank you to everyone who responded. The above solution did exactly what I was looking for.
 
Sorry for being late to the game, but thank you to everyone who responded. The above solution did exactly what I was looking for.
Better late than never. Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom