Solved

How to: Set Focus on Message Box: vbOK

Posted on 2010-11-20
9
744 Views
Last Modified: 2012-05-10
Hello ~  I would like to write a command to set the focus on a message box.  Because it opens prior to a report printing to PDF, it doesn't receive the focus while the PDF printing is happening.  How can I set the focus to the message box?  I have tried MsgBox.SetFocus, which does not work.

Thank you, Jacob
0
Comment
Question by:Chi Is Current
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 6

Expert Comment

by:JVWC
ID: 34178949
Can you post the code you are using?

Cheers
JC
0
 
LVL 77

Expert Comment

by:peter57r
ID: 34179199
Definitely need to see the code.

Once you issue a msgbox command no further code should run until the msgbox has been clicked.
0
 
LVL 84
ID: 34179307
Note that you can define the default button when you build the Messagebox:

MsgBox "This is a message", vbYesNoCancel + vbDefaultButton2

Would set the "focus" on the No button.

However, as others have said - you cannot transfer focus to a Msgbox after it's opened. It would be better to open the MsgBox only when you need to show it; anything that runs AFTER that may affect the msgbox focus.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 34180228
Thank you all for your replies.

The purpose of the message box (MsgBox "The Gross Invoice isn't here just yet."....) is to allow time for the PDF report to be created.  Without it, the email message is created without the PDF attachment.

However, the message box looses focus to the PDF creation operation, so it's not possible to just press 'Enter' (default) to move through the message, which would be easiest for the client.

Alternatively, I have tried:

    On Error Resume Next
    DoCmd.OpenReport stDocName, acNormal
    While Dir("C:\Invoice.pdf") = ""     ' Way to PAUSE Access until report is printed and ready to attach.
    Name "C:\Invoice.pdf" As "C:\Invoice.pdf"
    Wend

Which often results in the email being created without the attachment - which is why I changed to the the message box.

The code is below.

Jacob
Private Sub Command1037_Click()

On Error GoTo Err_Command1037_Click

    

    Dim stDocName As String

    Dim Cancel As Integer

    Dim msg As String

 

    stDocName = "rptInvoice_GrossPDF"

     

  'Refresh forms

    Forms!frmOrders.Recalc

    msg = MsgBox("Be Certain to save this PDF file in C:\ directory.", vbExclamation, "Save Carefully!!")



'Delete the old PDF

    On Error Resume Next

    Kill "C:\Invoice.pdf"

        

    '<<<<<<<

    If Dir("C:\Invoice.pdf") <> "" Then

    Else

    MsgBox "The Gross Invoice isn't here just yet." & vbCrLf & vbCrLf & "It will be by the time you click 'OK'.", 64, "OK"

    End If



'Does not always work

    'On Error Resume Next

    'DoCmd.OpenReport stDocName, acNormal

    'While Dir("C:\Invoice.pdf") = ""     ' Way to PAUSE Access until report is printed and ready to attach.

    'Name "C:\Invoice.pdf" As "C:\Invoice.pdf"

    'Wend



    

'  Create the email message & attach

    

    Dim strRecipient As String

    Dim strSubject As String

    Dim strAttachments As String

    Dim objMailItem As Outlook.MailItem

    Dim objOutlookAttach As Outlook.Attachment    '<<<<<< new

    Dim olkApp As Outlook.Application

    Dim olkNameSpace As NameSpace

    

    

    Set olkApp = New Outlook.Application

    

    Set olkNameSpace = olkApp.GetNamespace("MAPI")

    Set objMailItem = olkApp.CreateItem(olMailItem)

    



With objMailItem

        .To = IIf(IsNull(Forms!frmOrders!custAltEmail) = True, Forms!frmOrders!contEmail, Forms!frmOrders!custAltEmail)

        .Recipients.ResolveAll

        .Subject = "INVOICE: Your B&F Invoice # " & Forms!frmOrders!fordID & "  (Ref. Your PO #  " & Forms!frmOrders!ordCustPO & ")"

        Set objOutlookAttach = .Attachments.Add("C:\Invoice.pdf")

                

        

        .HTMLBody = "Email message stuff here.</body>"

        

        .Display

  

    End With

    

    Set objMailItem = Nothing

    Set olkNameSpace = Nothing

    Set olkApp = Nothing

    Set objOutlookAttach = Nothing

 

Exit_Command1037_Click:

    Exit Sub



Err_Command1037_Click:

    MsgBox Err.Description

    Resume Exit_Command1037_Click

End Sub

Open in new window

0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 75
ID: 34180952
Jacob.  I'm confused.  Once a MessageBox opens, it now has the Focuse.  And until one of the buttons is clicked, it retains the focus, and nothing else is going to happen or run.  The MessageBox is application Modal by default (can be System modal as option).

So RE: "it doesn't receive the focus while the PDF printing is happening."
How would the printing even start until the message box is closed?  And once it is closed, then it's not going to get the focus again.

And regarding "you cannot transfer focus to a Msgbox after it's opened. " ... Again, when the MsgBox is open ... it *has* the focus.

mx
0
 
LVL 6

Expert Comment

by:JVWC
ID: 34181438
At which point are you generating the PDF?
There seems to be some mising logic between Line 15 & 22...

Cheers
JC
0
 
LVL 84

Accepted Solution

by:
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 500 total points
ID: 34182801
If you must insure that a PDF file is created BEFORE you generate your email, then just check for the existence of that file in a loop:

Dim dtStart As Date
Dim bOK As Boolean

dtStart = Now
'/loop for 10 seconds, or until the loop finds the file
Do
  bOK = (Dir("The path to your PDF File") <> "")
  If bOK Then Exit Do
Loop Until DateAdd("s", 10, dtStart) < Now

If bOK then
  '/create your email
Else
  Msgbox "Couldn't find the PDF file"
End If

Open in new window

You'd do this somewhere around your Line 19 in the code above.
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 34184221
JC: Yes, I did leave out some code, the line that prints the report to a PDF file, beginning the process (ahem):

Line 17:  DoCmd.OpenReport stDocName, acNormal

A critical omission, for sure.

LSM:
>If you must insure that a PDF file is created BEFORE you generate your email

Your solution works as an excellent way to be certain the PDF file is there to attach prior to creating the email message; eliminating the necessity for the MsgBox to create the pause.  This works beautifully.   Extra points for mind reading.

Thank you!  Jacob
0
 
LVL 2

Author Comment

by:Chi Is Current
ID: 34184580
Well.....  The "Invoice.pdf" file still does not always attach to the email message!?  #!@%?$*&!@+%

I have opened a new question here, displaying the revised code containing LSM's suggestion....

http://www.experts-exchange.com/Microsoft/Development/MS_Access/Q_26630154.html

Any additional thoughts would be appreciated.

Best Regards, Jacob
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

863 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

26 Experts available now in Live!

Get 1:1 Help Now