想从某个时间点开始,每间隔2分钟,执行一次命令,写了代码不知为何到点不执行,请大家帮忙分析原因!谢谢!!!
我的代码:从8:00:00开始每隔2分钟Text1.Text显示OK
Dim ks As Date, XZ As Date
Private Sub Timer1_Timer()
Dim k
  ks = "8:00:00"
  XZ = Time
k = 2 * (Fix((60 * (Format(CDate(XZ - ks), "h")) + Format(CDate(XZ - ks), "n")) / 2) + 1)
If Time = DateAdd("N", k, ks) Then
Text1.Text = "OK"
End If
End Sub

解决方案 »

  1.   

    Private Sub Timer1_Timer()
        If CLng(Format(Time, "HH")) > 20 Then
            lngTimeCount = lngTimeCount + 1
            If lngTimeCount = 2 Then
                Text1.Text = "OK"
                lngTimeCount = 0
            End If
        End If
    End SubTimer1的Interval设置成1分钟一次即可
      

  2.   

    lngTimeCount 设置成全局变量
      

  3.   

    Option Explicit
    Private lngTimeCount As LongPrivate Sub Command1_Click()
        Timer1.Interval = 60000
    End SubPrivate Sub Timer1_Timer()
        If CLng(Format(Time, "HH")) > 8 Then
            lngTimeCount = lngTimeCount + 1
            If lngTimeCount = 2 Then
    '            Text1.Text = "OK"
                lngTimeCount = 0
            End If
        End If
    End Sub
      

  4.   

    Dim ks As Long, XZ As LongPrivate Sub Timer1_Timer()
        XZ = Timer
        If ks = 0 Then ks = XZ
        If XZ - ks = 120 Then
            ks = XZ
            Text1.Text = "OK"
        End If
    End Sub'将timer1的Interval设置为小于1000即可
      

  5.   

    我上面的代码没考虑从"8:00:00"开始的情况,稍改了一下:
    Dim ks As Date, XZ As DatePrivate Sub Timer1_Timer()
        If ks = "0:00:00" Then ks = "8:00:00"
        XZ = Time
        If DateDiff("s", ks, XZ) Mod 120 = 0 Then
            ks = XZ
            Text1.Text = "OK"
        End If
    End Sub
    '将timer1的Interval设置为1000即可