Ticking checkboxes using to and from textboxes...?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • OllyJ
    New Member
    • Dec 2007
    • 50

    Ticking checkboxes using to and from textboxes...?

    Hi guys hope you can help me with this one...

    I have a form with 24 tick boxes, 1 for each hour of the day.

    I want to check the boxes if a certain product is being made within one of those hours. I have created to and from textboxs formatted as 'short time' and would like a command button to be pressed once the times are filled in and auto tick the correct boxes....ANY IDEAS?

    OllyJ
  • Stewart Ross
    Recognized Expert Moderator Specialist
    • Feb 2008
    • 2545

    #2
    Hi Olly. Assuming you have named your checkboxes sequentially along the lines of

    check01, check02, check03 ... check24

    it would be possible to clear all of the boxes then tick the hours to and from via the on-click event of your command button like this:
    [code=vb]dim i as integer
    const namepart = "check" ' replace this if the controls are named differently
    for i = 1 to 24
    me.Controls(nam epart & format(i, "00")) = False
    next i
    me.Controls(nam epart & format(me![from], "hh")) = True
    me.Controls(nam epart & format(me![to], "hh")) = True[/code]
    If somebody enters 11:55, say, it may be better to round it up to 12 - but I'll leave that as an exercise for you at the moment. You would need to take account of the wrapping from one 24 hour period to the next.

    -Stewart

    Originally posted by OllyJ
    Hi guys hope you can help me with this one...

    I have a form with 24 tick boxes, 1 for each hour of the day.

    I want to check the boxes if a certain product is being made within one of those hours. I have created to and from textboxs formatted as 'short time' and would like a command button to be pressed once the times are filled in and auto tick the correct boxes....ANY IDEAS?

    OllyJ

    Comment

    • Stewart Ross
      Recognized Expert Moderator Specialist
      • Feb 2008
      • 2545

      #3
      Hi again. Ah well, with 24 checkboxes you will be starting from a time of 00 hours, won't you? So the tickboxes will be from 0 to 23, not 1 to 24. The for loop limits have to change accordingly: for i = 0 to 23

      -Stewart

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32656

        #4
        This (these) solutions assume any times entered are always simply hours (00:00; 01:00; 02:00; etc rather than 15:30).

        However, the between hours also need to be handled.

        My code will be different from Stewart's, not because there's anything wrong with that approach, but simply because it's already there and this is an alternative approach.
        [CODE=VB]Private Const conStub As String = "chk" 'consistent part of name

        Private Sub txtFrom_AfterUp date()
        Call FillCheckBoxes
        End Sub

        Private Sub txtTo_AfterUpda te()
        Call FillCheckBoxes
        End Sub

        Private Sub FillCheckBoxes( )
        Dim intX As Integer, intFrom As Integer, intTo As Integer
        Dim ctlThis As Control

        With Me
        'X + Y (strings) results in Null if EITHER X or Y is null
        If IsNull(.txtFrom + .txtTo) Then Exit Sub
        For Each ctlThis in .Controls
        If Left(ctlThis.Na me, Len(conStub)) = conStub Then ctlThis = False
        Next ctlThis
        'Split() returns an array
        intFrom = Val(Split(.txtF rom, ":")(0))
        'We should handle the operator entering a "To" time of 24:00 or 00:00
        intTo = (Val(Split(.txt To, ":")(0)) + 23) Mod 24
        'Set ALL Checkboxes between the selected times
        For intX = intFrom To intTo
        .Controls(conSt ub & Format(intX, "00")) = True
        Next intX
        End With
        End Sub[/CODE]

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32656

          #5
          I suppose I should explain this a little. The two _AfterUpdate procedures handle this routine being triggered simply by entering figures in either of the boxes.

          As the code is the same for either (relying on both), we put the code in a separate procedure and simply call this from both places.

          Comment

          • Stewart Ross
            Recognized Expert Moderator Specialist
            • Feb 2008
            • 2545

            #6
            Hi NeoPa. I am happy to see alternative solutions presented - the more the merrier.
            Originally posted by NeoPa
            This (these) solutions assume any times entered are always simply hours (00:00; 01:00; 02:00; etc rather than 15:30).
            Just to correct a misconception, what I posted in posts 2 and 3 does not assume whole-number hours values; it works as expected for any typical time value (hence my comment about rounding 11:55). After all, it is simply taking the hours component of the value entered and using that value to select the relevant tick-box.

            As I am sure we all do wherever possible, I test before posting and I tested that values at random (10:01, 01:35 and so on) ticked the relevant hours box as required.

            -Stewart
            Last edited by Stewart Ross; May 8 '08, 04:09 PM. Reason: moved quote

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32656

              #7
              Originally posted by NeoPa
              This (these) solutions assume any times entered are always simply hours (00:00; 01:00; 02:00; etc rather than 15:30).
              ...
              You're quite right Stewart. That wasn't what I'd meant to say at all :/

              I was trying to say that (unless otherwise changed as suggested) both sets of code will assume that no minutes element exists (IE they ignore the minutes and treat it as if it were hh:00).

              Comment

              Working...