Solved

determine if a program is running (exe)

Posted on 1998-08-21
10
196 Views
Last Modified: 2010-04-30
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
0
Comment
Question by:thien
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 3
  • +1
10 Comments
 
LVL 4

Expert Comment

by:mcix
ID: 1430265
I have code, we would be happy to e-mail...
0
 
LVL 9

Accepted Solution

by:
Dalin earned 70 total points
ID: 1430266
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
 
LVL 9

Expert Comment

by:Dalin
ID: 1430267
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

Author Comment

by:thien
ID: 1430268
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
 

Author Comment

by:thien
ID: 1430269
ignore the extra Form_load!!!
Thien

0
 
LVL 9

Expert Comment

by:Dalin
ID: 1430270
Thien,
Try:

lHandle = FindWindow(vbNullString, "Microsoft Access")

Everything else stay the same.

Let me know if you have any more problems.
Regards
Dalin
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1430271
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1430272
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
 
LVL 10

Expert Comment

by:viktornet
ID: 1430273
Opppss a little mistake over there...

SendMessage WinHandle, WM_CLOSE, 0, 0

this is the right way =)

Regards,
Viktor Ivanov
0
 

Author Comment

by:thien
ID: 1430274
Thank you for your answers!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question