Exit Program - Do Loop question

User avatar
CrashN8
Site Admin
Site Admin
Posts: 597
Joined: Thu Feb 09, 2017 4:07 pm

Re: Upgrade to 3.9 sp2

Post by CrashN8 » Thu Feb 27, 2020 10:38 am

You're 2nd End If statement cannot be inside the Do Loop... Basically this creates a situation where the End If would be executed in the Do Loop until the "Until" criteria is met.

Here is a better way...

Line 005 :

Code: Select all

answer = MsgBox("IS MANUAL ALIGNMENT REQUIRED?", 4)
IF answer = 6 THEN

Line 006 :

Code: Select all

End If
Do
 answer = MsgBox("Is program Rev. up to date?  Rev.   Sht. 1  Rev. A / ADCN 1", 4)
    IF answer = 7 THEN
    MsgBox("Program Need up date")
    Exit Do
  End if
Line 011 :
Moved your End If Statement to beginning of Line 006

Line 032 :

Code: Select all

ProgExit = 1
Loop Until ProgExit = 1

ken60
Power User
Power User
Posts: 98
Joined: Fri Mar 21, 2014 12:32 pm

Re: Exit Program - Do Loop question

Post by ken60 » Thu Feb 27, 2020 1:35 pm

Thank you CrashN8
I was think about moved "end if " statement before Do loop but to do that the program has to go thru 2nd query every time which is not my intended.

Thank you Guru,
Same as CrashN8 suggestion, you modification still requiring the program execute 2 queries on every run....I know it is only 1 more key stroke but the idea works in 3.7 sp2

User avatar
CMM Guru
Moderator
Moderator
Posts: 105
Joined: Thu Feb 24, 2011 7:49 pm

Re: Exit Program - Do Loop question

Post by CMM Guru » Thu Feb 27, 2020 2:39 pm

So...

The purpose of the loop is to allow the program to run, say 20 times (without closing the program) to inspect 20 parts, but only prompt for the alignment once?

In other words, you are not putting part 1 on the table, clicking "run" in CMM-Manager, then placing part 2 on the table and clicking "run" in CMM-Manager...? You are running the program and letting it loop, changing parts between each loop?

We can make it do that too. :-)

ken60
Power User
Power User
Posts: 98
Joined: Fri Mar 21, 2014 12:32 pm

Re: Exit Program - Do Loop question

Post by ken60 » Thu Feb 27, 2020 4:23 pm

The purpose of the loop is to allow the program to run, say 20 times (without closing the program) to inspect 20 parts, but only prompt for the alignment once?

In other words, you are putting part 1 on the table, clicking "run" in CMM-Manager, then placing part 2 on the table and clicking "run" in CMM-Manager...? You are running the program and letting it loop, changing parts between each loop??
Yes, that is what I want the program do
We can make it do that too. :-)
I know you can because I did it in older version (3.5, 3.7) :) :) :)
Thank you

User avatar
CMM Guru
Moderator
Moderator
Posts: 105
Joined: Thu Feb 24, 2011 7:49 pm

Re: Exit Program - Do Loop question

Post by CMM Guru » Thu Feb 27, 2020 4:55 pm

One last clarification.

WHICH question do want asked each time? I would have thought the "Is the program up to date" would be the once only?

ken60
Power User
Power User
Posts: 98
Joined: Fri Mar 21, 2014 12:32 pm

Re: Exit Program - Do Loop question

Post by ken60 » Thu Feb 27, 2020 6:40 pm

I would have thought the "Is the program up to date" would be the once only?
You are absolute right, only need to verify "Rev." once for every batch we run

User avatar
CMM Guru
Moderator
Moderator
Posts: 105
Joined: Thu Feb 24, 2011 7:49 pm

Re: Exit Program - Do Loop question

Post by CMM Guru » Fri Feb 28, 2020 1:11 pm

This is a way of doing it. Not the only way.

Code: Select all

DoAlignment = 0
UpToDate = MsgBox("Is the program up-to-date?", 4)
IF UpToDate = 6 THEN
    DO
        IF DoAlignment = 0 THEN
            DoAlignment = MsgBox("Do Alignment?", 4)
            IF DoAlignment = 6 THEN
************************************
Put your alignment code here
************************************

Code: Select all

            END IF	
        END IF
************************************
Put yourprogram code here
************************************

Code: Select all

    LOOP
ELSE
    MsgBox("Update the program")
END IF
2020-03-02: Updated to add the ELSE for the "Is Program up-to-date" prompt

If UpToDate does not equal 6, then nothing in the "IF" executes and the program ends (you can put an ELSEIF at the end with a message if you wish)

During the first run, DoAlignment is 0, so you are prompted to run, or not run the alignment. If you say yes (6) then it runs that code and when the loop comes back, none of the alignment query or code runs (it's not 0 now, it's 6! (or 7 if you said NO)).

NOTE: Running this code "as is" as the only code will lock you in the loop. The stop button is not active if this is the only "operation".

ken60
Power User
Power User
Posts: 98
Joined: Fri Mar 21, 2014 12:32 pm

Re: Exit Program - Do Loop question

Post by ken60 » Fri Feb 28, 2020 1:41 pm

Thank you Guru

ken60
Power User
Power User
Posts: 98
Joined: Fri Mar 21, 2014 12:32 pm

Re: Exit Program - Do Loop question

Post by ken60 » Mon Mar 02, 2020 8:31 pm

Capture.JPG
Hello Guru, the code in the program still looping continuously when run with both "Yes" or "No" for "Do Alignment?"
It seems there is no other way I can have the 2 queries work as before since "Goto" statement is not support any more.
I modify the program to use IF, THEN, ELSE instead.

Line 004

Code: Select all

answer = MsgBox("Is program Rev. up to date?  Rev.   Sheet 2  Rev. NEW", 4)
IF answer = 6 THEN
Line 005

Code: Select all

answer = MsgBox("IS MANUAL ALIGNMENT REQUIRED?", 4)
IF answer = 6 THEN
Line 022

Code: Select all

End If
Line 033

Code: Select all

Else
MsgBox("Program Need up date")
Line 034

Code: Select all

End If
The program works as I expect except I have to answer for 2 prompts instead of 1 :) :) :)
You do not have the required permissions to view the files attached to this post.

User avatar
CMM Guru
Moderator
Moderator
Posts: 105
Joined: Thu Feb 24, 2011 7:49 pm

Re: Exit Program - Do Loop question

Post by CMM Guru » Tue Mar 03, 2020 8:45 am

We are curious why you do not use "Batch Run"? It does virtually everything you are trying to do in VB, but is built in and requires no extra code (except you may still want your "Is program up-to-date?" prompt with an ELSE/ENDIF at the end)

Also - what was the "STOP" criteria originally? The way it was written, it appeared that your program was in a continuous loop until you clicked the CMM-Manager STOP button?

Post Reply