C0D3 MA5T3R Homepage

Alarm Clock

Alarm clock function to check actual time against alarm time

(See Below Code)

'Call this function to check whether the values for the hours, minutes and seconds are met
Public Function Alarm(hours As Variant, minutes As Variant, seconds As Variant)

'Set Alarm to 1 to start with
Alarm = 1

'Check if hours is null - if not, check whether it matches the actual time
If hours <> "" And Hour(Time) <> hours Then Alarm = 0 'If it doesn't then set Alarm to 0 - the alarm should not sound
'Check if minutes is null - if not, check whether it matches the actual time
If minutes <> "" And Minute(Time) <> minutes Then Alarm = 0 'If it doesn't then set Alarm to 0 - the alarm should not sound
'Check if seconds is null - if not, check whether it matches the actual time
If seconds <> "" And Second(Time) <> seconds Then Alarm = 0 'If it doesn't then set Alarm to 0 - the alarm should not sound

'Finished Checking
End Function

'Example of Calling Function
'Call in timer to keep checking to see if alarm should go off
Private Sub Timer1_Timer()

'Error Trap
On Error GoTo errtrap

'Set the values that will be checked
'(Integer them to block errors and convert string values to numbers)

hours = Int(Text1.Text)
minutes = Int(Text2.Text)
seconds = Int(Text3.Text)

'Call Function with the parameters as the values to check for
Value = Alarm(hours, minutes, seconds)

'If a Value of 1 is return then the requirements are met
If Value = 1 Then MsgBox "Alarm!"

GoTo Done 'Finished

errtrap:

'Trap checks to see that all the testing values (hours, minutes and seconds) are not null or strings
'i.e. at least 1 value must be testing for
erred = erred + 1

'If erred=3 then all values are null, so do not check for alarm
If erred = 3 Then GoTo Done

'Otherwise, resume at the next line
Resume Next

Done:
'Finished
End Sub

(Copy and Paste this code into your Visual Basic project)

*Want to join the C0D3 MA5T3R e-mail list?*

Get all the latest code news and articles, and even submit your own.

You can sign-up for free below (you may opt-out at any time):

Join the mailing list
Enter your name and email address:
Name:
Email:  
Subscribe      Unsubscribe