[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 461
  • Last Modified:

Intercept a windows msgbox

Hi,
 I'm building a simple app that uses a shell statement to compact and repair an access database.  I want this app to run in the middle of the night.  Everything works with the exception that when access is done repairing the database it raises a msgbox stating that it is finished repairing the database.  You have to click the ok button on the msgbox in order for access to begin compacting the database.  I'm not familiar with using handles to get hold of the window but I was thinking this might be the way.  Is this correct?  Any help on sending a message to that window, (say, send the enter key to the msgbox) would be very helpful.  Please include code in response.  The repair time is variable and kind of time intensive so I have to check for that message say every thirty second or even sixty seconds.  Thanks for any help.
0
Dreamn3d
Asked:
Dreamn3d
  • 7
  • 4
  • 4
  • +2
1 Solution
 
BobbyOwensCommented:
Why are you using access to compact and repair the database?

I would have thought the best way would be to use DAO to connect to the database and then use the CompactDatabase method.

The syntax is CompactDatabase(SrcName As String, DstName As String, [DstLocale], [Options], [SrcLocale])

e.g.

CompactDatabase "C:\TEMP\SOURCE.MDB", "C:\TEMP\DEST.MDB"

Then copy the new file over the old one.

CopyFile "C:\TEMP\DEST.MDB" , "C:\TEMP\SOURCE.MDB"

I would have thought this was quicker anyway due to not loading the whole of access. Also, you can check for errors in case the operation fails
0
 
ofirgCommented:
Try running your shell with hide window style.

retval = shell(myapp,vbHide)

Joy and Happiness
0
 
BobbyOwensCommented:
I just tried the vbHide option, but the message box still appeared!!!!
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
If you develop your app as ActiveX Exe, you have option of unattended execution option, which will "hit" the default button on any messagebox that would have been issued.

Hope this helps
0
 
orbitaltechCommented:
How about the handle method you are thinking of. All windows MsgBox windows have the handle of #32770 and a window title. Lets work with this

Dim lHandle as Long

Do:
   DoEvents
   lHandle = FindWindow("#32770", "The title of your msg box")
   If lHandle <> 0 then
      Button = FindWindowEx(lHandle, 0, "Button", vbNullString)
      Call PostMessage(Button, WM_LBUTTONDOWN, 0, 0)
      Call PostMessage(Button, WM_LBUTTONUP, 0, 0)
   End If
Loop While lHandle = 0


This is kindof generic but does work.. give it a try and see
0
 
orbitaltechCommented:
lol, lets try this again... My previosu code would throw you into an endless loop... Use this code instead

Do:
   DoEvents
   lHandle = FindWindow("#32770", "The title of your msg box")
Loop While lHandle = 0


Button = FindWindowEx(lHandle, 0, "Button", vbNullString)
Call PostMessage(Button, WM_LBUTTONDOWN, 0, 0)
Call PostMessage(Button, WM_LBUTTONUP, 0, 0)


0
 
Dreamn3dAuthor Commented:
Sorry everyone, just got home.  I'll try everyone's suggestions in turn and give points to the winner, as it were...:)

Bobby Owens,
  Thanks for the response.  The answer to your question as that I've only been using VB for about two years and while I've gotten fairly familiar with VB and development I haven't used DAO or RDO.  I've never needed to do anything where I needed anything other that ADO.  I have heard of compacting and repairing a database via DAO but have never done it myself.

ofirg,
 I have tried that method (using the vbhide) but that doesn't hide any interaction generated by Access.  Any messages or errors reported are posted and I need a way to intercept them so the user doesn't croak first thing in the morning and call and wake me up...:)
Cush jobs, ya gotta love'em.

angelIII,
 Unfortunately I haven't worked with ActiveX very much and at this point in the app i'm reluctant to scratch everything i've done and rewrite the code into something that will work as an activex exe.

orbitaltech ,
  So far you're code looks like it will work.  I'm not that familiar with this particular API (apparently) and just had a few questions.  I took your code and modified into a timer that checks to see if that window has popped up, say every second (during testing) but i'm getting an error and I wanted to see if you knew what to do about it.  This is the error:
Compile error:
Variable not defined.

This is the code it was erroring out on.
Call PostMessage(Button, WM_LBUTTONDOWN, 0, 0)

Ok, This is what i did to  enable your code in my app.  I declared the neccesary API functions in my bas file.  I declared the Button as a variant (for testing) and I declared the lHandle as a long, both in the declarations section of the form. I put your code in a timer_timer event and set it to go off every second. Then for the command button I enabled the timer.  While steeping through the code i saw that your code had actually grabbed the hadle for that particular window.  So it works, except for the postr message part.  Could I use a sendkeys statement instead of the post message? What else should I be doing that maybe i'm not?  If you want I can paste my code but right now there's a lot of things that i've tried and haven't removed yet...:)  Nothing like testing...:)

Let me know,
Jess


Thanks for the help everyone, but I think I'm going to stick with orbitaltech.  His answer fits in better with my app then any previous answers.  Thanks again! :)
0
 
Dreamn3dAuthor Commented:
Bobby Owens,
  I tried your way and it was pretty straight forward...:)  I'm still going to go with the window handle idea as I've already got quite a bit of ADO and ADOX code and I don't want to have to go and put the libraries in front of everything, but I appreciate your answer just the same...Gives me a few more options in the future..  I do appreciate it..
Thanks,
Jess
0
 
Dreamn3dAuthor Commented:
BobbyOwens,
  Just out of curiosity...When you use the CompactDatabase method, does it repair the database as well as compact it? Or does it just do the compact and that's it?

Thanks again,
Jess
0
 
BobbyOwensCommented:
From what I understand, DAO 3.51 and DAO 3.60 automatically repair and compact the database at the same time. When you compact, the database is compacted into a new file and he structure is verified also.

With DAO 2.0, from what I remember, you had to repair a database and then do a separate compact on it!!!!
0
 
BobbyOwensCommented:
I've just checked MSDN, and it says that in Jet v4.0, the repair utility is performed by the compactdatabase method.
0
 
orbitaltechCommented:
You need to have the WM_LButtonDown and WM_LButtonUP defined as constants in a .bas file. here they are:

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

Public Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long

Public Const WM_LBUTTONDOWN = &H201
Public Const WM_LBUTTONUP = &H202

These are found using the API Viewer supplied with VB or (if you are advanterous), by opening the equivilent C++ header file and perform a C++ to Vb conversion.. =o)
0
 
orbitaltechCommented:
P.S.

You might also try using Sendmessage instead of Postmessage to the WM_LBUTTONDOWN / UP commands (weird how that works)

Also, sendkeys will work as well, but if , for some reason, the MsgBox is not the topmost window, then the current topmost window will receive an <ENTER>.

 

0
 
Dreamn3dAuthor Commented:
Hey orbitaltech,
   Sorry about not checking in lately, I'm having a lot of fun with a WAN at work lately, nothing is ever simple.  Anyway, your answer is definetely what I was looking for so points are being awarded to you.  Thanks for the info.

Jesse
0
 
Dreamn3dAuthor Commented:
Adjusted points from 100 to 125
0
 
Dreamn3dAuthor Commented:
Comment accepted as answer
0
 
Dreamn3dAuthor Commented:
Great answer, my app works great, intercepts the Access msgbox and closes it.  Not too hard once you get the code :)  kudos to orbitaltech :)  Thanks again
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.

  • 7
  • 4
  • 4
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now