MsgBox shown in background in respect to the window application

C

cghersi

Hi all.
I'm developing an Access application with some VBA code.
There's a form with a subform. This subform shows a list of items.
There's a button to delete one item of the list. This button is linked
to a VBA subroutine which asks for a double-confirmation of the
operation via two MsgBox (I know it's not a good practice, but the
committent specially asked this behaviour...)
The code is the following:

Sub btnErase_Click()
If MsgBox("Are you sure?", vbOKCancel + vbDefaultButton2) = vbCancel
then Exit Sub
If MsgBox("Are you really sure?", vbOKCancel + vbDefaultButton2) =
vbCancel then Exit Sub
....
End Sub

Clicking of the btnErase, the result is that the MsgBox appears
"below" the active window of Access application (that is, the MsgBox
is shown in background and not in foreground as usual).
To see this MsgBox, I have to click on the desktop and then return to
the Access application: in this way the MsgBox is shown in foreground.

Obviously this is not acceptable (when the problem arose the first
time, I passed 30 minutes searching for this hidden MsgBox!!!).

How may I fix this issue? Is there a method to be sure that the MsgBox
is shown in foregroud?

Thank you very much!
Bye
Cristiano
 
M

Mike B

Yes. Use the Windows API MessageBox function, which is the father of the
msgbox used in VBA. The MessageBox Function takes a window handle as
argument and will be modal to that window. It can also be made systemModal,
which will put it on top of ALL windows.

The VB or VBA declaration:

Private Declare Function MessageBox _
Lib "User32" Alias "MessageBoxA" _
(ByVal hWnd As Long, _
ByVal lpText As String, _
ByVal lpCaption As String, _
ByVal wType As Long) _
As Long

The MSDN details:
http://msdn.microsoft.com/en-us/library/ms645505(VS.85).aspx

Couple of Links showing Useage:

http://www.tek-tips.com/faqs.cfm?fid=4699

http://www.devx.com/vb2themax/Tip/18324
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top