code for previous comand button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mustakbal
    New Member
    • Jul 2007
    • 25

    #1

    code for previous comand button

    This has to be very simple but I can't get it right and I'm getting frustrated with it. My code for the Next button works fine while using "index1" for lowerlimit and "index2" for the upperlimit and it goes like this :

    Code:
    If index1 < index2 Then
                   index1 = index1 + 1
              Else
                 MsgBox "This is the last record of the data"
               End If
    but I can't get anything to work for the Previous command button so it stops at the lowerlimit and not keep going til the beginning of the file. I don't know if the simplest way to do this is with a For loop or If statement or Do While....
    So I am all ears ..
    Thanks in advance
  • Trent
    New Member
    • Jul 2007
    • 2

    #2
    Originally posted by mustakbal
    This has to be very simple but I can't get it right and I'm getting frustrated with it. My code for the Next button works fine while using "index1" for lowerlimit and "index2" for the upperlimit and it goes like this :

    Code:
    If index1 < index2 Then
                   index1 = index1 + 1
              Else
                 MsgBox "This is the last record of the data"
               End If
    but I can't get anything to work for the Previous command button so it stops at the lowerlimit and not keep going til the beginning of the file. I don't know if the simplest way to do this is with a For loop or If statement or Do While....
    So I am all ears ..
    Thanks in advance
    You are changing the lowerlimit (index1) when you click the Next button.

    Try this for the Next Button
    Code:
    If index < index2 Then
                   index = index + 1
              Else
                 MsgBox "This is the last record of the data"
               End If
    And this for the Previous Button
    Code:
    If index >= index1 Then
                    index = index - 1
                Else
                    MsgBox "This is the first record of the data"
                End If

    Comment

    • mustakbal
      New Member
      • Jul 2007
      • 25

      #3
      Trent, thanks for the help, but that didn't work either .As soon as I hit the previous button I get the message no matter what the position in the file is at that time. I was told that I need three variables for lowerlimit, upperlimit and currentposition -- but I still don't know if to use a for loop or do while or whatever. Any suggestions on that?

      Comment

      • Trent
        New Member
        • Jul 2007
        • 2

        #4
        Originally posted by mustakbal
        Trent, thanks for the help, but that didn't work either .As soon as I hit the previous button I get the message no matter what the position in the file is at that time. I was told that I need three variables for lowerlimit, upperlimit and currentposition -- but I still don't know if to use a for loop or do while or whatever. Any suggestions on that?
        That's strange, it works for me except the first line of the code for the Previous Button should be:

        Code:
        If index > index1 Then
        The only scenario I can think of that would continue to give you this problem is you are still using index1 in the code for the Next Button.

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #5
          Has index been set, anywhere?

          Not having seen the rest of the code, I'm just guessing. But it looks as though we may have just introduced a new variable, index, without actually using it for anything. If that's so, then it will have the value zero, which will (presumably) always be less than index1.

          Comment

          • mustakbal
            New Member
            • Jul 2007
            • 25

            #6
            That's true killer42, I got the error that index is not defined.The code for the Next and the Previous are :

            Code:
             
              Private Sub CmdN_Click()
            If index1 < index2 Then    ' I am using index2 as upperlimit
              index1 = index1 + 1
              Else
              MsgBox "This is the last record of the data"
                End If
            lbltext1.Caption = line_value1(index1)
            
            Private Sub CmdR_Click()
            If index1 > index1 Then   ' index1 for lower limit
              index1 = index1 - 1
              Else
                MsgBox "This is the first record of the data"
                End If
            lbltext1.Caption = line_value1(index1)
            end
            That's what I had at the very beginning. The Next button works fine but the Previous doesn't.
            Trent I need to get back to index1 or I do not get anything displayed in lbltext1.

            Maybe this eil make it easier to figure out the problem.
            Thanks

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              Sounds to me as though you need to use three variables rather than two. Which is what Trent was driving at, I'm sure. You see, at the moment you are treating index1 as both the current position and the lowest-allowed position. For example, you have a line which says "If index1 > index1". This is not possible. A variable cannot have a value different to itself.

              What you need to do is use a separate variable (such as index) to indicate the current position within the range of index1 to index2. It then follows, of course, that index needs to be initialised to the current position (probably the same value as index1) before you start trying to adjust it through these two buttons. Most likely, index1 and index2 should never change during this processing.

              Comment

              • mustakbal
                New Member
                • Jul 2007
                • 25

                #8
                So why doesn't Trent's code work even after I define "index " as long and then initialise it as = index1? Could you take a closer look at that because this is really driving me nuts.

                It was simpler and it worked fine when I had "If Index1 > 1" and when it went back to the beginning of the record!

                Did I do this the right way whe I "Dim index as long" and then
                initialised " index = index1"? because it stil doesn't work.

                Comment

                • Killer42
                  Recognized Expert Expert
                  • Oct 2006
                  • 8429

                  #9
                  Originally posted by mustakbal
                  So why doesn't Trent's code work even after I define "index " as long and then initialise it as = index1? Could you take a closer look at that because this is really driving me nuts.

                  It was simpler and it worked fine when I had "If Index1 > 1" and when it went back to the beginning of the record!
                  Hey, if it works, it works. That's the name of the game, huh. Although generally speaking, if something doesn't work I feel it's probably a good idea to have some idea of why. But that's just me.

                  Originally posted by mustakbal
                  Did I do this the right way when I "Dim index as long" and then initialised " index = index1"? because it stil doesn't work.
                  My guess is that the problem is the scope of the variable. Without seeing the whole code it's hard to be sure. One question, though - where did you put that Dim?

                  Comment

                  Working...