Re: what am i doing wrong
First you posted to the wrong forum, this forum is for people to post interesting snippets of code/application that may be useful to others, you should have posted this question to the Classic VB Forum. Secondly, please wrap your code with the [VBCODE] your code goes in here [/VBCODE] tags as it makes it much easier to read.
MOD Please move this post to the Classic VB Forum, thank you.
Re: what am i doing wrong
Re: what am i doing wrong
Quote:
Originally Posted by seth050190
VB Code:
Option Explicit
Private Sub cmdForLoop_Click()
Dim number As Integer
Dim finalLoopNumber As Integer
Dim firstLoopNumber As Integer
Form1.Cls
firstLoopNumber = Val(txtFirstNumber)
finalLoopNumber = Val(txtSecondNumber)
If txtFirstNumber < txtSecondNumber Then
For number = firstLoopNumber To finalLoopNumber
Else
For number = finalLoopNumber To firstLoopNumber
End If
Print number;
Next number
End Sub
What and/or where is txtFirstNumber/txtSecondNumber? I'm assuming they're text boxes on a form somewhere. If so, VB may have trouble finding them unless you tell it where they are.
You're also forgetting something with your loops, i.e. you're leaving them open.
Re: what am i doing wrong
Its a structured language and not declarative, so your implementation of for loops is wrong.
VB Code:
If txtFirstNumber < txtSecondNumber Then
For number = firstLoopNumber To finalLoopNumber
Print number;
Next number
Else
For number = finalLoopNumber To firstLoopNumber
Print number;
Next number
End If
Re: what am i doing wrong
any ideas on a solution its just a simple form1
________________________________________________________________
label txtbox - txtFirstNumber
label txtbox - txtSecondNumber
cmdbutton - command loop number
its just a simple thing that prints the nubers along the top of the page the error message it gives is else without if but there is an IF statement there
i am such a NOOB please help
Re: what am i doing wrong
oh soz i didnt see your reply thank you
Re: what am i doing wrong
When you have received an answer to your question, please mark it as Resolved using the Thread Tools menu.http://www.vbforums.com/images/ieimages/2004/12/1.gif
Re: what am i doing wrong
it still not working, i want the numbers if i type 2 in txtfirstnumber and 1 in txtsecondnumber to print on the form 2 1 and if i type 1 2 in text boxes it to print on page 1 2 any ideas soz that im such a noob
Re: what am i doing wrong
One suggestion for you:
Where you have
VB Code:
firstLoopNumber = Val(txtFirstNumber)
finalLoopNumber = Val(txtSecondNumber)
If txtFirstNumber < txtSecondNumber Then
For number = firstLoopNumber To finalLoopNumber
Else
For number = finalLoopNumber To firstLoopNumber
End If
where you have the if/then/else have it so it sets firstloopnumber to txtfirstnumber (and same for second) in the loop if txtfirstnumber < txtsecondnumber...that way you can have it doing what you want either way. I could probably explain this more clearly but I'm about to go out :-)
Putting it basically, I am suggesting moving the if/then/else to where you set firstloopnumber and secondloopnumber so that THEY get set based on the values in txtfirstnumber and txtsecondnumber rather than you trying to use a for/next loop like you are (which won't work...logically in your brain you can follow them but the computer will refuse to let it run because it's like that :-))
One other alternative...change:
VB Code:
If txtFirstNumber < txtSecondNumber Then
For number = firstLoopNumber To finalLoopNumber
Else
For number = finalLoopNumber To firstLoopNumber
End If
Print number;
Next number
End Sub
to
VB Code:
If txtFirstNumber < txtSecondNumber Then
For number = firstLoopNumber To finalLoopNumber
Print number;
Next number
Else
For number = finalLoopNumber To firstLoopNumber
Print number;
Next number
End If
End Sub
This makes the loops nested within the if/then/else and they should run fine then :-)
Re: what am i doing wrong
Quote:
Originally Posted by seth050190
it still not working, i want the numbers if i type 2 in txtfirstnumber and 1 in txtsecondnumber to print on the form 2 1 and if i type 1 2 in text boxes it to print on page 1 2 any ideas soz that im such a noob
Then you don't need the loop.
Form1.Print txtfirstnumber.text & " " & txtSecondNumber.Text
Re: what am i doing wrong
Quote:
Originally Posted by seth050190
it still not working, i want the numbers if i type 2 in txtfirstnumber and 1 in txtsecondnumber to print on the form 2 1 and if i type 1 2 in text boxes it to print on page 1 2 any ideas soz that im such a noob
If you're using a for/next loop and the first number is higher than the second number then you have to use step
VB Code:
for b = 100 to 1 step -1:print b:next b
This would print numbers from 100 to 1. AFAIK you would need this if the step isn't 1 (the default)