|
-
Oct 17th, 2006, 05:25 AM
#1
Thread Starter
New Member
what am i doing wrong
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
Last edited by Hack; Oct 17th, 2006 at 06:12 AM.
Reason: Added [vbcode] [/vbcode] tags
-
Oct 17th, 2006, 06:13 AM
#2
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.
Regards,
Mark
Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."
-
Oct 17th, 2006, 06:13 AM
#3
Re: what am i doing wrong
-
Oct 17th, 2006, 06:29 AM
#4
Hyperactive Member
Re: what am i doing wrong
 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.
-
Oct 17th, 2006, 09:34 AM
#5
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
-
Oct 18th, 2006, 04:28 AM
#6
Thread Starter
New Member
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
-
Oct 18th, 2006, 04:29 AM
#7
Thread Starter
New Member
Re: what am i doing wrong
oh soz i didnt see your reply thank you
-
Oct 18th, 2006, 05:04 AM
#8
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.
-
Oct 20th, 2006, 06:27 AM
#9
Thread Starter
New Member
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
-
Oct 20th, 2006, 06:38 AM
#10
PowerPoster
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 :-)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
-
Oct 20th, 2006, 06:49 AM
#11
Re: what am i doing wrong
 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
-
Oct 20th, 2006, 06:52 AM
#12
PowerPoster
Re: what am i doing wrong
 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)
Well, everyone else has been doing it :-)
Loading a file into memory QUICKLY - Using SendKeys - HyperLabel - A highly customisable label replacement - Using resource files/DLLs with VB - Adding GZip to your projects
Expect more to come in future
If I have helped you, RATE ME! :-)
I love helping noobs with their VB problems (probably because, as an amateur programmer, I am only slightly better at VB than them :-)) but if you SERIOUSLY want to get help for free from a community such as VBForums, you have to first have a grounding (basic knowledge) in VB6, otherwise you're way too much work to help...You've got to give a little if you want to get help from us, in other words!
And we DON'T do your homework. If your tutor doesn't teach you enough to help you make the project without his or her help, FIND A BETTER TUTOR or try reading books on programming! We are happy to help with minor things regarding the project, but you have to understand the rest of it if you want our help to be useful.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|