11 Replies - 1155 Views - Last Post: 20 March 2010 - 02:26 PM Rate Topic: -----

#1 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

How do I make a loop?

Posted 19 March 2010 - 11:25 AM

Heres my code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim input As integer
        input = InputBox("Please Enter: ")
        
    End Sub


How do I make it if I type in a number and then press OK, the input box would loop around to that number?
Is This A Good Question/Topic? 0
  • +

Replies To: How do I make a loop?

#2 bflosabre91  Icon User is offline

  • go sabres

Reputation: 105
  • View blog
  • Posts: 1,439
  • Joined: 22-February 08

Re: How do I make a loop?

Posted 19 March 2010 - 11:35 AM

what do u mean loop around to that number? im not sure what ur trying to accomplish
Was This Post Helpful? 0
  • +
  • -

#3 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: How do I make a loop?

Posted 19 March 2010 - 11:55 AM

For example, if you type in "5": the inputbox would then come up again 5 times. Sorry for not explaining better.

This post has been edited by mbrother64: 19 March 2010 - 11:56 AM

Was This Post Helpful? 0
  • +
  • -

#4 CodeMonkey10  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 09-March 10

Re: How do I make a loop?

Posted 19 March 2010 - 03:41 PM

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim input As integer
        input = InputBox("Please Enter: ")
        Dim Number as integer
        Dim Count as integer
        for Count = 0 to Number 
             'statements'
        next count        
    End Sub


Count will store the number loop it is on, eg: If the statements are being run for the first time, Count will equal zero.

This post has been edited by CodeMonkey10: 19 March 2010 - 03:42 PM

Was This Post Helpful? 1
  • +
  • -

#5 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: How do I make a loop?

Posted 19 March 2010 - 04:23 PM

 

        Dim input As integer
        input = InputBox("Please Enter: ")

        Dim Count as integer
        for Count = 0 to Input ' <- I changed this
             'statements'
        next count        
  



Wouldn't it work better like that above?

This post has been edited by vbnet9: 19 March 2010 - 04:25 PM

Was This Post Helpful? 1
  • +
  • -

#6 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: How do I make a loop?

Posted 19 March 2010 - 06:19 PM

Neither of those codes work.

When I type a number into the inputbox and then press OK, I want the number the input box to loop around to that number. For example: I click on the button, a messagebox comes up, I type in "2" and press OK, I want the inputbox to come up again 2 times, causing me to press OK 2 times.

This post has been edited by mbrother64: 19 March 2010 - 06:20 PM

Was This Post Helpful? -1
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: How do I make a loop?

Posted 19 March 2010 - 07:07 PM

OK, i've been looking at this and I think I'll jump in with this

The InputBox is a method of getting a response from the user. In the case of your code you are asking for a number that you would like to use as a total loop count. Here is the problem. When you ask the user how many times to loop from the response of the inputbox, you will then set up to run the loop. You can then, inside the loop, repeat the line input = InputBox("Please Enter:") but you cannot move backward to repeat the original. (Short of using a goto statement that I don't recommend)

So here is what will do what you're asking for
Dim input As integer
        input = InputBox("Please Enter: ")

        Dim Count as integer
        for Count = 1 to Input ' <- I changed this
             input = InputBox("Please Enter: ")
        next count        


Now here is the problem with this code
1. You are not doing anything to ensure that the user has entered a number which WILL result in an error if text is entered
2. input will equal whatever the last inputbox received before the loop ends

Perhaps, if you could explain exactly what you're wanting to accomplish from this, we can help you come up with a solution.
Was This Post Helpful? 0
  • +
  • -

#8 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: How do I make a loop?

Posted 19 March 2010 - 08:32 PM

Heres what I'm trying to accomplish as a whole:

"When the program starts:

• When the user selects the Add Records button, the programme asks the user how many products they wish to enter e.g. 5. This number determines the upper limit of the loop e.g. for i = 1 to NumRecords. If the user selects Cancel, then the programme needs to exit the sub routine without causing any problems.
• The programme will then go into a loop which is executed the number of times the user entered in response to “how many items do you wish to record?” If the user selects Cancel, then the programme needs to exit the sub routine without causing any problem.
• The number entered for the Sales Amount must be between 1 and 10000. If the number entered falls outside this range, then an error message is displayed and the user must re-enter the amount again.
• As each pair of products and amount of the sales made is entered, the programme will record the product name and amount of the sale in the All Sales list. It then records whether the pair is a High or a Low Sale in the relevant list and updates the total and average in the summary section of the form.
• If the user should select Cancel at any point during the data entry phase, then the programme will exit the sub routine.
• When the user selects the Exit button, the programme will display a message box asking the user if they are sure they wish to exit the programme. If the answer is No, then the user is returned to the application. If the answer is yes then the application will exit."

But first things first, I want to know how to create a loop as mentioned before.
Was This Post Helpful? 0
  • +
  • -

#9 Luc001  Icon User is offline

  • D.I.C Addict

Reputation: 84
  • View blog
  • Posts: 617
  • Joined: 04-May 09

Re: How do I make a loop?

Posted 20 March 2010 - 06:35 AM

Hi,

Like you said, first things first.

The inputbox isn't made for that, but it Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.

So, I suggest do something with a NumericInputbox.
I made something for you to show you how you can create a loop.
Just do a test and add a numericInputbox and a button to your form and then copy this code. See what it does.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim input As Integer
        input = NumericUpDown1.Value
        Dim intcount As Integer = 0
        Dim intnumber As Integer = input
        Dim counter As Integer = NumericUpDown1.Value
        While intnumber > 0
            intnumber -= 1
            intcount += 1
            MsgBox("You've got" & " " & counter & "" & " loops")
            counter -= 1
            If counter = 0 Then
                NumericUpDown1.Value = 0
            End If
        End While

       
    End Sub


I know it isn't done yet, but it will give you an idee.
Was This Post Helpful? 1
  • +
  • -

#10 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: How do I make a loop?

Posted 20 March 2010 - 01:55 PM

This is the code and idea that I'm going by:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer
        Dim num As Integer
        Dim Subject As String

        num = Val(TextBox1.Text)

        For i = 1 To num2
            Subject = InputBox("What is the subject name?")
        Next
    End Sub


You type in how many times you want the inputbox to loop in the textbox and then click on the button, and an inputbox successfully comes up that many times. But this time instead of typing the number of how many times I want the inputbox to loop in the textbox, I want to type the number in the inputbox. I'm just racking my brain on how to transform this code into what I'm trying to do. I'm a total noob and I just cant see it.

This post has been edited by mbrother64: 20 March 2010 - 02:11 PM

Was This Post Helpful? 0
  • +
  • -

#11 vbnet9  Icon User is offline

  • D.I.C Regular

Reputation: 12
  • View blog
  • Posts: 346
  • Joined: 26-May 09

Re: How do I make a loop?

Posted 20 March 2010 - 01:58 PM

View Postmbrother64, on 19 March 2010 - 05:19 PM, said:

Neither of those codes work.

When I type a number into the inputbox and then press OK, I want the number the input box to loop around to that number. For example: I click on the button, a messagebox comes up, I type in "2" and press OK, I want the inputbox to come up again 2 times, causing me to press OK 2 times.



Well first you have to finish the code to make it work. We are trying to get you on your way... not do it for you! And by the way, the code does work, you just need to tell it what you want it to do for you.
Was This Post Helpful? -1
  • +
  • -

#12 mbrother64  Icon User is offline

  • D.I.C Head

Reputation: -9
  • View blog
  • Posts: 78
  • Joined: 30-August 09

Re: How do I make a loop?

Posted 20 March 2010 - 02:26 PM

YAY! I finally got it to work.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Subject As String
        Subject = InputBox("How many")
        Dim i As Integer
        Dim num As Integer


        For i = 1 To Subject
            num = Val(InputBox("This many"))
        Next
    End Sub


Thanks everybody. It was right in front of me.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1