I'm trying to write some code that will read the clocktime. When it hits a predetermined time, I want it to run a routine. I know how to do this in It would be done like this:

Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick

        If TimeOfDay = "10:01:00 PM" Then
            'Go do something important
        End If

End Sub

Unfortunately I don't know the equivalent to the same in vbScript (if there is something like it).

I did find a "timer" online that counts the number of seconds from midnight. It looks like this:

Function TimeIt(N)

    Dim StartTime, EndTime
    StartTime = Timer
    For I = 1 To N
    EndTime = Timer
    TimeIt = EndTime - StartTime

End Function

However I'm not sure how to use this. So my questions are:

1) What is the "(N)" representing (better, what is "N")? I see it used in the loop.
2) When using a "Function", how is it called within your primary sub-routine?

If you have a better code suggestion to do what I want to do, I'm all ears!

In reading, I gather that vbScript is similar to VB6. Thus the reason for posting here. If there is a better forum to send this to, please direct me.

In advance, thanks for any assistance.


Recommended Answers

All 4 Replies

Been along time since I looked at VBScript but here's the link to the VBScript User's Guide which should have all the answers you need.


I've begun looking through the guide and it's some help. I've attempted the following code, but it's pausing for several seconds and telling me it's "Not Responding". Then it finishes with the last "i", posts that value and the finishes (I'm testing this in EXCEL). Now I'm wondering why it's stopping and then starting. Any thoughts?

Sub TimeCheck()

    Dim i As Integer
    Dim startTime As String
    Dim curTime As String
    Dim am As String
    Dim hh As String
    Dim mm As String
    Dim ss As String

    startTime = Hour(Now()) & ":" & Minute(Now()) & ":" & Second(Now())

    For i = 1 To 10000
        If Hour(Now()) < 12 Then
            am = "AM"
            hh = "0" & Hour(Now())
            mm = Minute(Now())
            If mm < 10 Then
                mm = "0" & mm
            End If
            ss = Second(Now())
            am = "PM"
            hh = Hour(Now()) - 12
            mm = Minute(Now())
            If mm < 10 Then
                mm = "0" & mm
            End If
            ss = Second(Now())
        End If

        curTime = hh & ":" & mm & ":" & ss & am

        Range("A1").Value = startTime
        Range("A2").Value = curTime
        Range("A4").Value = i

    Next i

End Sub

It might be a threading issue. The loop is tying the thread and doesn't release it until it's done. I'm not sure though, if that is the problem, what the fix would be.

This is a common threading problem in vb6. Here it is in the For Loop. Until completion of the loop it does not reales the thread. If your Loop runs in foreground, it will never permit you to do other works. You can do it by breaking the loop but it could not give you the appropriate result.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.