Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 825
  • Last Modified:

How to: Set Focus on Message Box: vbOK

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
Chi Is Current
Asked:
Chi Is Current
  • 3
  • 2
  • 2
  • +2
1 Solution
 
JVWCCommented:
Can you post the code you are using?

Cheers
JC
0
 
peter57rCommented:
Definitely need to see the code.

Once you issue a msgbox command no further code should run until the msgbox has been clicked.
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

 
Chi Is CurrentAuthor Commented:
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
 
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform)Commented:
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
 
JVWCCommented:
At which point are you generating the PDF?
There seems to be some mising logic between Line 15 & 22...

Cheers
JC
0
 
Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
 
Chi Is CurrentAuthor Commented:
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
 
Chi Is CurrentAuthor Commented:
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now