|
|
|||||||||
|
|||||||||
|
|||||||||
| |
||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Times Table
Hi I am trying to print the 10 times table but can't get the code to work?
Cheers ...Dez... |
|
#2
|
||||
|
||||
|
Quote:
Post your attempted code, and errors if any.
__________________
Fitness & Diet resources Career Descriptions Boat Cruises All code that is posted by me has not been tested, and it should only be interpreted as a guideline to a solution. There is no guarantee that any of my code samples will work as provided, and should be customized to suite the required need. |
|
#3
|
||||
|
||||
|
Quote:
I agree with D.O.M.I.N.A.T.O.R, it is a good idea to post your code along with descriptions of any errors or unexpected output to illustrate your problem and your attempt to solve it. Without knowing exactly what you are trying to achieve, or if you have indeed attempted it yourself, I will demonstrate how to calculate the 10 times table using a For..Next Loop: Code:
Private Sub Command1_Click() Dim counter As Integer Dim iterations As Integer iterations = 1 For counter = 10 To 100 Step 10 Debug.Print iterations & " x 10 = " & counter iterations = iterations + 1 Next End Sub |
|
#4
|
|||
|
|||
|
Hey thanks guys, I suppose what I am really trying to do is ask the user what table they want printed. Then print it out on a form up to 10X. Then include a continue box and if the user clicks on continue then the next 10 times (i.e. 11X, 12X..up to...20X) are printed and so on.
My code is just a poor variation of what you already have shown me.. Function Times() Dim counter As Integer Dim iterations As Integer Dim start As String start = InputBox("Enter the times table you want printed.") iterations = 1 For counter = start To 100 Step start Debug.Print iterations & " X " & start; " = " & counter iterations = iterations + 1 Next End Function Cheers and thank you ...Dez... |
|
#5
|
|||||
|
|||||
|
The code sample you're showing will only render iterations of the 10 times table in increments of 10.
Try the following code. Create a textbox control or button for the user to select the times table they want and pass the value to the Times function. And you can have another to button to perform the task again by submitting the same value to the times function. vb Code:
|
|
#6
|
|||
|
|||
|
Here is where I am at with this code so far I now want to continue with start+10 ask the question again etc..
Function ContinueTimes() Dim counter As Integer Dim iterations As Integer Dim start As String start = InputBox("Enter the times table you want printed.") iterations = 1 For counter = 1 To 10 Debug.Print counter & " X " & start; " = " & counter * start iterations = iterations + 1 Next Dim Msg, Style, Title, Response Msg = "Do you want the next ten?" Style = vbYesNo Response = MsgBox(Msg, Style) If Response = vbNo Then Exit Function End If End Function |
|
#7
|
|||
|
|||
|
Using the code I gave you above, if you place the code in the general declaration area of the form module. And you add a textbox to your form and name it UserValue. Then create a buttom and add e=Times(cint(UserValue.text)) to the click event code, (e can be for the return value if you choose to have one).
If the user enters a number in the UserValue and clicks the button, it will call the Times function and pass it the UserValue, which will show the times table for that UserValue in increments of 10, (10 has been hardcoded into the function). For example, if UserValue = 2 then it will show the following: 1 x 2 = 2 2 x 2 = 4 ... 9 x 2 = 18 10 x 2 = 20 If the user clicks the button again, the function with show the continuation for that same UserValue. 11 x 2 = 22 12 x 2 = 24 ... 19 x 2 = 38 20 x 2 = 40 , and will continue incrementing in groups of 10 each time the user clicks the button. Until the user enters a different value in the UserValue textbox, then when they click the button, the function will start with a fresh times table starting from 1 x the UserValue on through to 10 x the UserValue and if they click the button again it will continue with 11 x the UserValue for however many times the button is clicked. As long as the UserValue has not been changed. This happens because the variables: start, endat, and multiplier have module wide scope. They retain their value after the function ends. And the function can use them and can modify their values. That said, your code shows that you want to use recursion. You've already told your function what to do if the user clicks No, but you haven't told it what to do if they click Yes. |
|
#8
|
|||
|
|||
|
Times Table Code
Hi
I just noticed this thread whilst looking for some help myself.. Can anyone tell me how to code a form to calculate a basic times table 1-10 for any given input and then display the outcome on a seperate form? Basically i want a user to be able to input a number in a text box, hit a command button, then the code (loop ??) work out the times table and display this on a seperate form.. Im a newbie to VB and still learning, any advice would be greatly appreciated. |
|
#9
|
||||
|
||||
|
Aj,
In order to be able to display the times table in a different form you first have to declare a global variable. To do this go to Project -> Add Module, accept the default and click Open. You can now open up this module and declare a variable within it, just type Public strUserChoice As Integer Now, return to form1, add a Textbox and a Command Button and add the following code to the buttons click event: Code:
strUserChoice = Val(Text1.Text) Form1.Hide Form2.Show Now add a new form to your project (Project -> Add Form, accept the default and click Open). Drag a textbox onto form2 set its multiline property to True and resize it so that it fills most of the form, you can now add the following code to form2's code behind: Code:
Private Sub Form_Load() Text1.Text = "" Dim intCounter As Integer For intCounter = 1 To 10 Text1.Text = Text1.Text & intCounter & " X " & strUserChoice & " = " & Val(intCounter) * Val(strUserChoice) & vbCrLf Next End Sub |
![]() |
| Viewing: ASP Free Forums > Programming > Visual Basic Programming > Times Table |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|