OPENING CALCULATOR FROM ACCESS

  • Thread starter Thread starter Bill Heath
  • Start date Start date
B

Bill Heath

I have used the shell method to open up a calulator from
access vb whenever a button is clicked. Unfortunately,
shell opens up a new instance of calulator each time the
button is clicked. I need to have the calculator
application window opened if calculator is running.
Help and thanks
 
The best solution is using API functions (for details do a
search on the MSDN Library for the specific functions).
There is the function FindWindow that will give you a
Window's "handle" if you supply the caption (i.e. what is
in the title bar). If the handle comes out as zero(0)
then the window is not there - at that point use your
Shell function.

Then there is SetWindowPos which lets you do all sorts of
things with the window, including bringing it to the top
of your on-screen windows.

Here is the header code to include these functions:
Declare Function FindWindow% Lib "user32"
Alias "FindWindowA" _
(ByVal
lpclassname As Any, _
ByVal lpCaption
As Any)

Declare Function SetWindowPos Lib "user32" (ByVal hwnd As
Long, _
ByVal hWndInsertAfter As
Long, _
ByVal X As Long, _
ByVal y As Long, _
ByVal cX As Long, _
ByVal cY As Long, _
ByVal wFlags As Long) As
Long

' Constants for hWndInsertAfter
Global Const HWND_TOPMOST = -1 ' Puts the Window
permanently on top of all others
Global Const HWND_NOTOPMOST = -2 ' Puts the Window below
any 'topmost' windows but above
' all others
Global Const HWND_TOP = 0 ' Puts the Window
(temporarily) on top
Global Const HWND_BOTTOM = 1 ' Sends the Window
underneath all others

' Constants for wFlags:
Global Const SWP_NOSIZE = &H1 ' Do not resize the
Window
Global Const SWP_NOMOVE = &H2 ' Do not move the
Window
Global Const SWP_NOZORDER = &H4 ' Do not change the
Window's Z-order (top to bottom)
Global Const SWP_NOREDRAW = &H8 ' Do not erase the
image of the Window (leaves a "ghost")
Global Const SWP_NOACTIVATE = &H10 ' Do not make the
Window active unless it already was
Global Const SWP_DRAWFRAME = &H20 ' Redraw the Window in
it's new position
Global Const SWP_SHOWWINDOW = &H40 ' Show the Window if
it was hidden
Global Const SWP_HIDEWINDOW = &H80 ' Hide the Window
(make it invisible)
Global Const SWP_NOCOPYBITS = &H100 ' After the Window
moves, do not redraw anything

In FindWindow you should be able to use 0 for the
lpclassname parameter and "Calculator" as lpCaption.

In SetWindowPos you should be able to use 0 for all
parameters except:
hwnd - use the value obtained from FindWindow
hwndInsertAfter - use HWND_TOP

No time here for a further explanation, but I hope that
gives you an idea of how to solve your problem.
 
Dim retVal as Long

Sub Calculator()

If retVal <> 0 Then
AppActivate "Calc"
Else
retVal = Shell("C:\Windows\Calc.exe", vbMaximizedFocus)
End If

End Sub
 
Back
Top