我正在基于Access VBA中基于时间隐藏和取消隐藏的代码。
[根据我的测试,似乎跳过了If
,然后继续进行Else
。
我需要Visible1
出现在上午7:30:00到11:59:59 pm,而Visible2
出现在00:00:00 am到07:30:00 am。在任何给定时间,都应显示Visible1
或Visible2
。
Private Sub Form_Current()
Dim Visible1 As Boolean
Dim Visible2 As Boolean
Dim currentTime As String
Dim currentTimestring As String
currentTime = Time()
currentTimestring = Format(currentTime, "hh:mm:tt")
If currentTimestring >= TimeValue("07:30:00") And currentTimestring < TimeValue("00:00:00") Then
Visible2 = True
Visible1 = False
Else
Visible1 = True
Visible2 = False
End If
Me.Label22.Visible = Visible1
Me.Label11.Visible = Visible1
Me.Text10.Visible = Visible1
Me.Label13.Visible = Visible1
Me.Text12.Visible = Visible1
Me.Label23.Visible = Visible2
Me.Label16.Visible = Visible2
Me.Label18.Visible = Visible2
Me.Text15.Visible = Visible2
Me.Text17.Visible = Visible2
End Sub
始终将日期和时间作为Date处理,而不是文本,数字,数字。因此:
Private Sub Form_Current()
Dim Visible1 As Boolean
Dim Visible2 As Boolean
Dim currentTime As Date
currentTime = Time
If currentTime >= TimeSerial(7, 30, 0) Then
Visible1 = True
Visible2 = False
Else
Visible1 = False
Visible2 = True
End If
Me.Label22.Visible = Visible1
Me.Label11.Visible = Visible1
Me.Text10.Visible = Visible1
Me.Label13.Visible = Visible1
Me.Text12.Visible = Visible1
Me.Label23.Visible = Visible2
Me.Label16.Visible = Visible2
Me.Label18.Visible = Visible2
Me.Text15.Visible = Visible2
Me.Text17.Visible = Visible2
End Sub