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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i + 1 loop with conditions

Status
Not open for further replies.

remeng

Technical User
Jul 27, 2006
520
US
Hi All;

I am sure that this is an easy one, but I forgot how to finish this code off.

The goal is for a user to enter a starting number (i) between 1 to 120. If the number is within the range of 1 to 120, it runs the IF statement. Once the IF statement is run, I'd like to perform a Next i operation. The code would end once i is greater than 120. The problem is where to put the For and Next statements.

Here is my code:

Code:
Sub multi_panel_2()

Dim i As Single

i = InputBox("What panel number would you like to start with?", "Starting Panel Number")


    If i >= 1 And i <= 120 Then
    
         confirm = MsgBox("Are you ready to run file " & "panel" & i & ".tap" & " ?", vbOKCancel)

            If confirm = 1 Then
        
                MsgBox ("run " & i & ".tap") ' placeholder for file call
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
              

            Else: End If 'place holder for  code "M30" End If
        
        

    Else: MsgBox ("You entered Panel " & i & ".  " & "Panel number is outside the range of 1 to 120.")
    
        End If


End Sub
 
Just a guess here since your requirements are not very clear to me...

Code:
Sub multi_panel_2()

Dim i As Single[blue]
Dim x As Integer[/blue]

i = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If i >= 1 And i <= 120 Then[blue]
    For x = i To 120[/blue]
        confirm = MsgBox("Are you ready to run file " & "panel" & i & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then        
            MsgBox ("run " & i & ".tap") ' placeholder for file call
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
            Else: End If 'place holder for  code "M30" End If        [blue]
    Next x[/blue]
Else
    MsgBox ("You entered Panel " & i & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub

BTW - what if your user enters "XYZ" as the answer to your InputBox [ponder]


---- Andy

There is a great need for a sarcasm font.
 
I think that the variable i needs to change, therefore...
Code:
Sub multi_panel_2()

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    For i = x To 120
        confirm = MsgBox("Are you ready to run file " & "panel" & i & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then        
            MsgBox ("run " & i & ".tap") ' placeholder for file call
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
            Else: End If 'place holder for  code "M30" End If        
    Next x
Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
Hi Skip,

As always, thank you for the assistance. When I run your code, I get an Invalid Next Control variable reference. I understand where you are going with it, but don't know how to fix the error.

Thanks,

Mike
 
Check the counter name, it should be the same as after Next.

Wouldn't be easier to create a simple userform with input textbox, dynamically filled multiselect listbox (can have rather ugly built-in checkboxes and depending on the developer either not selected or preselected all items), two buttons: first, 'update list' with value check and filling list, and second, 'go'? You could avoid up to 120 replies to "Are you ready to run file ...".

combo
 
In Skip's example:

Code:
...
Next x   [green]'<- change x to i[/green]
...

or in my example:

Code:
...
Dim i As Single    [green]'<- change Single to Integer[/green]
Dim x As Integer   
...



---- Andy

There is a great need for a sarcasm font.
 
Hi combo,

The vb is going to be used in Mach3 as a macro for a CNC router. Mach3 doesn't support forms. I'm not 100% sure that this will work either but if it works in office,it'll be a good start.

Mike
 
You'll want to take the message box related code out of the loop.

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
I'm not sure if this Cypress basic manual refers to the language you use, but if so:
- MsgBox type and buttons are coded in single [Type] parameter (pp. 100-102),
- vbOKCancel constant does not exist,
- you can build dialogs, there is no visual designer, but [tt]Begin Dialog ... End Dialog[/tt] structure (pp. 60-61).

combo
 
Since this is Microsoft VBA for Applications forum, I would assume Mike is using something like Excel VBA
If so, his MsgBox could be written:

Code:
Select Case MsgBox("Are you ready to run file panel" & i & ".tap ?", [blue]vbOKCancel[/blue] Or vbQuestion, "Ready?")
    Case vbOK
            
    Case vbCancel

End Select

but - since this is a "Are you ready" question, I would expect Yes / No answers:

Code:
Select Case MsgBox("Are you ready to run file panel" & i & ".tap ?", [blue]vbYesNo[/blue] Or vbQuestion, "Ready?")
    Case [blue]vbYes[/blue]
        
    Case [blue]vbNo[/blue]

End Select


---- Andy

There is a great need for a sarcasm font.
 
Andy, if you look at the manual I linked, you can see that it is 99% VB(A). Mach3 site states thet the language is the same as Cypress. It is not VBA however in this post: "The vb is going to be used in Mach3 as a macro for a CNC router. Mach3 doesn't support forms." Some structures are different and I suspect that the full code was not tested in real environment.
Much simpler if the code could run from a file created in VBA supporting application and use automation.

combo
 
Ooops! My fault. I missed the Mach3 part... [blush]


---- Andy

There is a great need for a sarcasm font.
 
Hi Folks,

Thanks for the assistance. Mach3 does support message boxes and all the other properties in the MS Office code. I did check.


mach_rsibyx.jpg



My current code is working for the most part. There is one problem though. The NEXT loop will keep going as it should, but I need to do something a little different since during testing I ran across a situation that needs to be corrected.

Before each new file, I need to confirm that the operator wants to run it. I think the issue with the code is just an order of operation situation.

Steps:

1) Start macro
2) User enters the panel number into the message box prompt
3) A confirmation box is used to validate the starting point input value
4) The file is run (place holder continue)
5) the user's input number + 1
6) Verify that the user wants to run the next panel number (user's input + some value between 1 and 120)
7) The file is run
8) repeat till all files are run


What do I need to amend?


current code:

Code:
Sub multi_panel_3()

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    
        confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then
        
            
            
            continue = MsgBox("run " & x & ".tap", vbOKCancel)  ' placeholder for file call
                
            If continue = 1 Then
            
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
            For i = x To 120
            
            Next i
            
            End If
            
        Else: End If 'place holder for  code "M30" End If

Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub


Thanks for the assistance!

Mike
 
Code:
Sub multi_panel_3()

Dim i As Integer
Dim x As Integer

x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x >= 1 And x <= 120 Then
    
        confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)

        If confirm = 1 Then
        
            
            
            continue = MsgBox("run " & x & ".tap", vbOKCancel)  ' placeholder for file call
                
            If continue = 1 Then
            
               
            For i = x To 120
                [b]'Answer = Msgbox here to run the cnc 
                'If Answer = No Then Exit For[/b]
                ' loadrun("c:\CNC\panel" & i & ".tap")
                ' code "M00"
            Next i
            
            End If
            
        Else: End If 'place holder for  code "M30" End If

Else
    MsgBox ("You entered Panel " & x & ".  " & "Panel number is outside the range of 1 to 120.")
End If

End Sub

Skip,

[glasses]Just traded in my OLD subtlety...
for a NUance![tongue]

"The most incomprehensible thing about the universe is that it is comprehensible" A. Einstein
 
This makes a little more sense (to me, at least...) VBA approach, I hope it works the same in Mach3

Code:
Sub multi_panel_3()[green]
'1) Start macro[/green]
Dim i As Integer
Dim x As Integer
[green]
'2) User enters the panel number into the message box prompt[/green]
x = InputBox("What panel number would you like to start with?", "Starting Panel Number")

If x < 1 or x > 120 Then
    MsgBox ("You entered Panel " & x & ".  Panel number is outside the range of 1 to 120.")
    Exit Sub
End If
[green]
'3) A confirmation box is used to validate the starting point input value[/green]
If vbOK = MsgBox("Are you ready to run file panel" & x & ".tap" ?", vbOKCancel) Then
    For i = x To 120
        If vbOK = MsgBox("run " & x & ".tap ?", vbOKCancel) Then [green]
            ' loadrun("c:\CNC\panel" & i & ".tap")
            ' code "M00"[/green]
        End If
    Next i
End If

End Sub

'Starting point' question in MsgBox will be asked twice.
Do you really need to ask first time?

---- Andy

There is a great need for a sarcasm font.
 
>I would assume Mike is using something like Excel VBA

Nah, if they are trying to script Mach 3, then they are using the Mach 3 scripting language, which is Cypress Enable, which is a VBA-alike - one that doers not appear to have any built-in predefined constants ... Combo has already linked to a relevant manual
 
>[Mach 3] doers not appear to have any built-in predefined constants
But it looks like [tt]vbOKCancel[/tt] works OK for Mike... [ponder]


---- Andy

There is a great need for a sarcasm font.
 
We see the InputBox code result, no MsgBox ...
remeng said:
Before each new file, I need to confirm that the operator wants to run it. I think the issue with the code is just an order of operation situation.

For MsgBox, if in Mach3 [tt]vbOKCancel=0[/tt], only OK button is displayed and 1 returned, 1=1, so we always enter the [tt]For...Next[/tt] loop. According to the manual, the code should be:
[tt]confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", 1+32, "Confirm run")[/tt]

combo
 
Hi All;

I am still having the issue. Andrzejek code doesn't keep the NEXT loop going.

Again, I think it is an order of operation in my code.

Combo,

I don't understand what your last message meant. It might be just the formatting of the post. Could you please clarify?

Thanks,

Mike
 
Assumed you have Cypress VB script (you have not confirmed this). I am basing on the manual I linked to above.

If your script does not support VB/VBA named constans, in [tt]confirm = MsgBox("Are you ready to run file " & "panel" & x & ".tap" & " ?", vbOKCancel)[/tt] you have [tt]vbOKCancel=0[/tt], unless earlier you declared it as =1. If you pass o, the manual states 'Display OK button only.' Is it your case? If so, as MsgBox function returns the value of button clicked, you get [tt]confirm=1[/tt] (i.e. OK button selected, no other option). In next line you have [tt]If confirm = 1 Then[/tt], that is always True. The same refers to next lines and 'continue' testing.

You need to assign 1 to MsgBox Type argument if you plan to have OK and Cancel buttons. 32 is for question icon, not necessary, but in line with the message purpose.


combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top