Results 1 to 6 of 6

Thread: working with time?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2005
    Posts
    31

    Lightbulb working with time?

    Morning,
    IS there any way I can use a timer so that
    1. when it reaches a certain time a message box will appear

    2. between say 10:00 and 4:00 I could disable certain buttons on my form

  2. #2
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: working with time?

    Quote Originally Posted by Grainne
    Morning,
    IS there any way I can use a timer so that
    1. when it reaches a certain time a message box will appear

    2. between say 10:00 and 4:00 I could disable certain buttons on my form
    You may be best not working with timers there icky and it will get confusing. You could use the system clock to determine the time, (assuming the user cant change these settings)

  3. #3
    Fanatic Member doofusboy's Avatar
    Join Date
    Apr 2003
    Posts
    526

    Re: working with time?

    Here's a way to use the system time to do what you want down to the second. Start a project, put a label and a command button on it. The following code will display the current time in the label, disable the command button at the hour, minute and second you specify and then re-enable the command button 5 seconds later.

    In the example shown, I had this stuff occur at exactly 10:43:00 AM. Obviously you can tinker to fit your needs.

    VB Code:
    1. Option Explicit
    2. Dim showTime As Boolean
    3. Dim MyHour As Integer
    4. Dim MyMinute As Integer
    5. Dim MySecond As Integer
    6. Dim CurrHour As Integer
    7. Dim CurrMinute As Integer
    8. Dim CurrSecond As Integer
    9.  
    10. Private Sub DisplayTime()
    11. Do While showTime = False
    12.     Label1.Caption = Time
    13.     DoEvents
    14.     Check_Time
    15. Loop
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.     showTime = False
    21.     Me.Show
    22.     DisplayTime
    23. End Sub
    24.  
    25. Private Sub Form_Unload(Cancel As Integer)
    26.     showTime = True
    27. End Sub
    28.  
    29. Private Sub Check_Time()
    30.     ' Set hour, minute and seconds for alarm
    31.     MyHour = 10
    32.     MyMinute = 43
    33.     MySecond = 0
    34.    
    35.     CurrHour = Hour(Time)
    36.     CurrMinute = Minute(Time)
    37.     CurrSecond = Second(Time)
    38.    
    39.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond) Then
    40.         'MsgBox "Alarm !"
    41.         Command1.Caption = "Stop"
    42.         Command1.Enabled = False
    43.     End If
    44.  
    45.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond + 5) Then
    46.         'MsgBox "Alarm !"
    47.         Command1.Caption = "Start"
    48.         Command1.Enabled = True
    49.     End If
    50.  
    51. End Sub
    Do canibals not eat clowns because they taste funny?

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Posts
    4,787

    Re: working with time?

    Quote Originally Posted by doofusboy
    Here's a way to use the system time to do what you want down to the second. Start a project, put a label and a command button on it. The following code will display the current time in the label, disable the command button at the hour, minute and second you specify and then re-enable the command button 5 seconds later.

    In the example shown, I had this stuff occur at exactly 10:43:00 AM. Obviously you can tinker to fit your needs.

    VB Code:
    1. Option Explicit
    2. Dim showTime As Boolean
    3. Dim MyHour As Integer
    4. Dim MyMinute As Integer
    5. Dim MySecond As Integer
    6. Dim CurrHour As Integer
    7. Dim CurrMinute As Integer
    8. Dim CurrSecond As Integer
    9.  
    10. Private Sub DisplayTime()
    11. Do While showTime = False
    12.     Label1.Caption = Time
    13.     DoEvents
    14.     Check_Time
    15. Loop
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.     showTime = False
    21.     Me.Show
    22.     DisplayTime
    23. End Sub
    24.  
    25. Private Sub Form_Unload(Cancel As Integer)
    26.     showTime = True
    27. End Sub
    28.  
    29. Private Sub Check_Time()
    30.     ' Set hour, minute and seconds for alarm
    31.     MyHour = 10
    32.     MyMinute = 43
    33.     MySecond = 0
    34.    
    35.     CurrHour = Hour(Time)
    36.     CurrMinute = Minute(Time)
    37.     CurrSecond = Second(Time)
    38.    
    39.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond) Then
    40.         'MsgBox "Alarm !"
    41.         Command1.Caption = "Stop"
    42.         Command1.Enabled = False
    43.     End If
    44.  
    45.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond + 5) Then
    46.         'MsgBox "Alarm !"
    47.         Command1.Caption = "Start"
    48.         Command1.Enabled = True
    49.     End If
    50.  
    51. End Sub

    Thtas fine assuming the users canta djust the system clock.... Nice code by the way

  5. #5
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: working with time?

    or you could do a
    VB Code:
    1. Private Sub Timer1_Timer()
    2. text1.text = text1.text -1
    3. End Sub
    4.  
    5. Form_load()
    6. text1.text = 5
    7. If text1.text <= 0 then
    8. text2.text = Split(now)(1)
    9. text1.text = 5
    10. end sub

    And then you could

    If text1.text = 10:39 then 'or whatever
    'blah

  6. #6
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: working with time?

    Quote Originally Posted by doofusboy
    Here's a way to use the system time to do what you want down to the second. Start a project, put a label and a command button on it. The following code will display the current time in the label, disable the command button at the hour, minute and second you specify and then re-enable the command button 5 seconds later.

    In the example shown, I had this stuff occur at exactly 10:43:00 AM. Obviously you can tinker to fit your needs.

    VB Code:
    1. Option Explicit
    2. Dim showTime As Boolean
    3. Dim MyHour As Integer
    4. Dim MyMinute As Integer
    5. Dim MySecond As Integer
    6. Dim CurrHour As Integer
    7. Dim CurrMinute As Integer
    8. Dim CurrSecond As Integer
    9.  
    10. Private Sub DisplayTime()
    11. Do While showTime = False
    12.     Label1.Caption = Time
    13.     DoEvents
    14.     Check_Time
    15. Loop
    16.    
    17. End Sub
    18.  
    19. Private Sub Form_Load()
    20.     showTime = False
    21.     Me.Show
    22.     DisplayTime
    23. End Sub
    24.  
    25. Private Sub Form_Unload(Cancel As Integer)
    26.     showTime = True
    27. End Sub
    28.  
    29. Private Sub Check_Time()
    30.     ' Set hour, minute and seconds for alarm
    31.     MyHour = 10
    32.     MyMinute = 43
    33.     MySecond = 0
    34.    
    35.     CurrHour = Hour(Time)
    36.     CurrMinute = Minute(Time)
    37.     CurrSecond = Second(Time)
    38.    
    39.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond) Then
    40.         'MsgBox "Alarm !"
    41.         Command1.Caption = "Stop"
    42.         Command1.Enabled = False
    43.     End If
    44.  
    45.     If (CurrHour = MyHour) And (CurrMinute = MyMinute) And (CurrSecond = MySecond + 5) Then
    46.         'MsgBox "Alarm !"
    47.         Command1.Caption = "Start"
    48.         Command1.Enabled = True
    49.     End If
    50.  
    51. End Sub

    You might want to use => in case the second doesn't hit exactly at zero.

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