Solved

How to: Set Focus on Message Box: vbOK

Posted on 2010-11-20
9
729 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
Comment Utility
Can you post the code you are using?

Cheers
JC
0
 
LVL 77

Expert Comment

by:peter57r
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:DatabaseMX (Joe Anderson - Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

772 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

11 Experts available now in Live!

Get 1:1 Help Now