I would like to know the current idle time from user input on a given Windows XP machine programmatically. I am using VBA in MS Access. What options do I have?
I used the following to obtain the solution.
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Declare Function GetLastInputInfo Lib "user32" (plii As Any) As Long
Private Type LastInputInformation
cbSize As Long
dwTime As Long
End Type
Public Function GetUsersIdleTime() As Long
Dim lii As LastInputInformation
lii.cbSize = Len(lii)
Call GetLastInputInfo(lii)
GetUsersIdleTime = FormatNumber((GetTickCount() - lii.dwTime) / 1000, 2)
End Function
There are other parts of the system which can be idle such as,
CPU
Disk
Network
Other devices
To find out more regarding performance and other idle types see this SO post here.
Related
I have read countless times that you should always release objects at the end of your projects, such as:
Sub Test()
Dim obj As Object
Set obj = GetObject(, "xxxxxx.Application")
' Code your project...
Set obj = Nothing
End Sub
However, I use a program that calls upon Excel's object. I call Excel's object so often in so many different routines, I decided to make a function to make things simpler:
Public Function appXL As Object
Set appXL = GetObject(, "Excel.Application")
End Function
Although that is an extremely simple function, there is no way for me to release the object appXL at the end of any subroutine I use, at least as far as I am aware. But a bigger concern that I have is that a subroutine that uses appXL 100+ times is actually grabbing the object the same number of times.
I honestly don't see why it would be a big issue since I am using GetObject as opposed to CreateObject. I am not creating a new instance of Excel everytime the function is called, so is this something that I should be concerned about when coding? Do I just need to completely get rid of the function and declare appXL on every routine I use so I am able to release it in the end?
int VK_F_DOWN = GetVirtualKey('F', 0);
int VK_F_UP = GetVirtualKey('F', 1);
HWND hWnd = FindWindow(NULL,L"Calculator.exe");
SendMessage(hWnd, WM_KEYDOWN, toascii('F'), VK_F_DOWN);
SendMessage(hWnd, WM_KEYUP, toascii('F'), VK_F_UP);
It doesn't work on the Calculator.exe,however it can work on my visual studio when I change hWnd to HWND(0xFFFF).
And I also already got a non-zero hWnd. What am I missing here? Like authority or something?
Sounds like User Interface Privilege Isolation. As the Wikipedia article explains, you can request the access you want in your application manifest.
That said, you should be using SendInput rather than SendMessage. See one of Raymond Chen's blog entries on that point.
I have read many threads on implementing the Sleep API however most are a step ahead of where I am at in terms of knowledge to some guidance would be greatly appreciated.
I have a macro in VBA which I would like to add pauses to, between steps (purely aesthetic rather than functional) however I am struggling with the basic declaring and calling.
I understand that first I must declare the api and that the code for this is...
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
however, I am confused with regards to where this should go. Should it go in a module of it's own or can it be stated at the top of the macro module?
Then to call the function do I simply add a line to the macro of...
Call Sleep (1000)
I'd appreciate it if anyone can help with this question and appreciate your patience with my basic grasp.
The declaration of Sleep should go to the top of a module. Standard coding module.
You can omit Call and just use
Sleep 1000 ' 1 second delay
anywhere within an existing sub, so
Option Explicit
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Sub Main()
' wait 3 seconds
Sleep 3000
End Sub
an alternative without declaring the Sleep sub would be
Application.Wait Now + TimeValue("00:00:03") ' waits 3 seconds
Hello i have a form written in VBA Access , when i type or select some values from comboboz or text some values, my NUMLOCK And CAPSLOCK key are turning OFF, how to make that they ALWAYS will be ON , i can't figure how?
this has code that will check and set capslock, numlock and scrolllock.
It uses the windows APIs
Private Declare Function GetKeyboardState Lib "user32" _
(pbKeyState As Byte) As Long
Private Declare Function SetKeyboardState Lib "user32" _
(lppbKeyState As Byte) As Long
to check and set the keyboard state - with this API, other fun things are available, such as playing with the windows key.
Would it be possible to see the CallStack in VBA for MS Access 2003? That is to say, would it be possible to see from what procedure or function another function was called?
At runtime, View menu -> Call Stack (or press CTRL + L).
There is no programmatic way in VBA to view the call stack that I know of. The usual solution to this problem is to use some structure to track calling of functions, but it always seems like a kludge to me, and really of use only when programming (not at runtime), in which case it seems to me that the VBE's built-in capability for seeing the call stack is sufficient.
And, BTW, I always put the call stack button on my VBE toolbar, since it's one of the most frequently used functions for me. I also add the compile button -- I think it's crazy that it's not on the toolbar by default because it encourages people to code without ever forcing a compile. Then again, Access 2000 didn't even use Option Explicit by default (supposedly for consistency with the other apps using the VBE -- in other words, dumb down Access in order to make it consistent with apps that aren't nearly as code-heavy).
But I digress...
Eventually, add an optional parameter to your function, and pass the caller name that way. For forms, you can use Me.Name as the parameter.
Yes it's possible, BUT it's not quite usefull!
Private Declare Sub SetMode Lib "vba332.dll" Alias "EbSetMode" (ByVal lngMode As Long)
Private Declare Function GetCallStackCount Lib "vba332.dll" Alias "EbGetCallstackCount" (lngCount As Long) As Long
Private Declare Function GetCallStackFunction Lib "vba332.dll" Alias "EbGetCallstackFunction" (ByVal Lvl As Long, ByRef strBase As String, ByRef strModule As String, ByRef strFunction As String, ByRef Done As Long) As Long
Before use GetCallStackCount and GetCallStackFunction call SetMode(2), and after SetMode(1).