Solved

How to: Set Focus on Message Box: vbOK

Posted on 2010-11-20
9
806 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 85
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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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
 
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 85

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

627 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