Showing posts with label Visual Basic 6. Show all posts
Showing posts with label Visual Basic 6. Show all posts

Wednesday, August 20, 2008

Visual Basic 6: How to run an event at a given time (Without using the Timer Control)

Without a doubt one of the best tools that Visual Basic 6 brings to the table is the Timer Control, in my opinion. This gives you the ability to make your application perform events at given intervals without using looping delays and burning up resources, and under most circumstances it works wonders. There are some obvious limitations however, such as the Timer Interval maxing out around 64 seconds, so if you want to trigger an event at intervals of say an hour apart, you're left with a challenge.

There are a couple of ways that you can get around this limitation but I've found the following to be the "best" for the most number of situations and circumstances that I commonly encounter. It's relatively straight forward (at least for VB) but explanations are seldom as effective as a simple demonstration so, here you go.

As an example you can create a new .exe project with two buttons, one text box and a standard module. Enter the code below and then compile the project as a standard .exe.

Put the following code in the standard module:

'Start Module Code
Declare Function SetTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long, _
ByVal uElapse As Long, _
ByVal lpTimerFunc As Long) As Long

Declare Function KillTimer Lib "user32" _
(ByVal hwnd As Long, _
ByVal nIDEvent As Long) As Long


‘This is the code that you want the timer to execute at each interval

Public Sub VB_Action(ByVal hwnd As Long, _
ByVal uMsg As Long, _
ByVal idEvent As Long, _
ByVal dwTime As Long)

Form1.Text1.Text = Now

End Sub


Place the following code within Form1.

Dim Timer_ID As Long

Private Sub Command1_Click()
'Starts the timer.
Timer_ID = SetTimer(0, 0, 1000, AddressOf VB_Action)
End Sub

Private Sub Command2_Click()
'stops the timer.
Timer_ID = KillTimer(0, Timer_ID)
End Sub

Now compile the project as a .exe.

NOTE: This example WILL NOT work correctly if you try to run it from within VB6 as an uncompiled program. The Timer will start but the KillTimer function will not stop it correctly and when you try to end the run it's likely that VB6 will crash with an error. If you compile the code to a .exe and run it, it works fine.

This simple example will basically make a VB Clock. When you click Command Button 1 the program will start updating text1 with the date / time at 1 second intervals until you either click command button 2 to stop it, or exit the program.

To use this procedure in a more practical manner you would replace VB_Action with the sub of your choice and would set the interval to whatever time frame you needed. When setting the interval you'll need to convert the interval to milliseconds - I know, it's painful - and you have a maximum interval of 2,147,483,647 milliseconds or approximately 24.8 days.

Another limitation with this method is that for the event to occur the application must remain open and running between intervals. If the application is closed or stops responding at any point then no further action will occur from that point on. I'm sure there's probably a way to cause the application to launch itself with a callback but it's not something I've needed to know how to do at this point so I can't offer any advice on that one.

I don't see any reason why this method wouldn't work for VBA as well, however it's typically easier to the use the Application.OnTime method when you're dealing with VBA. As a bonus, when using the Application.OnTime method, you can close the file between intervals and the system will automatically launch the file at the next interval.

Tuesday, August 19, 2008

Visual Basic 6: How to Simulate a Mouse Click

Sometimes, despite everything else you've tried you just need to simulate a mouse click somewhere. Again, not that difficult but not intuitive either.


'Start Module Code

Public Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dX As Long, ByVal dY As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Public Enum vButtons
vRightClick = 2
vDoubleRight = 4
vLeftClick = 8
vDoubleLeft = 16
End Enum

Public Const LEFTDOWN = &H2, LEFTUP = &H4, MIDDLEDOWN = &H20, MIDDLEUP = &H40, RIGHTDOWN = &H8, RIGHTUP = &H10

Public Sub Mouse_Click(Check_Button As vButtons = vNothing)

Select Case Check_Button
Case vRightClick
mouse_event RIGHTDOWN, 0&, 0&, 0&, 0&
mouse_event RIGHTUP, 0&, 0&, 0&, 0&

Case vDoubleRight
mouse_event RIGHTDOWN, 0&, 0&, 0&, 0&
mouse_event RIGHTUP, 0&, 0&, 0&, 0&
DoEvents
mouse_event RIGHTDOWN, 0&, 0&, 0&, 0&
mouse_event RIGHTUP, 0&, 0&, 0&, 0&

Case vLeftClick
mouse_event LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event LEFTUP, 0&, 0&, 0&, 0&

Case vDoubleLeft
mouse_event LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event LEFTUP, 0&, 0&, 0&, 0&
DoEvents
mouse_event LEFTDOWN, 0&, 0&, 0&, 0&
mouse_event LEFTUP, 0&, 0&, 0&, 0&

End Select
End Sub

'End Module Code


Using the code is simple: just find a spot where you need to simulate a mouse click and call the Mouse_Click() sub with one of the built in options (Right Click, Double Right Click, Left Click, Double Left Click). I typically use this in conjunction with SetCursorPos to move the pointer to a specific location and then call whichever click action I need at the time.

Monday, August 18, 2008

Visual Basic 6: How to create a delay using the Sleep API Call

Note: The following applies to both Visual Basic 6 and VBA.

Sometimes you just need to put your application to sleep for a little while such as when you need to build in a delay while other applications do their thing. There are a couple of ways you can accomplish this but the easiest is to call on the Sleep API.

Enter the following in the General Declarations section of a form or module.

Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

To use: just add the line Sleep X to your code where X is the number of milliseconds of delay that you want to build in. Works like a charm.