1

I tried to learn more but sadly I can't understand how it works with the Windows Task Scheduler, so I tried something simple, but it still doesn't work.

I want this function to run every day except the weekends.

Here is my code:

Sub automaticworkdayinsert()

    If Timer4.Interval = 7200000 Then
        Dim time As Date = Date.Now
        Dim currhour As Integer
        Dim currminute As Integer
        Dim ReportHour As Integer
        Dim ReportMinute As Integer
        currhour = time.Hour
        currminute = time.Minute
        ReportHour = 15
        ReportMinute = 10
        If currhour = ReportHour AndAlso currminute = ReportMinute Then
            insertAndcheckWorkday(False)
        End If
    End If
End Sub
J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94
BK201
  • 19
  • 6
  • 2
    The `Timer` will raise a `Tick` event each time the `Interval` expires. The obvious thing to do is to calculate the number of milliseconds until the time you're interested in and assign that to the `Interval`. That calculation is what you need to research. It's easy enough to determine whether the current day is a weekday or not. – jmcilhinney May 19 '20 at 09:15
  • Is there any other method ? I will try what you suggested, but i would like to have a second option. I researched on the C# question, but sadly couldn't understand the logic behind it. – BK201 May 19 '20 at 11:17
  • Why would you need a second option? What do you think is wrong with the first option? – jmcilhinney May 19 '20 at 12:01
  • Well because i thought with library is more faster and better. And i am not sure why but i want the hour to be 03 but vs turn it automatically at 3. I want everyday (except weekends) to insert a workday at 03 AM at the night. So that when the guys come at 7:00 they can start their workday. – BK201 May 19 '20 at 13:57
  • Does your code ultimately involve a database? If so, what database? With Sql Sever you can schedule a job. – Mary May 20 '20 at 03:29
  • Well it's with the question , because i want everyday at 3 AM the sub insertAndcheckWorkday to be executed. – BK201 May 20 '20 at 06:18
  • Yes it involves Access Database (for now) . In the future i will be upgrading it to a MS SQL Server. – BK201 May 20 '20 at 06:19

1 Answers1

0

It's a bit hard to tell by what you included with your code (you should probably also include the code in the insertAndcheckWorkday function), but as far as:

And i am not sure why but i want the hour to be 03 but vs turn it automatically at 3.

Have a look at:

https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-date-and-time-format-strings

In your case, you'd either want to use hh (12 hr format) or HH (24 hr format).

Also, for comparing dates/times there's a handy function built-into the framework already:

https://learn.microsoft.com/en-us/dotnet/api/system.datetime.compare?view=netcore-3.1

Last thing, this doesn't answer your question, but my OCD :p just wants to help ya out a bit:

Sub automaticworkdayinsert()

    If Timer4.Interval = 7200000 Then

        Dim time As Date = Date.Now

        Dim currhour As Integer = time.Hour
        Dim currminute As Integer = time.Minute
        Dim ReportHour As Integer = 15
        Dim ReportMinute As Integer = 10

        If currhour = ReportHour AndAlso currminute = ReportMinute Then insertAndcheckWorkday(False)

    End If

End Sub

The above changes will make the code easier to read in the long run, as well as initialize the vars immediately with a specific value (arguably, since Integers already get initialized as 0 it might be sort of moot, but generally considered good coding practice anyway). Also, if you've only got 1 thing happening after an if you can place it on one line as well. Again, easier for reading down the road.

J. Scott Elblein
  • 4,013
  • 15
  • 58
  • 94