Results 1 to 7 of 7

Thread: Previous / Next Function

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    4

    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:
    1. amount(x) = InputBox("Please enter the amount of storms you are watching")
    2.         If amount(x) < 1 Then
    3.           MsgBox ("please enter a value")
    4.         End If
    5.    
    6.  
    7.  
    8.     For x = 0 To amount(x) - 1
    9.          location(x) = InputBox("Please enter the location")
    10.          speed(x) = InputBox("Please enter the speed in MPH")
    11.          
    12.       If speed(x) >= 74 And speed(x) <= 96 Then
    13.          Category(x) = ("1")
    14.          
    15.       ElseIf speed(x) >= 97 And speed(x) <= 110 Then
    16.          Category(x) = ("2")
    17.        
    18.       ElseIf speed(x) >= 111 And speed(x) <= 130 Then
    19.          Category(x) = ("3")
    20.          
    21.       ElseIf speed(x) >= 131 And speed(x) <= 155 Then
    22.          Category(x) = ("4")
    23.        
    24.       ElseIf speed(x) > 155 Then
    25.          Category(x) = ("5")
    26.  
    27.    
    28.       End If
    29.     Next
    30.  
    31.  
    32.    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:
    1. Private Sub CmdNext_Click()
    2.  
    3.   lbl5.Visible = False
    4.  
    5.  
    6. 'Counter control variable to display the next set of information
    7. 'entered by the user. A label is displayed if the category is 5, it
    8. 'tells the user to "EVACUATE"
    9.  
    10.  
    11.    If x < amount(x) Then
    12.     amount(x) = amount(x) + 1
    13.   Else
    14.     amount(x) = 0
    15.   End If
    16.  
    17.   txtCategory.Text = Category(x)
    18.   TxtLocation.Text = location(x)
    19.  
    20. If txtCategory = "5" Then
    21.     lbl5.Visible = True
    22.   End If
    23. End Sub

    VB Code:
    1. Private Sub CmdPrev_Click()
    2.  
    3. lbl5.Visible = False
    4.  
    5. 'Counter control variable to display the previous set of information
    6. 'entered by the user. A label is displayed if the category is 5, it
    7. 'tells the user to "EVACUATE"
    8.  
    9.  
    10.   If x > amount(x) Then
    11.     x = amount(x) - 1
    12.   Else
    13.     x = 0
    14.   End If
    15.  
    16.   txtCategory.Text = Category(x)
    17.   TxtLocation.Text = location(x)
    18.  
    19.    
    20.    If txtCategory = "5" Then
    21.     lbl5.Visible = True
    22.   End If
    23.  
    24. End Sub


    It will not loop round the data, ive tried all different combinations but i just cannot do it =\

  2. #2
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

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

  3. #3

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    4

    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

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    4

    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

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2008
    Posts
    4

    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

  7. #7
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

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