473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Pausing macro execution with if condition

12
I have a form with a macro that has the action "SendObject" that sends a report by email. In the report itself I have macro that displays a messagebox if no records were found.

What I want o achieve is to halt the "SendObject" macro if no records were found and stop loading Outlook, instead display the parameter box for the report once more to let the user input a valid string after the messagebox that warns for No Records

-Sohail
Jun 20 '10 #1
7 3381
ADezii
8,834 Expert 8TB
@lupis
Let the User enter the Criteria for the Report on a Text Box on a Form, then check for the existence of any Records prior to opening the Report itself. Loop until Records matching the Criteria are met, or until the User says No.
Expand|Select|Wrap|Line Numbers
  1. Dim varLastName As Variant
  2. Dim intNumOfRecs As Integer
  3. Dim intResponse As Integer
  4.  
  5. varLastName = Me![txtLastName]
  6.  
  7. If IsNull(varLastName) Then
  8.   MsgBox "You must enter a Last Name", vbExclamation, "No Name Entered"
  9.     Me![txtLastName].SetFocus
  10.       Exit Sub
  11. End If
  12.  
  13. Do
  14.   intNumOfRecs = DCount("*", "Employees", "[LastName] = '" & varLastName & "'")
  15.  
  16.   If intNumOfRecs = 0 Then
  17.     intResponse = MsgBox("No Records exists for a Last name of [" & varLastName & "], try again?", _
  18.                           vbQuestion + vbYesNo + vbDefaultButton1, "Parameter Prompt")
  19.       If intResponse = vbYes Then Me![txtLastName].SetFocus: Exit Sub
  20.   Else    'Record(s) exist, Run Macro or execute code here
  21.     'Now OK, Run Macro or initiate SendObject
  22.   End If
  23. Loop Until intNumOfRecs > 0 Or intResponse = vbNo
P.S. - Instead of a Parameter Prompt, the Criteria for the Report's Row Source would point to a Form Field as in:
Expand|Select|Wrap|Line Numbers
  1. Forms![frmYaDaYaDa]![txtLastName]
Jun 20 '10 #2
lupis
12
Great, I am really thanksfull for your advice. I have just one more question.

Here is the code for the "SendObject":

Expand|Select|Wrap|Line Numbers
  1. 'Record(s) exist, Run Macro or execute code here
  2.  
  3.  
  4.     On Error GoTo cmdSendReport_Click_Err
  5.  
  6.     DoCmd.OpenReport "PurchaseByPerson", acViewReport, "", "", acNormal
  7.  
  8.     DoCmd.SendObject acReport, "PurchaseByPerson", "PDFFormat(*.pdf)", "", "", "", "Dina poster", "Härär dem poster du har köpt av CBC", True, ""
  9.  
When the Send Object fires, and the Outlook form pops up, the minimize property is disabled, so I cant view the report behind, I can maximize the email form or cancel the email form, which then fires a messagebox telling "The Send Object action was cancelled" This denies the usser to see the generated report prior to sending it. Is there any way to have focus on the report first instead of the mailform, or to have the mailform minimize property set to true?

-Thanks
Jun 20 '10 #3
ADezii
8,834 Expert 8TB
@lupis
This is a little tricky, but you can try the following:
Expand|Select|Wrap|Line Numbers
  1. Dim varLastName As Variant
  2. Dim intNumOfRecs As Integer
  3. Dim intResponse As Integer
  4.  
  5. varLastName = Me![txtLastName]
  6.  
  7. If IsNull(varLastName) Then
  8.   MsgBox "You must enter a Last Name", vbExclamation, "No Name Entered"
  9.     Me![txtLastName].SetFocus
  10.       Exit Sub
  11. End If
  12.  
  13. Do
  14.   intNumOfRecs = DCount("*", "Employees", "[LastName] = '" & varLastName & "'")
  15.  
  16.   If intNumOfRecs = 0 Then
  17.     intResponse = MsgBox("No Records exists for a Last name of [" & varLastName & "], try again?", _
  18.                           vbQuestion + vbYesNo + vbDefaultButton1, "Parameter Prompt")
  19.       If intResponse = vbYes Then Me![txtLastName].SetFocus: Exit Sub
  20.   Else    'Record(s) exist, Run Macro or execute code here
  21.     DoCmd.OpenReport "rptTest", acViewPreview
  22.       intResponse = MsgBox("EMail Report?", vbQuestion + vbYesNo, "E-Mail Confirmation")
  23.         If intResponse = vbYes Then
  24.           DoCmd.Close acReport, "PurchaseByPerson"
  25.           DoCmd.SendObject acReport, "PurchaseByPerson", "PDFFormat(*.pdf)", "", "", "", "Dina poster", _
  26.                            "Härär dem poster du har köpt av CBC", True, ""
  27.         Else
  28.           DoCmd.Close acReport, "rptTest"
  29.             Exit Sub
  30.         End If
  31.   End If
  32. Loop Until intNumOfRecs > 0 Or intResponse = vbNo
Jun 20 '10 #4
lupis
12
I tried it out. This is what happens:
  1. On button click
  2. Email confirmation messagebox appears
  3. Report is only vissible beyond the form, not accessable because Email confirmation has focus
  4. On yes, the report dissapears and Outlook form appears
  5. On No, report closes

If the report had focus in front of the form,then it would work, becuase I could move the messagebox around and the confirm to email report or not.

This is the code I am firing for the button event:

Expand|Select|Wrap|Line Numbers
  1. '------------------------------------------------------------
  2. ' cmdSendReport_Click
  3. '
  4. '------------------------------------------------------------
  5. Private Sub cmdSendReport_Click()
  6. Dim varName As String
  7. Dim intNumOfRecs As Integer
  8. Dim intResponse As Integer
  9.  
  10. varName = Me![txtName]
  11.  
  12. If IsNull(varName) Then
  13.   MsgBox "You must enter a Name", vbExclamation, "No Name Entered"
  14.     Me![txtName].SetFocus
  15.       Exit Sub
  16. End If
  17.  
  18. Do
  19.   intNumOfRecs = DCount("*", "qSalesQueryLastName", "[Name] = '" & varName & "'")
  20.  
  21.   If intNumOfRecs = 0 Then
  22.     intResponse = MsgBox("No Records exists for a Name of [" & varName & "], try again?", _
  23.                           vbQuestion + vbYesNo + vbDefaultButton1, "Parameter Prompt")
  24.       If intResponse = vbYes Then Me![txtName].SetFocus: Exit Sub
  25.   Else
  26.   'Record(s) exist, Run Macro or execute code here
  27.  
  28.  
  29.     On Error GoTo cmdSendReport_Click_Err
  30.  
  31.  
  32.  
  33.    DoCmd.OpenReport "PurchaseByPerson", acViewPreview
  34.       intResponse = MsgBox("EMail Report?", vbQuestion + vbYesNo, "E-Mail Confirmation")
  35.         If intResponse = vbYes Then
  36.           DoCmd.Close acReport, "PurchaseByPerson"
  37.           DoCmd.SendObject acReport, "PurchaseByPerson", "PDFFormat(*.pdf)", "", "", "", "Dina poster", _
  38.                            "Härär dem poster du har köpt av CBC", True, ""
  39.         Else
  40.           DoCmd.Close acReport, "PurchaseByPerson"
  41.             Exit Sub
  42.         End If
  43.   End If
  44.  
  45.  
  46. cmdSendReport_Click_Exit:
  47.     Exit Sub
  48.  
  49. cmdSendReport_Click_Err:
  50.     MsgBox Error$
  51.     Resume cmdSendReport_Click_Exit
  52.  
  53.  
  54.  
  55.  
  56. Loop Until intNumOfRecs > 0 Or intResponse = vbNo
  57.  
  58. End Sub
  59.  
  60.  
  61.  
Jun 20 '10 #5
lupis
12
Also when supplying a empty string, I am getting run time error 94 "Invalid use of null"
The debugger stops at:

Expand|Select|Wrap|Line Numbers
  1. varName = Me![txtName]
Jun 20 '10 #6
ADezii
8,834 Expert 8TB
@lupis
Why does the Report need to be accessible and simply not visible?
Jun 21 '10 #7
lupis
12
Sorry, I mean actually vissible, its is partially vissible, but as long as the SendObject has focus with the email form and the report is behind the form, I cant see the report completly or its result
Jun 21 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
7
by: Dr. Know | last post by:
I am working on an ASP page that writes to several databases, ranging from MDBs to x-base. One of the tasks involves using an existing highest value from the DB and incrementing it before...
9
by: Tony Williams | last post by:
I have two tables 1.tblmonth which holds two fields txtmonth and txtqtrlabel and 2. tblmain which holds a number of fields but in particular a field called txtqtrlabel2. The two tables are linked...
1
by: Dlady2004 | last post by:
I am trying to create a macro that opens a form and displays a message box and records if the condition holds true if no records match I want it to go to the next criteria in another form here is...
4
by: tmountjr | last post by:
I've got a user who's trying to export a text file with unicode formatting. When he exports it as straight ascii, some of the foreign characters (mostly just accent marks and the like - no...
27
by: Yan | last post by:
A lot of times when reading open software, i come across macros that are defined as follows: #define CALL_FUNCS(x) \ do { \ func1(x); \ func2(x); \ func3(x); \ } while (0);
6
by: Efim | last post by:
Hi all, Due to performance issue, I want to pevent execution of ToString() function in the code like the following: if(reporting_level & DEBUG_LEVEL) log(reporting_level,string.Format("Event of...
4
by: Fred Nelson | last post by:
Hi: I'm trying to write a routine that will permit my VB.NET program to pause execution for five minutes and try a task again if there is a failure. After a set number of tries it should return...
12
by: greg | last post by:
Hi, Can anyone help me with the following issue: How can I pause the execution of a program until a given file is created (by another process) in a specified directory? Any ideas would be...
0
by: thesti | last post by:
hello, i have some jbuttons in my frame. and i have a recursive method, which will check a certain condition and if satisfied, will move one of the jbuttons location to somewhere else in the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.