Link to home
Start Free TrialLog in
Avatar of redrp
redrp

asked on

Close An Instance of Access using Visual Basic

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!
Avatar of zemp1212
zemp1212
Flag of United States of America image

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
 
 
 
 
Avatar of redrp
redrp

ASKER

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...
SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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
Avatar of redrp

ASKER

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?
that's what I mentioned earlier. change "any" to "long".

you will need to import System.Runtime .InteropServices
Avatar of redrp

ASKER

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

Avatar of redrp

ASKER

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...
Avatar of redrp

ASKER

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...
Avatar of redrp

ASKER

Looks like this was my problem with the FindWindow:
https://www.experts-exchange.com/questions/21100827/FindWindow-Not-Working.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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial