Option Explicit
Dim AlarmTime
Const conMinimized = 1
Private Sub Form_Click()
AlarmTime = InputBox("Escriba la hora de alarma", "Alarma de VB", AlarmTime)
If AlarmTime = "" Then Exit Sub
If Not IsDate(AlarmTime) Then
MsgBox "La hora no es válida."
Else ' La cadena devuelta por InputBox es una hora válida,
AlarmTime = CDate(AlarmTime) ' de modo que se almacena en AlarmTime como fecha/hora.
End If
End Sub
Private Sub Form_Load()
AlarmTime = ""
End Sub
Private Sub Form_Resize()
If WindowState = conMinimized Then ' Si el formulario se minimiza, muestra la hora en un título.
SetCaptionTime
Else
Caption = "Reloj con alarma"
End If
End Sub
Private Sub SetCaptionTime()
Caption = Format(Time, "Medium Time") ' Presenta la hora con el formato Medium Time.
End Sub
Private Sub Timer1_Timer()
Static AlarmSounded As Integer
If lblTime.Caption <> CStr(Time) Then
' Ahora el número de segundo es diferente del mostrado.
If Time >= AlarmTime And Not AlarmSounded Then
Beep
MsgBox "Alarma a las " & Time, , "Alarma"
AlarmSounded = True
ElseIf Time < AlarmTime Then
AlarmSounded = False
End If
If WindowState = conMinimized Then
' Si está minimizado, actualiza el título del formulario cada minuto.
If Minute(CDate(Caption)) <> Minute(Time) Then SetCaptionTime
Else
' Si no, actualiza la etiqueta del formulario cada segundo.
lblTime.Caption = Time
End If
End If
End Sub