By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,469 Members | 2,402 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,469 IT Pros & Developers. It's quick & easy.

Ticking checkboxes using to and from textboxes...?

P: 50
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
May 7 '08 #1
Share this Question
Share on Google+
6 Replies


Expert Mod 2.5K+
P: 2,545
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:
Expand|Select|Wrap|Line Numbers
  1. dim i as integer
  2. const namepart = "check" ' replace this if the controls are named differently
  3. for i = 1 to 24
  4.     me.Controls(namepart & format(i, "00")) = False
  5. next i
  6. me.Controls(namepart & format(me![from], "hh")) = True
  7. me.Controls(namepart & format(me![to], "hh")) = True
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

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
May 7 '08 #2

Expert Mod 2.5K+
P: 2,545
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
May 7 '08 #3

NeoPa
Expert Mod 15k+
P: 31,186
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.
Expand|Select|Wrap|Line Numbers
  1. Private Const conStub As String = "chk"  'consistent part of name
  2.  
  3. Private Sub txtFrom_AfterUpdate()
  4.   Call FillCheckBoxes
  5. End Sub
  6.  
  7. Private Sub txtTo_AfterUpdate()
  8.   Call FillCheckBoxes
  9. End Sub
  10.  
  11. Private Sub FillCheckBoxes()
  12.   Dim intX As Integer, intFrom As Integer, intTo As Integer
  13.   Dim ctlThis As Control
  14.  
  15.   With Me
  16.     'X + Y (strings) results in Null if EITHER X or Y is null
  17.     If IsNull(.txtFrom + .txtTo) Then Exit Sub
  18.     For Each ctlThis in .Controls
  19.       If Left(ctlThis.Name, Len(conStub)) = conStub Then ctlThis = False
  20.     Next ctlThis
  21.     'Split() returns an array
  22.     intFrom = Val(Split(.txtFrom, ":")(0))
  23.     'We should handle the operator entering a "To" time of 24:00 or 00:00
  24.     intTo = (Val(Split(.txtTo, ":")(0)) + 23) Mod 24
  25.     'Set ALL Checkboxes between the selected times
  26.     For intX = intFrom To intTo
  27.       .Controls(conStub & Format(intX, "00")) = True
  28.     Next intX
  29.   End With
  30. End Sub
May 8 '08 #4

NeoPa
Expert Mod 15k+
P: 31,186
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.
May 8 '08 #5

Expert Mod 2.5K+
P: 2,545
Hi NeoPa. I am happy to see alternative solutions presented - the more the merrier.
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
May 8 '08 #6

NeoPa
Expert Mod 15k+
P: 31,186
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).
May 8 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.