Results 1 to 12 of 12

Thread: For Loops

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77

    Wink For Loops

    I am writing a program which asks the user to input a no and based on the number displays a msg those many no of times.

    The code is
    Private Sub txtNumber_Change()
    intNumber = CInt(txtNumber.Text)
    For i = 1 To intNumber Step 1
    lblMessage.Caption = "VB is Fun!!!"
    Next
    End Sub

    Pl.help

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Take the code out of the text box Change event, and put it into a command buttons click event.

  3. #3
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Code:
    Private Sub CommandButton1_Click() 
     
       'declare 2 variables
       Dim intCounter as Integer, intNumber
     
       'Display an inputbox which'll prompt the user for a number
       intNumber = Inputbox("Please enter a number")
     
    
       'was a number entered ?
       If IsNumeric(intNumber) Then
      
           For intCounter = 1 To intNumber 
    
                 'the vbcrlf bit inserts a new line at te end too
                 lblMessage.Caption = "VB is Fun!!!" & VBCrlf
    
           Next intCounter 
    
       End If
    End Sub

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2001
    Posts
    77
    Dim intNumber As Integer
    Dim i As Integer

    Private Sub cmdClick_Click()
    intNumber = CInt(txtNumber.Text)*
    For i = 1 To intNumber Step 1
    lblMessage.Caption = "VB is Fun!!!"
    Next
    End Sub

    the problem still persists *gives error

  5. #5
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657
    What error does it give you? If txtNumber.Text is > 32767 or contains non-numeric characters, CInt will have a problem. The quick & dirty solution would be to use Val instead of CInt; to do it the "right" way, you could be more stringent in the data entry process when the user enters the number. Also, you could put error-handling in the Sub.
    "It's cold gin time again ..."

    Check out my website here.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Try this in cmdClick_Click:

    Dim intNumber As Integer
    Dim i As Integer

    intNumber = CInt(Text1.Text)
    For i = 0 To intNumber
    lblMessage.Caption = ""
    lblMessage.Caption = "VB is Fun!!! "
    Next

    F8 through the sub so you can actually see the loop happening. If you just run it, it appears to only go through one time.

  7. #7
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Hi
    The Cxxx conversion functions usually fall over if the passed data is not in the expected format. So u should add a little error check at the beginning. This check will by no means cover every eventuality but i will leave to you to add more.
    VB Code:
    1. Dim intNumber As Integer
    2. Dim i As Integer
    3.  
    4. Private Sub Command1_Click()
    5.     If IsNumeric(txtnumber.Text) Then 'Added this check
    6.         intNumber = CInt(txtnumber.Text)
    7.         'removed Step 1 as that is default step
    8.         For i = 1 To intNumber
    9.             Changed next line for concatenation ( adding strings )
    10.             lblMessage.Caption = lblMessage.Caption & "VB is Fun!!!" & vbCrLf
    11.         Next
    12.     End If
    13. End Sub
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  8. #8
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    I am writing a program which asks the user to input a no and based on the number displays a msg those many no of times.
    The Cxxx conversion functions usually fall over if the passed data is not in the expected format
    My code not good enough for you lot then ?!?!?!?

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  9. #9
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by alex_read
    My code not good enough for you lot then ?!?!?!?
    Welllll actually. Your code wouldnt work cos of this line
    VB Code:
    1. lblMessage.Caption = "VB is Fun!!!" & VBCrlf
    and also u used the dreaded Input Box !!
    I only responded cos he said was still having errors
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  10. #10
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Welllll actually. Your code wouldnt work cos of this line
    The VBCrlf works with labels - put this in a new project & run :

    Private Sub Form_Load()
    Label1.Caption = "1" & vbCrLf & "2"
    End Sub


    Anyway, I'll let you off then
    Anyone know what you declare an inputbox as ?
    I always use a variant (I know I shouldn't) if I need to use it, there must be a proper way though. Cheers !

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  11. #11
    PowerPoster beachbum's Avatar
    Join Date
    Jul 2001
    Location
    Wollongong, NSW, Australia
    Posts
    2,274
    Originally posted by alex_read
    The VBCrlf works with labels -
    LOL of course it does and u will see that i used it butttttttttt u forgot to add the caption to itself ie
    VB Code:
    1. 'You had
    2. lblMessage.Caption = "VB is Fun!!!" & VBCrlf
    3.  
    4. 'Should be
    5. lblMessage.Caption = [b]lblMessage.Caption &[/b] "VB is Fun!!!" & VBCrlf
    Regards
    Stuart
    Stuart Laidlaw
    Brightspark Financial Software
    http://www.gstsmartbook.com

  12. #12
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Oh yeah
    That'd make a slight difference LOL !

    Well done for spotting my deliberate mistake there - 10 brownie points lol

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

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