|
-
Apr 6th, 2008, 08:00 AM
#1
Thread Starter
New Member
Previous / Next Function
Hello there!
i am quite new to VB6 (Been learning it for 3 months at college) and im finding it very interesting and simple to program things with it, but i am however struggling with this perticular piece of work.
What i have created for a college assignment is a Hurricane category program. It works by asking the user to input the location(s) and windspeed(s) of various hurricanes.
Once the user has done this, the program tests the criteria to see what category each storm is, and what effect each category will have.
to get input from the user i first ask them how many storms they are watching, i then ask them to input the windspeed and locations. The information is stored in an array module so it can be used in other forms.
I also have a Previous / Next form with buttons that allow the user to see all of the storms they have entered with labels describing what course of action
depending on the category.
The Problem:
Here is the code to get input from the user.
VB Code:
amount(x) = InputBox("Please enter the amount of storms you are watching")
If amount(x) < 1 Then
MsgBox ("please enter a value")
End If
For x = 0 To amount(x) - 1
location(x) = InputBox("Please enter the location")
speed(x) = InputBox("Please enter the speed in MPH")
If speed(x) >= 74 And speed(x) <= 96 Then
Category(x) = ("1")
ElseIf speed(x) >= 97 And speed(x) <= 110 Then
Category(x) = ("2")
ElseIf speed(x) >= 111 And speed(x) <= 130 Then
Category(x) = ("3")
ElseIf speed(x) >= 131 And speed(x) <= 155 Then
Category(x) = ("4")
ElseIf speed(x) > 155 Then
Category(x) = ("5")
End If
Next
End Sub
This part works brilliantly, the data the user inputs is displayed in listboxes in the next form.
My problem lies here with my Previous / Next form
VB Code:
Private Sub CmdNext_Click()
lbl5.Visible = False
'Counter control variable to display the next set of information
'entered by the user. A label is displayed if the category is 5, it
'tells the user to "EVACUATE"
If x < amount(x) Then
amount(x) = amount(x) + 1
Else
amount(x) = 0
End If
txtCategory.Text = Category(x)
TxtLocation.Text = location(x)
If txtCategory = "5" Then
lbl5.Visible = True
End If
End Sub
VB Code:
Private Sub CmdPrev_Click()
lbl5.Visible = False
'Counter control variable to display the previous set of information
'entered by the user. A label is displayed if the category is 5, it
'tells the user to "EVACUATE"
If x > amount(x) Then
x = amount(x) - 1
Else
x = 0
End If
txtCategory.Text = Category(x)
TxtLocation.Text = location(x)
If txtCategory = "5" Then
lbl5.Visible = True
End If
End Sub
It will not loop round the data, ive tried all different combinations but i just cannot do it =\
-
Apr 6th, 2008, 08:16 AM
#2
Re: Previous / Next Function
From a cursory look at your code it appears that valid x values range from 0 to amount(x) - 1.
Also if amount is an array I suspect this is an error. Isn't there really just one amount involved here?
In your "wrap around" logic I think you need to test against amount(x) - 1 (or just a non-array amount - 1) in the forward case, and against having reached 0 when going backward (i.e. "previous").
-
Apr 6th, 2008, 12:48 PM
#3
Thread Starter
New Member
Re: Previous / Next Function
although im very grateful for your help
i will admit that i dont fully understand what you mean =\
if it is possible, could you perhaps "dumb" it down so to speak haha
-
Apr 6th, 2008, 12:57 PM
#4
Re: Previous / Next Function
Is what you posted, your actual code? If so, add Option Explicit to the top of your form & press Ctrl+F5. Any errors? If so those, need to be addressed first.
Second. What is X? You use it quite often but it is not declard anywhere. Not only that, but you are freely using it as a loop variable, some input type variable and an array index -- this is going to cause you issues.
Third, as dilettante pointed out, why do you have amount() as an array? Your app will only have one amount value, ever, correct? Therefore, make it easier on yourself and declare Amount as a Long, not as an array.
-
Apr 6th, 2008, 03:21 PM
#5
Thread Starter
New Member
Re: Previous / Next Function
I see
i have got option explicit on my coding, but i just missed it out by accident
I have declared X in a module as my array index, as you rightly said using it for a loop variable caused me issues, i changed the loop variable to "pos" instead.
i used "amount(x)" because if you look at the first set of coding, "amount(x)" represents the amount of storms the user is watching, so i figured that using it here would cause the loop to go around the amount of storms the user entered.
but i have gotten rid of Amount(x) and declared is as long.
again getting input and displaying the data works fine. Its just this part which seems to go wrong.
again thanks for the help
-
Apr 6th, 2008, 03:23 PM
#6
Thread Starter
New Member
Re: Previous / Next Function
For pos = 0 To amount
If pos < amount Then
pos = amount + 1
Else
pos = 0
End If
this is what i have now, it still wont work however
-
Apr 6th, 2008, 04:30 PM
#7
Re: Previous / Next Function
You are not using a For:Next loop properly. pos will always be less than amount until the loop terminates natually. It starts at zero and ends after it equals amount. You do not manually increment pos, the For:Next does that automatically.
Recommend this: Declare form-level variable lStep As Long
Clicking next:
Each time next is clicked increment lStep. If it is > Amount, then reset it back to first step or abort whichever is appropriate.
Clicking previous:
Each time previous is clicked, decrement lStep. If it is < first step then abort or set to Amount (last step) whichever is appropriate
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
|