Solved

How to: Set Focus on Message Box: vbOK

Posted on 2010-11-20
9
759 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add records to a form to a table 11 43
Search for text in a .txt file 14 47
Attachment field in SQL 3 29
DSN-LESS connection to MS Access database 6 33
Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

825 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