determine if a program is running (exe)

Here is an overview:

I use vb with access.  the program monitors certain directories for new textfiles.  when the file arrives it imports it into access.  If I have access running it will usually give me an error message.

I need code to detect if Access is running.

I would prefer code that will determine if it is running  and shut it down if it is running and continue with the process.

Thanks
thienAsked:
Who is Participating?
 
DalinConnect With a Mentor Commented:
Thien,
In the form's declaration area, add:

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


When you need to check, add:
Dim lHandle as long
lHandle = FindWindow(0&, "Microsoft Access")
If lHandle<> 0 Then
   ' Access is running
   'Do something?
   End If


Regards
Dalin
0
 
mcixCommented:
I have code, we would be happy to e-mail...
0
 
DalinCommented:
Sorry, I did not realize you wanted to kill Access idf you see it.

Use the following code:

'In the form's general Declaration area, add:

Private Declare Function EnumWindows Lib "user32" (ByVal wndenmprc As Long, ByVal lParam As Long) As Long
Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Const WM_CLOSE = &H10

Dim Target As String

'Add a New function

Function EnumCallback(ByVal app_hWnd As Long, ByVal param As Long) As Long
Dim buf As String * 256
Dim title As String
Dim length As Long

    ' Get the window's title.
    length = GetWindowText(app_hWnd, buf, Len(buf))
    title = Left$(buf, length)

    ' See if this is the target window.
    If InStr(title, Target) <> 0 Then
        ' Kill the window.
        SendMessage app_hWnd, WM_CLOSE, 0, 0
    End If
   
    ' Continue searching.
    EnumCallback = 1
End Function

'When you want to check, add
    Target = "Microsoft Access"
    EnumWindows AddressOf EnumCallback, 0

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
thienAuthor Commented:
Dalin,

I posted the first part of the code, I have access open, but it is not working, what is wrong?

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

Private Sub Form_Load()
Private Sub Form_Load()
Dim lHandle As Long
   lHandle = FindWindow(0&, "Microsoft Access")
   If lHandle <> 0 Then
   MsgBox ("open")
   End If
   
End Sub

In this example it it does nothing if open or closed.

Thien
0
 
thienAuthor Commented:
ignore the extra Form_load!!!
Thien

0
 
DalinCommented:
Thien,
Try:

lHandle = FindWindow(vbNullString, "Microsoft Access")

Everything else stay the same.

Let me know if you have any more problems.
Regards
Dalin
0
 
viktornetCommented:
You don't need to use EnumWindows function ans stuff like that....Why making it hard when you can do it the easy way and it is....

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

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

Private Sub Form_Load()
Dim WinHandle as long
  WinHandle = FindWindow(vbNullString, "Microsoft Access")
  If WinHandle<> 0 Then
    SendMessage IHandle, WM_CLOSE, 0, 0
  End If
end sub

Since I'm a Delphi programer I don't know about the vbNullString,but in Delphi we use nil

Regards,
Viktor Ivanov
0
 
viktornetCommented:
You don't need to use EnumWindows function ans stuff like that....Why making it hard when you can do it the easy way and it is....

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

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

Const WM_CLOSE = &H10

Private Sub Form_Load()

Dim WinHandle as long

  WinHandle = FindWindow(vbNullString, "Microsoft Access")
  If WinHandle<> 0 Then
    SendMessage IHandle, WM_CLOSE, 0, 0
  End If

end sub

Since I'm a Delphi programer I don't know about the vbNullString,but in Delphi we use nil

Regards,
Viktor Ivanov
0
 
viktornetCommented:
Opppss a little mistake over there...

SendMessage WinHandle, WM_CLOSE, 0, 0

this is the right way =)

Regards,
Viktor Ivanov
0
 
thienAuthor Commented:
Thank you for your answers!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.