Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I run a VB program once every 24 hours? 3

Status
Not open for further replies.

SkennyR

Programmer
Mar 7, 2004
157
0
0
US
I posted this earilier, but I thought I would change the subject to maybe make it clearer as to what I want.

I need to run a small VB program every 24 hours, it will check a list of birthdates from a file, then if one of the dates matches the NOW date, this program will alert the user and start the bigger VB program that will allow user to look at the birthdates.
How can I easily do this? I know there has to be an easy way. Im new to this.
I would like to be able to control the times from the program itself.
 
SkennyR, take a look at this thread thread713-356060

"Two strings walk into a bar. The first string says to the bartender: 'Bartender, I'll have a beer. u.5n$x5t?*&4ru!2[sACC~ErJ'. The second string says: 'Pardon my friend, he isn't NULL terminated'."
 
Code:
Option Explicit
Private Declare Function NetScheduleJobAdd Lib "netapi32.dll" (ByVal Servername As String, Buffer As Any, JobID As Long) As Long

Private Type AT_INFO
    JobTime As Long     ' The time is the local time at a computer on which the schedule service is running;
                        ' it is measured from midnight, and is expressed in milliseconds but is only accurate to the minute
    DaysOfMonth As Long ' bit for each day of month
    DaysOfWeek As Byte  ' bit for each day of week
    Flags As Byte       ' See enums
    Command As String   ' Unicode command string you want to run
End Type

Private Enum JobAdd
    JOB_RUN_PERIODICALLY ' If you set this flag, the job runs, and continues to run, on each
                         ' day for which a corresponding bit is set in the DaysOfMonth or
                         ' DaysOfWeek member. The job is not deleted after it executes.
                         ' If this flag is clear, the job runs only once for each bit set in
                         ' these members. The job is deleted after it executes once.
                         
    JOB_ADD_CURRENT_DATE ' If you set this flag, the job executes at the first occurrence of
                         ' JobTime at the computer where the job is queued.
                         ' Setting this flag is equivalent to setting the bit for the current
                         ' day in the DaysOfMonth member.
    JOB_NONINTERACTIVE
End Enum

Private Enum sjWeekdays
    Monday = 1
    Tuesday = 2
    Wednesday = 4
    Thursday = 8
    Friday = 16
    Saturday = 32
    Sunday = 64
End Enum

Private Enum sjDays
    d1 = 1
    d2 = 2
    d3 = 4
    d4 = 8
    d5 = 16
    d6 = 32
    d7 = 64
    d8 = 128
    d9 = 256
    d10 = 512
    d11 = 1024
    d12 = 2048
    d13 = 4096
    d14 = 8192
    d15 = 16384
    d16 = 32768
    d17 = 65536
    d18 = 131072
    d19 = 262144
    d20 = 524288
    d21 = 1048576
    d22 = 2097152
    d23 = 4194304
    d24 = 8388608
    d25 = 16777216
    d26 = 33554432
    d27 = 67108864
    d28 = 134217728
    d29 = 268435456
    d30 = 536870912
    d31 = 1073741824
End Enum

Private Function vbScheduleJob(strCommand As String, sjTime As Date, AddFlags As JobAdd, Optional DayOfWeek As sjWeekdays = 0, Optional DayOfMonth As sjDays = 0, Optional PCName As String = vbNullString) As Long
    Dim myInfo As AT_INFO
    Dim JobID As Long
    
    myInfo.Command = StrConv(strCommand, vbUnicode)
    myInfo.Flags = AddFlags
    myInfo.JobTime = DateDiff("s", "00:00:00", Format(sjTime, "hh:mm:ss")) * 1000 
    myInfo.DaysOfWeek = DayOfWeek
    myInfo.DaysOfMonth = DayOfMonth
    
    NetScheduleJobAdd PCName, myInfo, JobID
    
    
    vbScheduleJob = JobID
    
End Function
 
Thanks strongm..
I copied and pasted the code to vb6, but I cant get it to work.
When I run it a form pops up, and does nothing else. No job is added to task scheduler. I just dont understand.
Where do I plug in the name of the job, path, etc?
Thanks for your patience and your help..
I was aiming to get back with you on it, but Ive been working midnight shift this week and havent had a lot of time to concentrate on it.
 
Um, that's because I've just provided you with a function, not a program. You need to call the function.

e.g:
Code:
vbScheduleJob "notepad", DateAdd("n", 1, Now), JOB_ADD_CURRENT_DATE
Given that I now have a better understanding of your level of knowledge, I'd better warn you that the function only works if you have the right access privileges (basically you need to be administrator) on your NT4/W2K/XP workstation/server.
 
Thanks strongm!
Its working now.
So what you are saying is that if I incorporate this procedure to run within a VB program, and I give the program to someone else, they wont be able to run it, unless they are an administrator on that computer?
Im not planning on running this program on a network, just personal PC's.
Once again thanks!!
A star for your patience..
 
Strongm;
The code you posted works, but only if I use the job example you gave "notepad". If I changed notepad to "myjob" ("myjob" being the name of the program I want to schedule), it wont run.
In the task scheduler, when it is time to run the job, the status message says that the job cannot run.
I have noticed that when I right click on the scheduled job and go to properties, "myjob" has nothing in the "START IN" box and nothing in the "RUN AS" box, but if I change "myjob" to "notepad", the start in box fills with "C:\WINDOWS\system32" and the RUN As box fills with my computer name and my user name.
I have the feeling these boxes need to be filled in before the job will run correctly. Now the big dumb question:
How do I do that?
 
Windows happens to know where notepad is, and so can run it without being explicitly told this information. It doesn't know this for your program, so you have to be explicit and give the full path
 
I tried typing in the complete path where you had "notepad" I put "c:\myjobdir\myjob" and it didnt work.
What am I doing wrong?
Thanks again.
 
Yes Im sure I did. Im at work now, I will try that when I get home in the morn.
 
Well, if it doesn't work I'm afraid I can't think of anything else; it works fine here on XP Pro and W2K
 
Strongm, I tried typing in the entire path and adding .exe and still no show. I know Im doing something wrong, maybe I will get it soon.
"Even a blind Hog finds an acron sometimes."

LordGarsdale: Thanks for the post, I will check it out.
 
Great piece of code, strongm. Thanks!

One modification is required, though, to make it work as expected - you have to define values for the constants in the JobAdd Enum.

Code:
Private Enum JobAdd
    JOB_RUN_PERIODICALLY = 1& 
    JOB_ADD_CURRENT_DATE = 8& 
    JOB_NONINTERACTIVE = 16&
End Enum

Thanks for a very handy bit of code - now I can schedule tasks to run every day anonymously without having to re-password them every time my login changes!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top