Results 1 to 12 of 12

Thread: what am i doing wrong

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    what am i doing wrong

    VB Code:
    1. Option Explicit
    2. Private Sub cmdForLoop_Click()
    3. Dim number As Integer
    4. Dim finalLoopNumber As Integer
    5. Dim firstLoopNumber As Integer
    6.  
    7. Form1.Cls
    8.  
    9. firstLoopNumber = Val(txtFirstNumber)
    10. finalLoopNumber = Val(txtSecondNumber)
    11.  
    12. If txtFirstNumber < txtSecondNumber Then
    13.     For number = firstLoopNumber To finalLoopNumber
    14. Else
    15.     For number = finalLoopNumber To firstLoopNumber
    16. End If
    17.  
    18. Print number;
    19. Next number
    20. End Sub
    Last edited by Hack; Oct 17th, 2006 at 06:12 AM. Reason: Added [vbcode] [/vbcode] tags

  2. #2
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    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."


  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: what am i doing wrong

    Moved from the CodeBank

  4. #4
    Hyperactive Member
    Join Date
    Oct 2006
    Location
    USA
    Posts
    476

    Re: what am i doing wrong

    Quote Originally Posted by seth050190
    VB Code:
    1. Option Explicit
    2. Private Sub cmdForLoop_Click()
    3. Dim number As Integer
    4. Dim finalLoopNumber As Integer
    5. Dim firstLoopNumber As Integer
    6.  
    7. Form1.Cls
    8.  
    9. firstLoopNumber = Val(txtFirstNumber)
    10. finalLoopNumber = Val(txtSecondNumber)
    11.  
    12. If txtFirstNumber < txtSecondNumber Then
    13.     For number = firstLoopNumber To finalLoopNumber
    14. Else
    15.     For number = finalLoopNumber To firstLoopNumber
    16. End If
    17.  
    18. Print number;
    19. Next number
    20. 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.

  5. #5
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: what am i doing wrong

    Its a structured language and not declarative, so your implementation of for loops is wrong.
    VB Code:
    1. If txtFirstNumber < txtSecondNumber Then
    2.     For number = firstLoopNumber To finalLoopNumber
    3.        Print number;
    4.     Next number
    5. Else
    6.     For number = finalLoopNumber To firstLoopNumber
    7.        Print number;
    8.     Next number
    9. End If

  6. #6

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    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

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    Re: what am i doing wrong

    oh soz i didnt see your reply thank you

  8. #8
    Frenzied Member cssriraman's Avatar
    Join Date
    Jun 2005
    Posts
    1,465

    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.
    CS

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2006
    Posts
    4

    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

  10. #10
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    Re: what am i doing wrong

    One suggestion for you:

    Where you have
    VB Code:
    1. firstLoopNumber = Val(txtFirstNumber)
    2. finalLoopNumber = Val(txtSecondNumber)
    3.  
    4. If txtFirstNumber < txtSecondNumber Then
    5.     For number = firstLoopNumber To finalLoopNumber
    6. Else
    7.     For number = finalLoopNumber To firstLoopNumber
    8. 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:
    1. If txtFirstNumber < txtSecondNumber Then
    2.     For number = firstLoopNumber To finalLoopNumber
    3. Else
    4.     For number = finalLoopNumber To firstLoopNumber
    5. End If
    6.  
    7. Print number;
    8. Next number
    9. End Sub

    to

    VB Code:
    1. If txtFirstNumber < txtSecondNumber Then
    2.     For number = firstLoopNumber To finalLoopNumber
    3.     Print number;
    4.     Next number
    5. Else
    6.     For number = finalLoopNumber To firstLoopNumber
    7.     Print number;
    8.     Next number
    9. End If
    10.  
    11. 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.

  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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

  12. #12
    PowerPoster
    Join Date
    May 2006
    Location
    Location, location!
    Posts
    2,673

    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:
    1. 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
  •  



Click Here to Expand Forum to Full Width