?
Solved

Close An Instance of Access using Visual Basic

Posted on 2009-04-22
12
Medium Priority
?
228 Views
Last Modified: 2012-05-06
Hey Experts!
I am creating an updater application that will close a particular instance of an Access Database (.mdb), delete it from the user's desktop, copy the new version to their desktop and then open the new file. I have everything except the first part: closing the particular instance of Access. I'm not really even sure where to start with this and Google wasn't much help. I'm pretty sure that I need to reference the window title (in this case we'll call it "REDRP's Database") , but I have no idea where to go...

Any help is much appreciated!
0
Comment
Question by:redrp
  • 7
  • 4
12 Comments
 
LVL 3

Expert Comment

by:zemp1212
ID: 24208628
try the CloseCurrentDatabase command.
here is a MS document to read. May help you out, or spark a thought process
 
http://support.microsoft.com/kb/317113
 
 
 
 
0
 

Author Comment

by:redrp
ID: 24208841
The only problem with using the CloseCurrentDatabase command is that the instance of Access that I'm closing is not being opened by the VB code, it's being opened by the user...
0
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 24209511
You will most likely need the Windows API for this. Below are the API calls you would need. I believe you would use them as such:

Dim handle As Long

handle = FindWindow(Nothing, "REDRP's Database")
SendMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_CLOSE = 0xF060;
 
Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
 
Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, wParam As Any, lParam As Any) As Long

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24209547
Sorry, copied from C# code :\

The const declarations should be

Constant WM_SYSCOMMAND As Long = 0x0112
Constant SC_CLOSE As Long = 0xF060

And I don't know what the deal with the "Any" is, but I believe those should be long as well.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24209601
Ok, I'm a little ridiculous right now...

the syntax is (I swear this time)

Const WM_SYSCOMMAND As Integer = &H112
Const SC_CLOSE As Integer = &HF060
0
 

Author Comment

by:redrp
ID: 24210189
Thanks for the code!
I tried this and received the following debug error:

'As Any' is not support in 'Declare' statements.

I'm coding in Visual Basic 2008 Express Edition, are there any references I need to include to make this work?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 24214242
that's what I mentioned earlier. change "any" to "long".

you will need to import System.Runtime .InteropServices
0
 

Author Comment

by:redrp
ID: 24218613
Thanks for the tip. I missed the change 'any' to 'long' comment earlier. My appologies.

I've tried everything and it still has no effect on the open Access window. The code compiles and runs with no errors, but Access remains open. I thought maybe it was an Access issue, so I tested it using Notepad and the code still had no effect.  

Here is the code that I have so far (seems to be right?)...

Any and all help is greatly appreciated!
    Const WM_SYSCOMMAND As Integer = &H112
    Const SC_CLOSE As Integer = &HF060
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Declare Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim handle As Long
 
        handle = FindWindow(Nothing, "RP Database")
        SendMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0)

Open in new window

0
 

Author Comment

by:redrp
ID: 24219230
After doing a little research, it appears that the SendMessage() function isn't actually processing the requests using the right handle (almost like the handle doesn't exist/the FindWindow function isn't working as it should). As I debug the code, my variable "handle" receives the value 2415336775154139136...

I'm not sure if this helps us identify the issue...
0
 

Author Comment

by:redrp
ID: 24219298
As I've been debugging this code, I've only ever run it while my "RP Database" window was open. I just tried to run it with that window closed and the "handle" variable was still assigned that same value...
0
 

Author Comment

by:redrp
ID: 24219454
Looks like this was my problem with the FindWindow:
http://www.experts-exchange.com/Programming/Misc/Q_21100827.html

Now it looks like I just need to figure out where the issue is with the SendMessage because my window remained open after running the updated code.
0
 

Accepted Solution

by:
redrp earned 0 total points
ID: 24219588
So I finally got it to work using the following code:
    Const WM_SYSCOMMAND As Integer = &H112
    Const SC_CLOSE As Integer = &HF060
    Private Declare Auto Function FindWindow Lib "user32" (ByVal lpClassName As String, ByVal lpWindowName As String) As IntPtr
    Private Declare Ansi Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As Integer, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As String) As Integer
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 
        Dim handle As Long
        handle = FindWindow(vbNullString, "RP Database")
        SendMessage(handle, WM_SYSCOMMAND, SC_CLOSE, 0)

Open in new window

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

809 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