Content
Resources
To Do
Toolbox

 GetWinCalcVal2

Through a google search, any number of calculators built for use in Access may be found. However, the majority of these do not include keypad functionality and/or scientific calculation as the standard windows calculator does. The following module and function GetWinCalcVal2() returns the value of the current number in the windows calculator, or Null if the calculator is not open. The windows calculator can be started by shelling "calc.exe". Note that this does not work with the Windows 7 calculator.

CODE

' GetWinCalcVal2
' http://www.utteraccess.com/wiki/index.php/GetwinCalcVal2
' Code courtesy of UtterAccess Wiki
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-11-09

Option Compare Database
Option Explicit

Private Declare Function FindWindow _
Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String _
) As Long

Private Declare Function FindWindowEx _
Lib "user32" Alias "FindWindowExA" _
(ByVal hWnd1 As Long, _
ByVal hWnd2 As Long, _
ByVal lpsz1 As String, _
ByVal lpsz2 As String _
) As Long

Private Declare Function SendMessage _
Lib "user32" Alias "SendMessageA" _
(ByVal hWnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Integer, _
ByVal lParam As Any _
) As Long

Public Function GetWinCalcVal2() As Variant
Dim s As String
Dim hWnd As Long
Dim Res As Long
Dim Ret As Variant

hWnd = FindWindow("SciCalc", "Calculator")

If hWnd = 0 Then

MsgBox "Calc is not open!"
Ret = Null

Else

hWnd = FindWindowEx(hWnd, 0, "Edit", vbNullString)
s = Space(260)
Res = SendMessage(hWnd, &HD, 260, s)
Ret = CDbl(s)

End If

GetWinCalcVal2 = Ret

End Function