Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Loop inputbox until ... 2

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hello,

I am quite new to vbs and just trying to learn more about loops. I have been playing around with some simple code that is a reminder. I need some help with adding to this code as I want to ask the user if they want to enter another reminder once they cancel the first one. I have played around with it but cannot get it to do what I want. Here is the code I am working with....

Code:
reminder = InputBox(vbNewLine & strComputerName & ", Enter a reminder message for today!" & vbNewLine & vbNewLine & WeekDayName(WeekDay(Now())) & "  -  " & Date,"Daily Reminder")
frequency = InputBox(vbNewLine & "How often in 'MINUTES' would you like to be reminded?")


do
WScript.Sleep 60000 * frequency
answer=MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     " ,65,"Time:  " & Time)
loop


if answer = 2 then WScript.Quit

I want the user to have the option to continue if the user presses cancel.
Like:

Code:
if answer = 2 then
reply = msgbox"Would you like to create another reminder?", vbYesNo

This is what I have tried but I get errors trying to place in another Do-Loop. Any help is greatly appreciated.

Thanks!
 
Why create a new thread instead of reply here thread329-1691031 ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Completely my mistake... I can't find how to remove a thread.
 
I can't find how to remove a thread
Red Flag it

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
How did you try adding the other loop?

You are on the right track... you will need two loops; an inner loop like you already have, and an outer loop around everything (to allow all the code to be repeated if the user presses "Cancel", and then "Yes")

Also note that your loop has no "While" condition, so you are not able to exit it without exiting the entire script.

This is how I would start building the inner loop. You would have to surround this with the outer loop, and terminate that loop if the user answers "No" to the second question

Code:
bInnerLoop = True
do While bInnerLoop
   WScript.Sleep 60000 * frequency
   answer=MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     ", _ 
      vbOkCancel,"Time:  " & Time)
   if answer = vbCancel Then
      bInnerLoop = False [COLOR=#4E9A06]'User hit cancel, so we are done with the inner loop[/color]

      [COLOR=#4E9A06]'ask user "create another reminder? yes/no?"[/color]

   end if
loop
, that allows to the next question. Also, your Do Loops need a "While" condition, so they can terminate at some point...
 
Hi Guitarzan

Thanks for your time and great help. I do get stuck on these loops and want to learn more about them. Based on what you sent, here is what I finished with. It seems to work, but I am not sure if this is really the correct way to write it or not.

Code:
bOuterLoop = True
do While bOuterLoop

reminder = InputBox(vbNewLine & "Enter a reminder message for today!" & vbNewLine & vbNewLine & WeekDayName(WeekDay(Now())) & "  -  " & Date,"Daily Reminder")
frequency = InputBox(vbNewLine & "How often in 'MINUTES' would you like to be reminded?")


bInnerLoop = True
do While bInnerLoop
   WScript.Sleep 60000 * frequency
   answer=MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     ", _ 
      vbOkCancel,"Time:  " & Time)
   if answer = vbCancel Then
      bInnerLoop = False 'User hit cancel, so we are done with the inner loop

   reply = msgbox("Create another reminder?", vbYesNo)
   if reply = vbYes Then
   bOuterLoop = True
   end if

   if reply = vbNo Then
   bOuterLoop = False
   end if
  end if

 loop
loop
 
The following should suffice:
Code:
bOuterLoop = True
Do While bOuterLoop
    reminder = InputBox(vbNewLine & "Enter a reminder message for today!" & vbNewLine & vbNewLine & WeekdayName(Weekday(Now())) & "  -  " & Date, "Daily Reminder")
    Frequency = InputBox(vbNewLine & "How often in 'MINUTES' would you like to be reminded?")
    Do While bInnerLoop
        WScript.Sleep 60000 * Frequency
        answer = MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     ", _
                        vbOKCancel, "Time:  " & Time)
        If answer = vbCancel Then
            Reply = MsgBox("Create another reminder?", vbYesNo)
            If Reply = vbNo Then
                bOuterLoop = False
            End If
            Exit For    'User hit cancel, so we are done with the inner loop
        End If
    Loop
Loop

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

Was there a FOR loop in your code?
 
OOps, sorry for the typo.
Code:
bOuterLoop = True
Do While bOuterLoop
    reminder = InputBox(vbNewLine & "Enter a reminder message for today!" & vbNewLine & vbNewLine & WeekdayName(Weekday(Now())) & "  -  " & Date, "Daily Reminder")
    Frequency = InputBox(vbNewLine & "How often in 'MINUTES' would you like to be reminded?")
    Do While bInnerLoop
        WScript.Sleep 60000 * Frequency
        answer = MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     ", _
                        vbOKCancel, "Time:  " & Time)
        If answer = vbCancel Then
            Reply = MsgBox("Create another reminder?", vbYesNo)
            If Reply = vbNo Then
                bOuterLoop = False
            End If
            Exit [!]Do[/!]    'User hit cancel, so we are done with the inner loop
        End If
    Loop
Loop

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Hi PHV,

I tried this out... and it stays in a continuous loop.
 
Code:
bOuterLoop = True
Do While bOuterLoop
    reminder = InputBox(vbNewLine & "Enter a reminder message for today!" & vbNewLine & vbNewLine & WeekdayName(Weekday(Now())) & "  -  " & Date, "Daily Reminder")
    Frequency = InputBox(vbNewLine & "How often in 'MINUTES' would you like to be reminded?")
    [highlight #FCE94F]Do [s]While bInnerLoop[/s][/highlight]
        WScript.Sleep 60000 * Frequency
        answer = MsgBox(reminder & "   " & vbNewLine & vbNewLine & "Press Cancel to Stop Reminder     ", _
                        vbOKCancel, "Time:  " & Time)
        If answer = vbCancel Then
            Reply = MsgBox("Create another reminder?", vbYesNo)
            If Reply = vbNo Then
                bOuterLoop = False
            End If
            Exit Do    'User hit cancel, so we are done with the inner loop
        End If
    Loop
Loop
The inner Do loop does not need a while condition, because PHV's code has an "Exit Do" once the user hits cancel.

Earlier I said that the Do loop needed a While condition, but I misspoke; PHV's example showed another way
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top