dates and msflexgrid problem trying to loop dates on a grid control for 2 weeks

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • breakdance89
    New Member
    • Jan 2013
    • 14

    dates and msflexgrid problem trying to loop dates on a grid control for 2 weeks

    I have this code which puts correct dates on msflexgrid control for weekly rotas on my program. What it currently does is checks which day is monday and the loops it through creating dates accordingly to as monday, tuesday etc.

    now I have a problem trying to create a rota for 2 weeks instead of one. if i just copy the code it will work for the time being but as soon as that week is over both rotas just skip a whole week instead of running like 14 days. 1 , 2 , 3 , 4 .... 14 and then loop both rotas on to staring 15 (monday) 16, 17 , 28 etc.

    Code:
    Dim f As Integer
    dim tDate as Date
    tDate = now
        For f = 1 To 7
           tDate = Format(tDate, "d/m/yy")
           grdRota.TextMatrix(1, f) = DateAdd("d", f - 1, tDate)
    
       Next f
    grdRota is msflexgrid1.
    Last edited by Rabbit; Jan 29 '13, 03:56 AM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    I'm not sure I understand the question. Some sample data would help.

    Comment

    • breakdance89
      New Member
      • Jan 2013
      • 14

      #3
      Basically my program generates rota from access database. But I want my dates to be added automatically. Which that what the code does. To see create a new form and add msflexgrid control on it set cols to 8 and paste the code in form initialize event. Also need to declare tdate as date. My problem is I want to create rota for 2 weeks either using one msflexgrid control or 2. What happens now is when dates are looped on the grid it changes as soon as the week over automatically. I need to automatically change the dates only after 2 weeks are lapsed. If system date is 1st of Feb I want it to loop for whole14 days and only change to next 14 days on the 15th of feb. I hope I make sense. Thanks for support

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        I see nothing in your code that resets the values after each week. I only see code that sets values based on the current date. There's nothing in there to rerun the code when a week is over.

        Comment

        • breakdance89
          New Member
          • Jan 2013
          • 14

          #5
          I tried to reset the values putting in an if statement in the loop to check if it's a new week but it is not working. How would you reset the counter?

          Comment

          • breakdance89
            New Member
            • Jan 2013
            • 14

            #6
            The problem is I don't know What value could be used to determine that the week is over.

            Comment

            • Rabbit
              Recognized Expert MVP
              • Jan 2007
              • 12517

              #7
              What you need to do is check if the current date is greater the last date in the grid. If it is, repopulate the grid.

              Comment

              • breakdance89
                New Member
                • Jan 2013
                • 14

                #8
                Code:
                Dim*f*As*Integer
                dim*tDate*as*Date
                tDate*=*now
                ****For*f*=*1*To*7
                *******tDate*=*Format(tDate,*"d/m/yy")
                *******grdRota.TextMatrix(1,*f)*=*DateAdd("d",*f*-*1,*tDate)
                ***Next*f
                tdate = now
                Dim dtcheck as date
                dtCheck = grdrota.textmatrix(1,7)
                If tdate>dtcheck then
                      tdate=now -7
                ****For*f*=*1*To*7
                *******tDate*=*Format(tDate,*"d/m/yy")
                *******grdRota.TextMatrix(1,*f)*=*DateAdd("d",*f*-*1,*tDate)
                *
                ***Next*f
                Something like that?

                Comment

                • breakdance89
                  New Member
                  • Jan 2013
                  • 14

                  #9
                  Not sure why all these stars come from, sorry about that.

                  Comment

                  • Rabbit
                    Recognized Expert MVP
                    • Jan 2007
                    • 12517

                    #10
                    Not a problem, it happens sometimes when you copy and paste on this forum.

                    That looks almost right to me. I just don't understand why you're running the population code twice on the same row.

                    Comment

                    • breakdance89
                      New Member
                      • Jan 2013
                      • 14

                      #11
                      Because I am repopulating the dates on the rota. So now hopefully it will do a check and if it returns true then it should repopulate and the dates should be staying the same. Then i can do another check when current date is greater than it is on the second Row like day 14. Then loop it through to add dates starting from 15th. What do you exactly mean by running it twice on the same row?

                      Comment

                      • Rabbit
                        Recognized Expert MVP
                        • Jan 2007
                        • 12517

                        #12
                        You populate it on grdRota.TextMat rix(1,f) before the check. Then you populate it again in the same place after the check.

                        Comment

                        • breakdance89
                          New Member
                          • Jan 2013
                          • 14

                          #13
                          I assume if I don't repopulate it then the dates will change automatically but I don't want that to happen, I want it to stay the same for 14 days but as soon as 8 days lapses then it assumes its a new week and starts to loop it again

                          Comment

                          • Rabbit
                            Recognized Expert MVP
                            • Jan 2007
                            • 12517

                            #14
                            I know, that's what the check is for.

                            But it still doesn't make sense why you would populate anything before the check and why when you populate it after the check, you replace the original population.

                            Comment

                            • breakdance89
                              New Member
                              • Jan 2013
                              • 14

                              #15
                              Because I would not have any values to do a check from. If I do a check from msflexgrid.text matrix(1,7) before the loop I would have nothing in that row to do a check from.

                              Comment

                              Working...