Solved Do Until Loop with MsgBox (1 Viewer)

pooldead

Registered User.
Local time
Today, 13:17
Joined
Sep 4, 2019
Messages
118
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:17
Joined
Oct 29, 2018
Messages
16,519
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.
 

plog

Banishment Pending
Local time
Today, 15:17
Joined
May 11, 2011
Messages
10,610
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:17
Joined
Aug 30, 2003
Messages
35,370
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
 

Isaac

Lifelong Learner
Local time
Today, 13:17
Joined
Mar 14, 2017
Messages
5,733
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
 

Uncle Gizmo

Nifty Access Guy
Staff member
Local time
Today, 21:17
Joined
Jul 9, 2003
Messages
13,416
There's some information about message boxes on my website which may be of interest:-

 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:17
Joined
Feb 19, 2002
Messages
33,168
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:17
Joined
Aug 30, 2003
Messages
35,370
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.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:17
Joined
Feb 19, 2002
Messages
33,168
That's a technicality. Confirmation isn't "data" entry in the sense the OP requires.
 

CJ_London

Super Moderator
Staff member
Local time
Today, 21:17
Joined
Feb 19, 2013
Messages
13,336
not really clear what you are trying to achieve but this should work

while MsgBox("run function?",vbyesno)=vbyes
Run function
wend
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 16:17
Joined
Feb 19, 2002
Messages
33,168
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.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 13:17
Joined
Aug 30, 2003
Messages
35,370
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)".
 

plog

Banishment Pending
Local time
Today, 15:17
Joined
May 11, 2011
Messages
10,610
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.
 

pooldead

Registered User.
Local time
Today, 13:17
Joined
Sep 4, 2019
Messages
118
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.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 13:17
Joined
Oct 29, 2018
Messages
16,519
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

Top Bottom