Solved

Intercept a windows msgbox

Posted on 2000-05-15
17
439 Views
Last Modified: 2010-05-18
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
Comment
Question by:Dreamn3d
  • 7
  • 4
  • 4
  • +2
17 Comments
 
LVL 2

Expert Comment

by:BobbyOwens
ID: 2810421
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
 
LVL 1

Expert Comment

by:ofirg
ID: 2810572
Try running your shell with hide window style.

retval = shell(myapp,vbHide)

Joy and Happiness
0
 
LVL 2

Expert Comment

by:BobbyOwens
ID: 2811129
I just tried the vbHide option, but the message box still appeared!!!!
0
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 2811159
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
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2811471
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
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2811479
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
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2812563
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
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2812590
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:Dreamn3d
ID: 2812596
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
 
LVL 2

Expert Comment

by:BobbyOwens
ID: 2813336
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
 
LVL 2

Expert Comment

by:BobbyOwens
ID: 2813352
I've just checked MSDN, and it says that in Jet v4.0, the repair utility is performed by the compactdatabase method.
0
 
LVL 2

Expert Comment

by:orbitaltech
ID: 2814234
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
 
LVL 2

Accepted Solution

by:
orbitaltech earned 125 total points
ID: 2815342
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
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2886123
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
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2886127
Adjusted points from 100 to 125
0
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2886128
Comment accepted as answer
0
 
LVL 1

Author Comment

by:Dreamn3d
ID: 2886129
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now